1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-11 20:27:12 +01:00
metamask-extension/ui/hooks/useOriginMetadata.js
Mark Stacey 3732c5f71e
Add JSDoc ESLint rules (#12112)
ESLint rules have been added to enforce our JSDoc conventions. These
rules were introduced by updating `@metamask/eslint-config` to v9.

Some of the rules have been disabled because the effort to fix all lint
errors was too high. It might be easiest to enable these rules one
directory at a time, or one rule at a time.

Most of the changes in this PR were a result of running
`yarn lint:fix`. There were a handful of manual changes that seemed
obvious and simple to make. Anything beyond that and the rule was left
disabled.
2022-01-07 12:27:33 -03:30

43 lines
1.2 KiB
JavaScript

import { useSelector } from 'react-redux';
import { getSubjectMetadata } from '../selectors';
import { SUBJECT_TYPES } from '../../shared/constants/app';
/**
* @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,
subjectType: SUBJECT_TYPES.UNKNOWN,
};
if (subjectMetadata?.[origin]) {
return {
...minimumOriginMetadata,
...subjectMetadata[origin],
};
}
return minimumOriginMetadata;
}