You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.1 KiB
103 lines
3.1 KiB
4 years ago
|
import React, { memo } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import { isEqual } from 'lodash';
|
||
|
import { safeComponentList } from './safe-component-list';
|
||
4 years ago
|
|
||
|
function getElement(section) {
|
||
4 years ago
|
const { element } = section;
|
||
|
const Element = safeComponentList[element];
|
||
4 years ago
|
if (!Element) {
|
||
|
throw new Error(
|
||
|
`${element} is not in the safe component list for MetaMask template renderer`,
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
4 years ago
|
return Element;
|
||
4 years ago
|
}
|
||
|
|
||
|
const MetaMaskTemplateRenderer = ({ sections }) => {
|
||
|
if (!sections) {
|
||
|
// If sections is null eject early by returning null
|
||
4 years ago
|
return null;
|
||
4 years ago
|
} else if (typeof sections === 'string') {
|
||
|
// React can render strings directly, so return the string
|
||
4 years ago
|
return sections;
|
||
4 years ago
|
} else if (
|
||
|
sections &&
|
||
|
typeof sections === 'object' &&
|
||
|
!Array.isArray(sections)
|
||
|
) {
|
||
|
// If dealing with a single entry, then render a single object without key
|
||
4 years ago
|
const Element = getElement(sections);
|
||
4 years ago
|
return (
|
||
|
<Element {...sections.props}>
|
||
|
{typeof sections.children === 'object' ? (
|
||
|
<MetaMaskTemplateRenderer sections={sections.children} />
|
||
|
) : (
|
||
|
sections?.children
|
||
|
)}
|
||
|
</Element>
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
|
||
|
// The last case is dealing with an array of objects
|
||
|
return (
|
||
|
<>
|
||
|
{sections.reduce((allChildren, child) => {
|
||
|
if (typeof child === 'string') {
|
||
|
// React can render strings directly, so push them into the accumulator
|
||
4 years ago
|
allChildren.push(child);
|
||
4 years ago
|
} else {
|
||
|
// If the entry in array is not a string, then it must be a Section.
|
||
|
// Sections are handled by the main function, but must
|
||
|
// be provided a key when a part of an array.
|
||
|
if (!child.key) {
|
||
|
throw new Error(
|
||
|
'When using array syntax in MetaMask Template Language, you must specify a key for each child of the array',
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
if (typeof child?.children === 'object') {
|
||
|
// If this child has its own children, check if children is an
|
||
|
// object, and in that case use recursion to render.
|
||
|
allChildren.push(
|
||
|
<MetaMaskTemplateRenderer sections={child} key={child.key} />,
|
||
4 years ago
|
);
|
||
4 years ago
|
} else {
|
||
|
// Otherwise render the element.
|
||
4 years ago
|
const Element = getElement(child);
|
||
4 years ago
|
allChildren.push(
|
||
|
<Element key={child.key} {...child.props}>
|
||
|
{child?.children}
|
||
|
</Element>,
|
||
4 years ago
|
);
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
return allChildren;
|
||
4 years ago
|
}, [])}
|
||
|
</>
|
||
4 years ago
|
);
|
||
|
};
|
||
4 years ago
|
|
||
4 years ago
|
export const SectionShape = {
|
||
4 years ago
|
props: PropTypes.object,
|
||
|
element: PropTypes.oneOf(Object.keys(safeComponentList)).isRequired,
|
||
|
key: PropTypes.string,
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
|
const ValidChildren = PropTypes.oneOfType([
|
||
|
PropTypes.string,
|
||
|
PropTypes.shape(SectionShape),
|
||
|
PropTypes.arrayOf(
|
||
|
PropTypes.oneOfType([PropTypes.shape(SectionShape), PropTypes.string]),
|
||
|
),
|
||
4 years ago
|
]);
|
||
4 years ago
|
|
||
4 years ago
|
SectionShape.children = ValidChildren;
|
||
4 years ago
|
|
||
|
MetaMaskTemplateRenderer.propTypes = {
|
||
|
sections: ValidChildren,
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
|
export default memo(MetaMaskTemplateRenderer, (prevProps, nextProps) => {
|
||
4 years ago
|
return isEqual(prevProps.sections, nextProps.sections);
|
||
|
});
|