import React, { useEffect, useState } from 'react'; import { useHistory, useLocation } from 'react-router-dom'; import { useDispatch, useSelector } from 'react-redux'; import { SnapCaveatType, WALLET_SNAP_PERMISSION_KEY, } from '@metamask/rpc-methods'; import Button from '../../../../components/ui/button'; import Typography from '../../../../components/ui/typography'; import { useI18nContext } from '../../../../hooks/useI18nContext'; import { TypographyVariant, TEXT_ALIGN, FRACTIONS, TextColor, } from '../../../../helpers/constants/design-system'; import SnapsAuthorshipPill from '../../../../components/app/flask/snaps-authorship-pill'; import Box from '../../../../components/ui/box'; import SnapRemoveWarning from '../../../../components/app/flask/snap-remove-warning'; import ToggleButton from '../../../../components/ui/toggle-button'; import PermissionsConnectPermissionList from '../../../../components/app/permissions-connect-permission-list/permissions-connect-permission-list'; import ConnectedSitesList from '../../../../components/app/connected-sites-list'; import Tooltip from '../../../../components/ui/tooltip'; import { SNAPS_LIST_ROUTE } from '../../../../helpers/constants/routes'; import { disableSnap, enableSnap, removeSnap, removePermissionsFor, updateCaveat, } from '../../../../store/actions'; import { getSnaps, getSubjectsWithSnapPermission, getPermissions, getPermissionSubjects, } from '../../../../selectors'; import { formatDate } from '../../../../helpers/utils/util'; function ViewSnap() { const t = useI18nContext(); const history = useHistory(); const location = useLocation(); const { pathname } = location; // The snap ID is in URI-encoded form in the last path segment of the URL. const decodedSnapId = decodeURIComponent(pathname.match(/[^/]+$/u)[0]); const snaps = useSelector(getSnaps); const snap = Object.entries(snaps) .map(([_, snapState]) => snapState) .find((snapState) => snapState.id === decodedSnapId); const [isShowingRemoveWarning, setIsShowingRemoveWarning] = useState(false); useEffect(() => { if (!snap) { history.push(SNAPS_LIST_ROUTE); } }, [history, snap]); const connectedSubjects = useSelector((state) => getSubjectsWithSnapPermission(state, snap?.id), ); const permissions = useSelector( (state) => snap && getPermissions(state, snap.id), ); const subjects = useSelector((state) => getPermissionSubjects(state)); const dispatch = useDispatch(); const onToggle = () => { if (snap.enabled) { dispatch(disableSnap(snap.id)); } else { dispatch(enableSnap(snap.id)); } }; const onDisconnect = (connectedOrigin, snapId) => { const caveatValue = subjects[connectedOrigin].permissions[WALLET_SNAP_PERMISSION_KEY] .caveats[0].value; const newCaveatValue = { ...caveatValue }; delete newCaveatValue[snapId]; if (Object.keys(newCaveatValue) > 0) { dispatch( updateCaveat( connectedOrigin, WALLET_SNAP_PERMISSION_KEY, SnapCaveatType.SnapIds, newCaveatValue, ), ); } else { dispatch( removePermissionsFor({ [connectedOrigin]: [WALLET_SNAP_PERMISSION_KEY], }), ); } }; if (!snap) { return null; } const versionHistory = snap.versionHistory ?? []; const [firstInstall] = versionHistory; return (