-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Feat: show site if no connection but has gator permissions #36811
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
base: feat/revoke-all-permissions-on-site-disconnect
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from 'react'; | |
import { useHistory } from 'react-router-dom'; | ||
import { useSelector } from 'react-redux'; | ||
import { isSnapId } from '@metamask/snaps-utils'; | ||
import { SubjectType } from '@metamask/permission-controller'; | ||
import { Content, Header, Page } from '../page'; | ||
import { | ||
Box, | ||
|
@@ -27,8 +28,14 @@ import { | |
REVIEW_PERMISSIONS, | ||
GATOR_PERMISSIONS, | ||
} from '../../../../helpers/constants/routes'; | ||
import { getConnectedSitesListWithNetworkInfo } from '../../../../selectors'; | ||
import { | ||
getConnectedSitesListWithNetworkInfo, | ||
getTargetSubjectMetadata, | ||
} from '../../../../selectors'; | ||
import { getGatorPermissionsMap } from '../../../../selectors/gator-permissions/gator-permissions'; | ||
import { isGatorPermissionsRevocationFeatureEnabled } from '../../../../../shared/modules/environment'; | ||
import { getURLHostName } from '../../../../helpers/utils/util'; | ||
import { getNetworkNameByChainId } from '../../../../../shared/modules/feature-flags'; | ||
import { ConnectionListItem } from './connection-list-item'; | ||
|
||
export const PermissionsPage = () => { | ||
|
@@ -39,10 +46,85 @@ export const PermissionsPage = () => { | |
const sitesConnectionsList = useSelector( | ||
getConnectedSitesListWithNetworkInfo, | ||
); | ||
const gatorPermissionsMap = useSelector(getGatorPermissionsMap); | ||
|
||
// Get all unique site origins from gator permissions with their first chainId and all unique addresses | ||
const getUniqueSiteOriginsFromGatorPermissions = (permissionsMap) => { | ||
const siteOriginsMap = new Map(); | ||
|
||
Object.values(permissionsMap).forEach((permissionTypeMap) => { | ||
Object.values(permissionTypeMap).forEach((permissions) => { | ||
permissions.forEach((permission) => { | ||
if (permission.siteOrigin) { | ||
if (!siteOriginsMap.has(permission.siteOrigin)) { | ||
// Store the first permission data for this site origin | ||
siteOriginsMap.set(permission.siteOrigin, { | ||
addresses: new Set(), | ||
chainId: permission.permissionResponse.chainId, | ||
}); | ||
} | ||
|
||
// Add address to the set if it exists | ||
const permissionData = siteOriginsMap.get(permission.siteOrigin); | ||
if (permission.permissionResponse.address) { | ||
permissionData.addresses.add( | ||
permission.permissionResponse.address.toLowerCase(), | ||
); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
// Convert Set to Array for each site origin | ||
siteOriginsMap.forEach((permissionData) => { | ||
permissionData.addresses = Array.from(permissionData.addresses); | ||
}); | ||
|
||
return siteOriginsMap; | ||
}; | ||
|
||
// Get merged connections list using useSelector to access getTargetSubjectMetadata | ||
const mergedConnectionsList = useSelector((state) => { | ||
if (!isGatorPermissionsRevocationFeatureEnabled()) { | ||
return sitesConnectionsList; | ||
} | ||
|
||
const gatorSiteOriginsMap = | ||
getUniqueSiteOriginsFromGatorPermissions(gatorPermissionsMap); | ||
const mergedConnections = { ...sitesConnectionsList }; | ||
|
||
// Add sites that only have gator permissions but no site connections | ||
gatorSiteOriginsMap.forEach((permissionData, siteOrigin) => { | ||
if (!mergedConnections[siteOrigin]) { | ||
const { addresses, chainId } = permissionData; | ||
const networkName = getNetworkNameByChainId(chainId); | ||
|
||
// Get subject metadata for name and iconUrl | ||
const subjectMetadata = getTargetSubjectMetadata(state, siteOrigin); | ||
const siteName = subjectMetadata?.name || getURLHostName(siteOrigin); | ||
const siteIconUrl = subjectMetadata?.iconUrl || null; | ||
|
||
// Create a minimal connection object for sites that only have gator permissions | ||
mergedConnections[siteOrigin] = { | ||
addresses: addresses || [], | ||
origin: siteOrigin, | ||
name: siteName, | ||
iconUrl: siteIconUrl, | ||
subjectType: SubjectType.Website, | ||
networkIconUrl: '', | ||
networkName: networkName || '', | ||
extensionId: null, | ||
}; | ||
} | ||
}); | ||
|
||
return mergedConnections; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Selector Callback Captures External DependenciesThe |
||
|
||
useEffect(() => { | ||
setTotalConnections(Object.keys(sitesConnectionsList).length); | ||
}, [sitesConnectionsList]); | ||
setTotalConnections(Object.keys(mergedConnectionsList).length); | ||
}, [mergedConnectionsList]); | ||
|
||
const handleConnectionClick = (connection) => { | ||
const hostName = connection.origin; | ||
|
@@ -100,7 +182,7 @@ export const PermissionsPage = () => { | |
<Content padding={0}> | ||
<Box ref={headerRef}></Box> | ||
{totalConnections > 0 ? ( | ||
renderConnectionsList(sitesConnectionsList) | ||
renderConnectionsList(mergedConnectionsList) | ||
) : ( | ||
<Box | ||
data-testid="no-connections" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Undefined Object Access and Type Errors
The
getUniqueSiteOriginsFromGatorPermissions
function accessespermission.permissionResponse.chainId
andpermission.permissionResponse.address
without ensuringpermissionResponse
is defined, which can causeTypeError
s. It also calls.toLowerCase()
on the address without confirming it's a string, potentially leading to further runtime errors.