-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## **Description** This PR adds a settings section for each preinstalled snaps that exposes a `onSettingsPage` handler [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29234?quickstart=1) ## **Related issues** Fixes: MetaMask/snaps#2874 ## **Manual testing steps** 1. Open MetaMask's settings 2. You should see the preinstalled snaps settings ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** ### **After** ![image](https://github.com/user-attachments/assets/55f4027c-258e-4039-9006-95228cfdca3f) ![image](https://github.com/user-attachments/assets/e05e6964-ac61-49a1-bb28-e8d7d077dadc) ![image](https://github.com/user-attachments/assets/b2db7088-069a-4b83-9740-11f23f36e879) ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
1 parent
70c7e35
commit c80f17a
Showing
16 changed files
with
270 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './snap-settings-renderer'; |
81 changes: 81 additions & 0 deletions
81
ui/components/app/snaps/snap-settings-page/snap-settings-renderer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { FunctionComponent, useEffect, useMemo } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { useLocation } from 'react-router-dom'; | ||
import { useI18nContext } from '../../../../hooks/useI18nContext'; | ||
|
||
import { deleteInterface } from '../../../../store/actions'; | ||
import { Box, Text } from '../../../component-library'; | ||
import { | ||
BackgroundColor, | ||
BlockSize, | ||
TextVariant, | ||
} from '../../../../helpers/constants/design-system'; | ||
import { SnapDelineator } from '../snap-delineator'; | ||
import { getSnapMetadata } from '../../../../selectors'; | ||
import { DelineatorType } from '../../../../helpers/constants/snaps'; | ||
import { Copyable } from '../copyable'; | ||
import { SnapUIRenderer } from '../snap-ui-renderer'; | ||
import { useSnapSettings } from '../../../../hooks/snaps/useSnapSettings'; | ||
import { decodeSnapIdFromPathname } from '../../../../helpers/utils/snaps'; | ||
|
||
type SnapSettingsRendererProps = { | ||
snapId: string; | ||
}; | ||
|
||
export const SnapSettingsRenderer: FunctionComponent< | ||
SnapSettingsRendererProps | ||
> = () => { | ||
const { pathname } = useLocation(); | ||
const dispatch = useDispatch(); | ||
const t = useI18nContext(); | ||
|
||
const snapId = useMemo(() => decodeSnapIdFromPathname(pathname), [pathname]); | ||
|
||
const { name: snapName } = useSelector((state) => | ||
getSnapMetadata(state, snapId), | ||
); | ||
|
||
const { data, error, loading } = useSnapSettings({ | ||
snapId, | ||
}); | ||
|
||
const interfaceId = !loading && !error ? data?.id : undefined; | ||
|
||
useEffect(() => { | ||
return () => { | ||
interfaceId && dispatch(deleteInterface(interfaceId)); | ||
}; | ||
}, [interfaceId]); | ||
|
||
if (!snapId) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Box | ||
height={BlockSize.Full} | ||
width={BlockSize.Full} | ||
backgroundColor={BackgroundColor.backgroundDefault} | ||
> | ||
{error && ( | ||
<Box height={BlockSize.Full} padding={4}> | ||
<SnapDelineator snapName={snapName} type={DelineatorType.Error}> | ||
<Text variant={TextVariant.bodySm} marginBottom={4}> | ||
{t('snapsUIError', [<b key="0">{snapName}</b>])} | ||
</Text> | ||
<Copyable text={error.message} /> | ||
</SnapDelineator> | ||
</Box> | ||
)} | ||
{(interfaceId || loading) && ( | ||
<SnapUIRenderer | ||
snapId={snapId} | ||
interfaceId={interfaceId} | ||
isLoading={loading} | ||
useDelineator={false} | ||
contentBackgroundColor={BackgroundColor.backgroundDefault} | ||
/> | ||
)} | ||
</Box> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { SnapId } from '@metamask/snaps-sdk'; | ||
import { isProduction } from '../../../shared/modules/environment'; | ||
|
||
/** | ||
* Check if the given value is a valid snap ID. | ||
* | ||
* NOTE: This function is a duplicate oF a yet to be released version in @metamask/snaps-utils. | ||
* | ||
* @param value - The value to check. | ||
* @returns `true` if the value is a valid snap ID, and `false` otherwise. | ||
*/ | ||
export function isSnapId(value: unknown): value is SnapId { | ||
return ( | ||
(typeof value === 'string' || value instanceof String) && | ||
(value.startsWith('local:') || value.startsWith('npm:')) | ||
); | ||
} | ||
|
||
/** | ||
* Decode a snap ID fron a pathname. | ||
* | ||
* @param pathname - The pathname to decode the snap ID from. | ||
* @returns The decoded snap ID, or `undefined` if the snap ID could not be decoded. | ||
*/ | ||
export const decodeSnapIdFromPathname = (pathname: string) => { | ||
const snapIdURI = pathname?.match(/[^/]+$/u)?.[0]; | ||
return snapIdURI && decodeURIComponent(snapIdURI); | ||
}; | ||
|
||
const IGNORED_EXAMPLE_SNAPS = ['npm:@metamask/preinstalled-example-snap']; | ||
|
||
/** | ||
* Check if the given snap ID is ignored in production. | ||
* | ||
* @param snapId - The snap ID to check. | ||
* @returns `true` if the snap ID is ignored in production, and `false` otherwise. | ||
*/ | ||
export const isSnapIgnoredInProd = (snapId: string) => { | ||
return isProduction() ? IGNORED_EXAMPLE_SNAPS.includes(snapId) : false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { | ||
forceUpdateMetamaskState, | ||
handleSnapRequest, | ||
} from '../../store/actions'; | ||
|
||
export function useSnapSettings({ snapId }: { snapId?: string }) { | ||
const dispatch = useDispatch(); | ||
const [loading, setLoading] = useState<boolean>(true); | ||
const [data, setData] = useState<{ id: string } | undefined>(undefined); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
let cancelled = false; | ||
async function fetchPage(id: string) { | ||
try { | ||
setError(undefined); | ||
setLoading(true); | ||
|
||
const newData = (await handleSnapRequest({ | ||
snapId: id, | ||
origin: '', | ||
handler: 'onSettingsPage', | ||
request: { | ||
jsonrpc: '2.0', | ||
method: ' ', | ||
}, | ||
})) as { id: string }; | ||
if (!cancelled) { | ||
setData(newData); | ||
forceUpdateMetamaskState(dispatch); | ||
} | ||
} catch (err) { | ||
if (!cancelled) { | ||
setError(err as Error); | ||
} | ||
} finally { | ||
if (!cancelled) { | ||
setLoading(false); | ||
} | ||
} | ||
} | ||
|
||
if (snapId) { | ||
fetchPage(snapId); | ||
} | ||
|
||
return () => { | ||
cancelled = true; | ||
}; | ||
}, [snapId]); | ||
|
||
return { data, error, loading }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.