Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove circular dependencies in ui/components/app/snaps folder #30022

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
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
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 @@
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 @@
};
t?: (key: string) => string;
contentBackgroundColor?: string | undefined;
componentMap: COMPONENT_MAPPING;
};

/**
Expand Down Expand Up @@ -102,7 +102,7 @@
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 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
Loading