2023-04-25 19:20:37 +02:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2022-02-15 01:02:51 +01:00
|
|
|
import { useHistory, useLocation } from 'react-router-dom';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2023-03-08 19:29:23 +01:00
|
|
|
import {
|
|
|
|
SnapCaveatType,
|
|
|
|
WALLET_SNAP_PERMISSION_KEY,
|
|
|
|
} from '@metamask/rpc-methods';
|
2023-04-25 19:20:37 +02:00
|
|
|
import classnames from 'classnames';
|
2022-02-15 01:02:51 +01:00
|
|
|
import Button from '../../../../components/ui/button';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
|
|
|
import {
|
2023-04-25 19:20:37 +02:00
|
|
|
Color,
|
|
|
|
FLEX_WRAP,
|
2023-02-02 21:15:26 +01:00
|
|
|
TextColor,
|
2023-04-24 12:21:37 +02:00
|
|
|
TextVariant,
|
2022-02-15 01:02:51 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
2023-06-07 15:18:49 +02:00
|
|
|
import SnapAuthorshipExpanded from '../../../../components/app/snaps/snap-authorship-expanded';
|
2022-02-15 01:02:51 +01:00
|
|
|
import Box from '../../../../components/ui/box';
|
2023-04-25 16:32:51 +02:00
|
|
|
import SnapRemoveWarning from '../../../../components/app/snaps/snap-remove-warning';
|
2022-02-15 01:02:51 +01:00
|
|
|
import ConnectedSitesList from '../../../../components/app/connected-sites-list';
|
2023-04-25 19:20:37 +02:00
|
|
|
|
2022-02-15 01:02:51 +01:00
|
|
|
import { SNAPS_LIST_ROUTE } from '../../../../helpers/constants/routes';
|
|
|
|
import {
|
|
|
|
removeSnap,
|
|
|
|
removePermissionsFor,
|
2023-03-08 19:29:23 +01:00
|
|
|
updateCaveat,
|
2022-02-15 01:02:51 +01:00
|
|
|
} from '../../../../store/actions';
|
2022-09-12 18:36:40 +02:00
|
|
|
import {
|
|
|
|
getSnaps,
|
2023-03-08 19:29:23 +01:00
|
|
|
getSubjectsWithSnapPermission,
|
2022-09-12 18:36:40 +02:00
|
|
|
getPermissions,
|
2023-03-08 19:29:23 +01:00
|
|
|
getPermissionSubjects,
|
2023-04-05 15:34:33 +02:00
|
|
|
getTargetSubjectMetadata,
|
2022-09-12 18:36:40 +02:00
|
|
|
} from '../../../../selectors';
|
2023-04-25 19:20:37 +02:00
|
|
|
import { getSnapName } from '../../../../helpers/utils/util';
|
2023-04-26 18:17:25 +02:00
|
|
|
import { Text, BUTTON_VARIANT } from '../../../../components/component-library';
|
2023-04-25 16:32:51 +02:00
|
|
|
import SnapPermissionsList from '../../../../components/app/snaps/snap-permissions-list';
|
2023-04-25 19:20:37 +02:00
|
|
|
import { SnapDelineator } from '../../../../components/app/snaps/snap-delineator';
|
2023-05-03 10:53:58 +02:00
|
|
|
import { DelineatorType } from '../../../../helpers/constants/snaps';
|
2022-02-15 01:02:51 +01:00
|
|
|
|
|
|
|
function ViewSnap() {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const history = useHistory();
|
|
|
|
const location = useLocation();
|
2023-04-25 19:20:37 +02:00
|
|
|
const descriptionRef = useRef(null);
|
2022-02-15 01:02:51 +01:00
|
|
|
const { pathname } = location;
|
2022-05-11 22:14:53 +02:00
|
|
|
// The snap ID is in URI-encoded form in the last path segment of the URL.
|
|
|
|
const decodedSnapId = decodeURIComponent(pathname.match(/[^/]+$/u)[0]);
|
2022-02-15 01:02:51 +01:00
|
|
|
const snaps = useSelector(getSnaps);
|
|
|
|
const snap = Object.entries(snaps)
|
|
|
|
.map(([_, snapState]) => snapState)
|
2022-05-11 22:14:53 +02:00
|
|
|
.find((snapState) => snapState.id === decodedSnapId);
|
2022-02-15 01:02:51 +01:00
|
|
|
|
2022-03-09 21:20:48 +01:00
|
|
|
const [isShowingRemoveWarning, setIsShowingRemoveWarning] = useState(false);
|
2023-04-25 19:20:37 +02:00
|
|
|
const [isDescriptionOpen, setIsDescriptionOpen] = useState(false);
|
|
|
|
const [isOverflowing, setIsOverflowing] = useState(false);
|
2022-03-09 21:20:48 +01:00
|
|
|
|
2022-02-15 01:02:51 +01:00
|
|
|
useEffect(() => {
|
|
|
|
if (!snap) {
|
|
|
|
history.push(SNAPS_LIST_ROUTE);
|
|
|
|
}
|
|
|
|
}, [history, snap]);
|
|
|
|
|
2023-04-25 19:20:37 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setIsOverflowing(
|
|
|
|
descriptionRef.current &&
|
|
|
|
descriptionRef.current.offsetHeight <
|
|
|
|
descriptionRef.current.scrollHeight,
|
|
|
|
);
|
|
|
|
}, [descriptionRef]);
|
|
|
|
|
2022-02-15 01:02:51 +01:00
|
|
|
const connectedSubjects = useSelector((state) =>
|
2023-03-08 19:29:23 +01:00
|
|
|
getSubjectsWithSnapPermission(state, snap?.id),
|
2022-02-15 01:02:51 +01:00
|
|
|
);
|
2022-09-13 12:41:29 +02:00
|
|
|
const permissions = useSelector(
|
|
|
|
(state) => snap && getPermissions(state, snap.id),
|
|
|
|
);
|
2023-03-08 19:29:23 +01:00
|
|
|
const subjects = useSelector((state) => getPermissionSubjects(state));
|
2023-04-05 15:34:33 +02:00
|
|
|
const targetSubjectMetadata = useSelector((state) =>
|
|
|
|
getTargetSubjectMetadata(state, snap?.id),
|
|
|
|
);
|
2022-02-15 01:02:51 +01:00
|
|
|
const dispatch = useDispatch();
|
2023-03-08 19:29:23 +01:00
|
|
|
|
|
|
|
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],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-15 01:02:51 +01:00
|
|
|
if (!snap) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-04 19:40:24 +02:00
|
|
|
|
2023-04-24 12:21:37 +02:00
|
|
|
const snapName = getSnapName(snap.id, targetSubjectMetadata);
|
2022-04-04 19:40:24 +02:00
|
|
|
|
2023-04-25 19:20:37 +02:00
|
|
|
const shouldDisplayMoreButton = isOverflowing && !isDescriptionOpen;
|
|
|
|
const handleMoreClick = () => {
|
|
|
|
setIsDescriptionOpen(true);
|
|
|
|
};
|
|
|
|
|
2022-02-15 01:02:51 +01:00
|
|
|
return (
|
2023-04-24 12:21:37 +02:00
|
|
|
<Box
|
|
|
|
className="view-snap"
|
2023-04-25 19:20:37 +02:00
|
|
|
paddingBottom={[4, 8]}
|
|
|
|
paddingTop={[4, 8]}
|
|
|
|
paddingLeft={4}
|
|
|
|
paddingRight={4}
|
2023-04-24 12:21:37 +02:00
|
|
|
>
|
2023-06-07 15:18:49 +02:00
|
|
|
<SnapAuthorshipExpanded snapId={snap.id} snap={snap} />
|
2023-04-25 19:20:37 +02:00
|
|
|
<Box className="view-snap__description" marginTop={[4, 7]}>
|
|
|
|
<SnapDelineator type={DelineatorType.Description} snapName={snapName}>
|
|
|
|
<Box
|
|
|
|
className={classnames('view-snap__description__wrapper', {
|
|
|
|
open: isDescriptionOpen,
|
|
|
|
})}
|
|
|
|
ref={descriptionRef}
|
2022-02-15 01:02:51 +01:00
|
|
|
>
|
2023-04-25 19:20:37 +02:00
|
|
|
<Text>{snap?.manifest.description}</Text>
|
|
|
|
{shouldDisplayMoreButton && (
|
|
|
|
<Button
|
|
|
|
className="view-snap__description__more-button"
|
2023-04-26 18:17:25 +02:00
|
|
|
type={BUTTON_VARIANT.LINK}
|
2023-04-25 19:20:37 +02:00
|
|
|
onClick={handleMoreClick}
|
|
|
|
>
|
|
|
|
<Text color={Color.infoDefault}>{t('more')}</Text>
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</SnapDelineator>
|
2023-04-24 12:21:37 +02:00
|
|
|
</Box>
|
|
|
|
<Box className="view-snap__permissions" marginTop={12}>
|
2023-04-25 19:20:37 +02:00
|
|
|
<Text variant={TextVariant.bodyLgMedium}>{t('permissions')}</Text>
|
2023-04-24 12:21:37 +02:00
|
|
|
<SnapPermissionsList
|
2023-07-06 22:54:27 +02:00
|
|
|
snapId={decodedSnapId}
|
2023-04-24 12:21:37 +02:00
|
|
|
permissions={permissions ?? {}}
|
|
|
|
targetSubjectMetadata={targetSubjectMetadata}
|
2023-07-06 22:54:27 +02:00
|
|
|
showOptions
|
2023-04-24 12:21:37 +02:00
|
|
|
/>
|
|
|
|
</Box>
|
2023-04-25 19:20:37 +02:00
|
|
|
<Box className="view-snap__connected-sites" marginTop={12}>
|
2023-04-24 12:21:37 +02:00
|
|
|
<Text variant={TextVariant.bodyLgMedium} marginBottom={4}>
|
|
|
|
{t('connectedSites')}
|
|
|
|
</Text>
|
|
|
|
<ConnectedSitesList
|
|
|
|
connectedSubjects={connectedSubjects}
|
|
|
|
onDisconnect={(origin) => {
|
|
|
|
onDisconnect(origin, snap.id);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
2023-04-25 19:20:37 +02:00
|
|
|
<Box className="view-snap__remove" marginTop={12}>
|
2023-04-24 12:21:37 +02:00
|
|
|
<Text variant={TextVariant.bodyLgMedium} color={TextColor.textDefault}>
|
|
|
|
{t('removeSnap')}
|
|
|
|
</Text>
|
|
|
|
<Text variant={TextVariant.bodyMd} color={TextColor.textDefault}>
|
|
|
|
{t('removeSnapDescription')}
|
|
|
|
</Text>
|
|
|
|
<Box marginTop={4}>
|
|
|
|
<Button
|
|
|
|
className="view-snap__remove-button"
|
|
|
|
type="danger"
|
|
|
|
onClick={() => setIsShowingRemoveWarning(true)}
|
|
|
|
>
|
2023-04-25 19:20:37 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodyMd}
|
|
|
|
color={TextColor.errorDefault}
|
|
|
|
flexWrap={FLEX_WRAP.NO_WRAP}
|
|
|
|
ellipsis
|
|
|
|
style={{ overflow: 'hidden' }}
|
|
|
|
>
|
2023-04-24 12:21:37 +02:00
|
|
|
{`${t('remove')} ${snapName}`}
|
|
|
|
</Text>
|
|
|
|
</Button>
|
|
|
|
{isShowingRemoveWarning && (
|
|
|
|
<SnapRemoveWarning
|
|
|
|
onCancel={() => setIsShowingRemoveWarning(false)}
|
|
|
|
onSubmit={async () => {
|
|
|
|
await dispatch(removeSnap(snap.id));
|
2022-02-15 01:02:51 +01:00
|
|
|
}}
|
2023-04-24 12:21:37 +02:00
|
|
|
snapName={snapName}
|
|
|
|
/>
|
|
|
|
)}
|
2022-02-15 01:02:51 +01:00
|
|
|
</Box>
|
2023-04-24 12:21:37 +02:00
|
|
|
</Box>
|
|
|
|
</Box>
|
2022-02-15 01:02:51 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-24 12:21:37 +02:00
|
|
|
export default ViewSnap;
|