Skip to content

Commit

Permalink
refactor: remove circular dependencies in ui/components/app/snaps f…
Browse files Browse the repository at this point in the history
…older
  • Loading branch information
davidmurdoch committed Jan 30, 2025
1 parent 538bbc9 commit 2427690
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export const COMPONENT_MAPPING = {
Banner: banner,
Skeleton: skeleton,
};

export type COMPONENT_MAPPING = typeof COMPONENT_MAPPING;
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChangeEvent as ReactChangeEvent } from 'react';
import { JSXElement, SnapsChildren } from '@metamask/snaps-sdk/jsx';
import type { COMPONENT_MAPPING } from '.';

export type UIComponentParams<T extends JSXElement> = {
map: Record<string, number>;
Expand All @@ -14,6 +15,7 @@ export type UIComponentParams<T extends JSXElement> = {
};
t: (key: string) => string;
contentBackgroundColor: string | undefined;
componentMap: COMPONENT_MAPPING;
};

export type UIComponent = {
Expand Down
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/snap-ui-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import { mapToExtensionCompatibleColor, mapToTemplate } from './utils';
import { COMPONENT_MAPPING } from './components';

// Component that maps Snaps UI JSON format to MetaMask Template Renderer format
const SnapUIRendererComponent = ({
Expand Down Expand Up @@ -86,6 +87,7 @@ const SnapUIRendererComponent = ({
promptLegacyProps,
t,
contentBackgroundColor: backgroundColor,
componentMap: COMPONENT_MAPPING,
}),
[content, onCancel, useFooter, promptLegacyProps, t, backgroundColor],
);
Expand Down
8 changes: 4 additions & 4 deletions ui/components/app/snaps/snap-ui-renderer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
BackgroundColor,
BorderRadius,
} from '../../../../helpers/constants/design-system';
import { COMPONENT_MAPPING } from './components';
import { UIComponent } from './components/types';
import type { COMPONENT_MAPPING, UIComponent } from './components/types';

Check failure on line 12 in ui/components/app/snaps/snap-ui-renderer/utils.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Module '"./components/types"' declares 'COMPONENT_MAPPING' locally, but it is not exported.

export type MapToTemplateParams = {
map: Record<string, number>;
Expand All @@ -25,6 +24,7 @@ export type MapToTemplateParams = {
};
t?: (key: string) => string;
contentBackgroundColor?: string | undefined;
componentMap: COMPONENT_MAPPING;
};

/**
Expand Down Expand Up @@ -102,7 +102,7 @@ function generateKey(
export const mapToTemplate = (params: MapToTemplateParams): UIComponent => {
const { type, key } = params.element;
const elementKey = key ?? generateKey(params.map, params.element);
const mapped = COMPONENT_MAPPING[
const mapped = params.componentMap[
type as Exclude<JSXElement['type'], 'Option' | 'Radio' | 'SelectorOption'>
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -112,7 +112,7 @@ export const mapToTemplate = (params: MapToTemplateParams): UIComponent => {

export const mapTextToTemplate = (
elements: NonEmptyArray<JSXElement | string>,
params: Pick<MapToTemplateParams, 'map'>,
params: Pick<MapToTemplateParams, 'map' | 'componentMap'>,
): NonEmptyArray<UIComponent | string> =>
elements.map((element) => {
// With the introduction of JSX elements here can be strings.
Expand Down

0 comments on commit 2427690

Please sign in to comment.