import React, { useEffect, useState } from 'react'; import { useHistory, useLocation } from 'react-router-dom'; import { useDispatch, useSelector } from 'react-redux'; import Button from '../../../../components/ui/button'; import Typography from '../../../../components/ui/typography'; import { useI18nContext } from '../../../../hooks/useI18nContext'; import { TYPOGRAPHY, COLORS, TEXT_ALIGN, FRACTIONS, } 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, } from '../../../../store/actions'; import { getSnaps, getSubjectsWithPermission } 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 authorshipPillUrl = `https://npmjs.com/package/${snap?.manifest.source.location.npm.packageName}`; const connectedSubjects = useSelector((state) => getSubjectsWithPermission(state, snap?.permissionName), ); const dispatch = useDispatch(); const onDisconnect = (connectedOrigin, snapPermissionName) => { dispatch( removePermissionsFor({ [connectedOrigin]: [snapPermissionName], }), ); }; const onToggle = () => { if (snap.enabled) { dispatch(disableSnap(snap.id)); } else { dispatch(enableSnap(snap.id)); } }; if (!snap) { return null; } const versionHistory = snap.versionHistory ?? []; const [firstInstall] = versionHistory; return (