2022-12-09 13:05:31 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import nanoid from 'nanoid';
|
|
|
|
import { isComponent } from '@metamask/snaps-ui';
|
2023-04-03 18:04:30 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
2022-12-09 13:05:31 +01:00
|
|
|
import MetaMaskTemplateRenderer from '../../metamask-template-renderer/metamask-template-renderer';
|
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2022-12-09 13:05:31 +01:00
|
|
|
FONT_WEIGHT,
|
|
|
|
DISPLAY,
|
|
|
|
FLEX_DIRECTION,
|
2023-03-16 11:45:44 +01:00
|
|
|
OVERFLOW_WRAP,
|
2023-04-03 18:04:30 +02:00
|
|
|
TextVariant,
|
2022-12-09 13:05:31 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
|
|
|
import { SnapDelineator } from '../snap-delineator';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2022-12-20 11:44:49 +01:00
|
|
|
import Box from '../../../ui/box';
|
2023-04-03 18:04:30 +02:00
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
|
|
|
import { getTargetSubjectMetadata } from '../../../../selectors';
|
|
|
|
import { Text } from '../../../component-library';
|
|
|
|
import { Copyable } from '../copyable';
|
|
|
|
import { DelineatorType } from '../../../../helpers/constants/flask';
|
2022-12-09 13:05:31 +01:00
|
|
|
|
|
|
|
export const UI_MAPPING = {
|
|
|
|
panel: (props) => ({
|
|
|
|
element: 'Box',
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
children: props.children.map(mapToTemplate),
|
|
|
|
props: {
|
|
|
|
display: DISPLAY.FLEX,
|
|
|
|
flexDirection: FLEX_DIRECTION.COLUMN,
|
|
|
|
className: 'snap-ui-renderer__panel',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
heading: (props) => ({
|
|
|
|
element: 'Typography',
|
|
|
|
children: props.value,
|
|
|
|
props: {
|
2023-02-02 21:15:26 +01:00
|
|
|
variant: TypographyVariant.H3,
|
2022-12-09 13:05:31 +01:00
|
|
|
fontWeight: FONT_WEIGHT.BOLD,
|
2023-03-16 11:45:44 +01:00
|
|
|
overflowWrap: OVERFLOW_WRAP.BREAK_WORD,
|
2022-12-09 13:05:31 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
text: (props) => ({
|
2022-12-13 15:37:20 +01:00
|
|
|
element: 'SnapUIMarkdown',
|
2022-12-09 13:05:31 +01:00
|
|
|
children: props.value,
|
|
|
|
}),
|
|
|
|
spinner: () => ({
|
|
|
|
element: 'Spinner',
|
|
|
|
props: {
|
|
|
|
className: 'snap-ui-renderer__spinner',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
divider: () => ({
|
|
|
|
element: 'hr',
|
|
|
|
props: {
|
|
|
|
className: 'snap-ui-renderer__divider',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
copyable: (props) => ({
|
|
|
|
element: 'Copyable',
|
|
|
|
props: {
|
|
|
|
text: props.value,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2022-12-20 11:44:22 +01:00
|
|
|
// TODO: Stop exporting this when we remove the mapToTemplate hack in confirmation templates.
|
|
|
|
export const mapToTemplate = (data) => {
|
2022-12-09 13:05:31 +01:00
|
|
|
const { type } = data;
|
|
|
|
const mapped = UI_MAPPING[type](data);
|
|
|
|
// TODO: We may want to have deterministic keys at some point
|
|
|
|
return { ...mapped, key: nanoid() };
|
|
|
|
};
|
|
|
|
|
|
|
|
// Component that maps Snaps UI JSON format to MetaMask Template Renderer format
|
2023-04-03 18:04:30 +02:00
|
|
|
export const SnapUIRenderer = ({
|
|
|
|
snapId,
|
|
|
|
delineatorType = DelineatorType.Content,
|
|
|
|
data,
|
|
|
|
}) => {
|
2022-12-09 13:05:31 +01:00
|
|
|
const t = useI18nContext();
|
2023-04-03 18:04:30 +02:00
|
|
|
const targetSubjectMetadata = useSelector((state) =>
|
|
|
|
getTargetSubjectMetadata(state, snapId),
|
|
|
|
);
|
2022-12-09 13:05:31 +01:00
|
|
|
|
2023-04-03 18:04:30 +02:00
|
|
|
const snapName = getSnapName(snapId, targetSubjectMetadata);
|
2022-12-09 13:05:31 +01:00
|
|
|
|
|
|
|
if (!isComponent(data)) {
|
|
|
|
return (
|
2023-04-03 18:04:30 +02:00
|
|
|
<SnapDelineator snapName={snapName} type={DelineatorType.Error}>
|
|
|
|
<Text variant={TextVariant.bodySm} marginBottom={4}>
|
|
|
|
{t('snapsUIError', [<b key="0">{snapName}</b>])}
|
|
|
|
</Text>
|
|
|
|
<Copyable text={t('snapsInvalidUIError')} />
|
2022-12-09 13:05:31 +01:00
|
|
|
</SnapDelineator>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sections = mapToTemplate(data);
|
|
|
|
|
|
|
|
return (
|
2023-04-03 18:04:30 +02:00
|
|
|
<SnapDelineator snapName={snapName} type={delineatorType}>
|
2022-12-20 11:44:49 +01:00
|
|
|
<Box className="snap-ui-renderer__content">
|
|
|
|
<MetaMaskTemplateRenderer sections={sections} />
|
|
|
|
</Box>
|
2022-12-09 13:05:31 +01:00
|
|
|
</SnapDelineator>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
SnapUIRenderer.propTypes = {
|
|
|
|
snapId: PropTypes.string,
|
2023-04-03 18:04:30 +02:00
|
|
|
delineatorType: PropTypes.string,
|
2022-12-09 13:05:31 +01:00
|
|
|
data: PropTypes.object,
|
|
|
|
};
|