2021-02-04 19:15:23 +01:00
|
|
|
import React, { memo } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { isEqual } from 'lodash';
|
|
|
|
import { safeComponentList } from './safe-component-list';
|
2021-02-02 19:14:04 +01:00
|
|
|
|
|
|
|
function getElement(section) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { element } = section;
|
|
|
|
const Element = safeComponentList[element];
|
2021-02-02 19:14:04 +01:00
|
|
|
if (!Element) {
|
|
|
|
throw new Error(
|
|
|
|
`${element} is not in the safe component list for MetaMask template renderer`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-02 19:14:04 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return Element;
|
2021-02-02 19:14:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const MetaMaskTemplateRenderer = ({ sections }) => {
|
|
|
|
if (!sections) {
|
|
|
|
// If sections is null eject early by returning null
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2021-02-02 19:14:04 +01:00
|
|
|
} else if (typeof sections === 'string') {
|
|
|
|
// React can render strings directly, so return the string
|
2021-02-04 19:15:23 +01:00
|
|
|
return sections;
|
2021-02-02 19:14:04 +01:00
|
|
|
} else if (
|
|
|
|
sections &&
|
|
|
|
typeof sections === 'object' &&
|
|
|
|
!Array.isArray(sections)
|
|
|
|
) {
|
|
|
|
// If dealing with a single entry, then render a single object without key
|
2021-02-04 19:15:23 +01:00
|
|
|
const Element = getElement(sections);
|
2021-02-02 19:14:04 +01:00
|
|
|
return (
|
|
|
|
<Element {...sections.props}>
|
|
|
|
{typeof sections.children === 'object' ? (
|
|
|
|
<MetaMaskTemplateRenderer sections={sections.children} />
|
|
|
|
) : (
|
|
|
|
sections?.children
|
|
|
|
)}
|
|
|
|
</Element>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-02 19:14:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2021-02-04 19:15:23 +01:00
|
|
|
allChildren.push(child);
|
2021-02-02 19:14:04 +01:00
|
|
|
} 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',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-02 19:14:04 +01:00
|
|
|
}
|
|
|
|
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} />,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-02 19:14:04 +01:00
|
|
|
} else {
|
|
|
|
// Otherwise render the element.
|
2021-02-04 19:15:23 +01:00
|
|
|
const Element = getElement(child);
|
2021-02-02 19:14:04 +01:00
|
|
|
allChildren.push(
|
|
|
|
<Element key={child.key} {...child.props}>
|
|
|
|
{child?.children}
|
|
|
|
</Element>,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-02-02 19:14:04 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return allChildren;
|
2021-02-02 19:14:04 +01:00
|
|
|
}, [])}
|
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2021-02-02 19:14:04 +01:00
|
|
|
|
2021-02-12 18:24:50 +01:00
|
|
|
export const SectionShape = {
|
2021-02-02 19:14:04 +01:00
|
|
|
props: PropTypes.object,
|
|
|
|
element: PropTypes.oneOf(Object.keys(safeComponentList)).isRequired,
|
|
|
|
key: PropTypes.string,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-02-02 19:14:04 +01:00
|
|
|
|
|
|
|
const ValidChildren = PropTypes.oneOfType([
|
|
|
|
PropTypes.string,
|
|
|
|
PropTypes.shape(SectionShape),
|
|
|
|
PropTypes.arrayOf(
|
|
|
|
PropTypes.oneOfType([PropTypes.shape(SectionShape), PropTypes.string]),
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2021-02-02 19:14:04 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
SectionShape.children = ValidChildren;
|
2021-02-02 19:14:04 +01:00
|
|
|
|
|
|
|
MetaMaskTemplateRenderer.propTypes = {
|
|
|
|
sections: ValidChildren,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-02-02 19:14:04 +01:00
|
|
|
|
|
|
|
export default memo(MetaMaskTemplateRenderer, (prevProps, nextProps) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return isEqual(prevProps.sections, nextProps.sections);
|
|
|
|
});
|