1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/hooks/useOriginMetadata.js
Erik Marks c03b6dd19b
Subject metadata cleanup (#13090)
This PR fixes some subject metadata-related issues that were missed in #13026.
2021-12-13 12:10:20 -08:00

40 lines
1.1 KiB
JavaScript

import { useSelector } from 'react-redux';
import { getSubjectMetadata } from '../selectors';
/**
* @typedef {Object} OriginMetadata
* @property {string} hostname - The hostname of the origin (host + port)
* @property {string} origin - The original origin string itself
* @property {string} [iconUrl] - The origin's site icon URL, if available
* @property {string} [name] - The registered name of the origin if available
*/
/**
* Gets origin metadata from redux and formats it appropriately.
* @param {string} origin - The fully formed url of the site interacting with
* MetaMask
* @returns {OriginMetadata | null} - The origin metadata available for the
* current origin
*/
export function useOriginMetadata(origin) {
const subjectMetadata = useSelector(getSubjectMetadata);
if (!origin) {
return null;
}
const url = new URL(origin);
const minimumOriginMetadata = {
host: url.host,
hostname: url.hostname,
origin,
};
if (subjectMetadata?.[origin]) {
return {
...minimumOriginMetadata,
...subjectMetadata[origin],
};
}
return minimumOriginMetadata;
}