Skip to content
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

[Counterfactual] Disable notifications and message signing #3240

Merged
merged 6 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { selectUndeployedSafes, type UndeployedSafesState } from '@/features/counterfactual/store/undeployedSafesSlice'
import {
Box,
Grid,
Expand All @@ -13,6 +14,9 @@ import {
ListItemText,
CircularProgress,
} from '@mui/material'
import mapValues from 'lodash/mapValues'
import difference from 'lodash/difference'
import pickBy from 'lodash/pickBy'
import { Fragment, useEffect, useMemo, useState } from 'react'
import type { ReactElement } from 'react'
import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'
Expand All @@ -37,6 +41,16 @@ import css from './styles.module.css'

// UI logic

export const _filterUndeployedSafes = (safes: NotifiableSafes, undeployedSafes: UndeployedSafesState) => {
return pickBy(
mapValues(safes, (safeAddresses, chainId) => {
const undeployedAddresses = undeployedSafes[chainId] ? Object.keys(undeployedSafes[chainId]) : []
return difference(safeAddresses, undeployedAddresses)
}),
(safeAddresses) => safeAddresses.length > 0,
)
}

// Convert data structure of added Safes
export const _transformAddedSafes = (addedSafes: AddedSafesState): NotifiableSafes => {
return Object.entries(addedSafes).reduce<NotifiableSafes>((acc, [chainId, addedSafesOnChain]) => {
Expand Down Expand Up @@ -235,6 +249,7 @@ export const _shouldUnregisterDevice = (
export const GlobalPushNotifications = (): ReactElement | null => {
const chains = useChains()
const addedSafes = useAppSelector(selectAllAddedSafes)
const undeployedSafes = useAppSelector(selectUndeployedSafes)
const [isLoading, setIsLoading] = useState(false)

const { dismissPushNotificationBanner } = useDismissPushNotificationsBanner()
Expand Down Expand Up @@ -267,8 +282,9 @@ export const GlobalPushNotifications = (): ReactElement | null => {
// Merged added Safes and `currentNotifiedSafes` (in case subscriptions aren't added)
const notifiableSafes = useMemo(() => {
const safes = _mergeNotifiableSafes(addedSafes, currentNotifiedSafes)
return _sanitizeNotifiableSafes(chains.configs, safes)
}, [chains.configs, addedSafes, currentNotifiedSafes])
const deployedSafes = _filterUndeployedSafes(safes, undeployedSafes)
return _sanitizeNotifiableSafes(chains.configs, deployedSafes)
}, [addedSafes, currentNotifiedSafes, undeployedSafes, chains.configs])

const totalNotifiableSafes = useMemo(() => {
return _getTotalNotifiableSafes(notifiableSafes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ export const PushNotificationsBanner = ({ children }: { children: ReactElement }
const isSafeAdded = !!addedSafesOnChain?.[safeAddress]
const isSafeRegistered = getPreferences(safe.chainId, safeAddress)
const shouldShowBanner = useDebounce(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious - why do we have to debounce here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue was that there were too many popups opening up at the same time which looked messy. This way there is a small delay before this popup opens. Not ideal but a bit better on the UX side.

isNotificationFeatureEnabled && !isPushNotificationBannerDismissed && isSafeAdded && !isSafeRegistered && !!wallet,
isNotificationFeatureEnabled &&
!isPushNotificationBannerDismissed &&
isSafeAdded &&
!isSafeRegistered &&
!!wallet &&
safe.deployed,
BANNER_DELAY,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PredictedSafeProps } from '@safe-global/protocol-kit'
import type { ChainInfo } from '@safe-global/safe-gateway-typescript-sdk'

import {
Expand All @@ -13,6 +14,7 @@ import {
_getSafesToUnregister,
_shouldUnregisterDevice,
_sanitizeNotifiableSafes,
_filterUndeployedSafes,
} from '../GlobalPushNotifications'
import type { AddedSafesState } from '@/store/addedSafesSlice'

Expand Down Expand Up @@ -78,6 +80,31 @@ describe('GlobalPushNotifications', () => {
})
})

describe('filterUndeployedSafes', () => {
it('should remove Safes that are not deployed', () => {
const notifiableSafes = {
'1': ['0x123', '0x456'],
'4': ['0xabc'],
}

const undeployedSafes = {
'1': {
'0x456': {
safeAccountConfig: {},
safeDeploymentConfig: {},
} as PredictedSafeProps,
},
}

const expected = {
'1': ['0x123'],
'4': ['0xabc'],
}

expect(_filterUndeployedSafes(notifiableSafes, undeployedSafes)).toEqual(expected)
})
})

describe('sanitizeNotifiableSafes', () => {
it('should remove Safes that are not on a supported chain', () => {
const chains = [{ chainId: '1', name: 'Mainnet' }] as unknown as Array<ChainInfo>
Expand Down
2 changes: 1 addition & 1 deletion src/components/settings/PushNotifications/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const PushNotifications = (): ReactElement => {
<FormControlLabel
control={<Switch checked={!!preferences} onChange={handleOnChange} />}
label={preferences ? 'On' : 'Off'}
disabled={!isOk || isRegistering}
disabled={!isOk || isRegistering || !safe.deployed}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in SignMessage we do const isDisabled= which I prefer, but ok :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, with that many variables it makes sense to extract it into a new one for better readability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue is that here we have the isOk variable which comes from the HoC so we can't extract that easily.

/>
)}
</CheckWallet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('SignMessage', () => {
},
chainId: '5',
threshold: 2,
deployed: true,
}

beforeEach(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/tx-flow/flows/SignMessage/SignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const SignMessage = ({ message, safeAppId, requestId }: ProposeProps | ConfirmPr
const decodedMessageAsString = isPlainTextMessage ? decodedMessage : JSON.stringify(decodedMessage, null, 2)
const hasSigned = !!safeMessage?.confirmations.some(({ owner }) => owner.value === wallet?.address)
const isFullySigned = !!safeMessage?.preparedSignature
const isDisabled = !isOwner || hasSigned
const isDisabled = !isOwner || hasSigned || !safe.deployed

const { onSign, submitError } = useSyncSafeMessageSigner(
safeMessage,
Expand Down Expand Up @@ -290,6 +290,8 @@ const SignMessage = ({ message, safeAppId, requestId }: ProposeProps | ConfirmPr
<MessageDialogError isOwner={isOwner} submitError={submitError} />

<RiskConfirmationError />

{!safe.deployed && <ErrorMessage>Your Safe Account is not activated yet.</ErrorMessage>}
</TxCard>
<TxCard>
<CardActions>
Expand Down
2 changes: 1 addition & 1 deletion src/features/counterfactual/store/undeployedSafesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PredictedSafeProps } from '@safe-global/protocol-kit'

type UndeployedSafesSlice = { [address: string]: PredictedSafeProps }

type UndeployedSafesState = { [chainId: string]: UndeployedSafesSlice }
export type UndeployedSafesState = { [chainId: string]: UndeployedSafesSlice }

const initialState: UndeployedSafesState = {}

Expand Down
Loading