From 38c4127d9e0eb3fa0a35b3170f535b909d47d081 Mon Sep 17 00:00:00 2001 From: truemiller Date: Thu, 21 Nov 2024 16:56:21 +0000 Subject: [PATCH] feat: refactor SettingsPage to use master wallet context and improve backup address handling --- frontend/components/SettingsPage/index.tsx | 42 ++++++++++++++++------ frontend/hooks/useMultisig.ts | 9 +++-- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/frontend/components/SettingsPage/index.tsx b/frontend/components/SettingsPage/index.tsx index e12725c9..a3c97cf1 100644 --- a/frontend/components/SettingsPage/index.tsx +++ b/frontend/components/SettingsPage/index.tsx @@ -1,5 +1,6 @@ import { CloseOutlined, SettingOutlined } from '@ant-design/icons'; import { Button, Card, Flex, Typography } from 'antd'; +import { isNil } from 'lodash'; import Link from 'next/link'; import { useMemo } from 'react'; @@ -11,7 +12,9 @@ import { SettingsScreen } from '@/enums/SettingsScreen'; import { useMultisig } from '@/hooks/useMultisig'; import { usePageState } from '@/hooks/usePageState'; import { useSettings } from '@/hooks/useSettings'; -import { useWalletContext } from '@/hooks/useWallet'; +import { useMasterWalletContext } from '@/hooks/useWallet'; +import { Address } from '@/types/Address'; +import { Optional } from '@/types/Util'; import { truncateAddress } from '@/utils/truncate'; import { CustomAlert } from '../Alert'; @@ -83,15 +86,34 @@ export const Settings = () => { }; const SettingsMain = () => { - const { wallets } = useWalletContext(); - const { backupSafeAddress } = useMultisig(); + const { masterEoa, masterSafes } = useMasterWalletContext(); + + const { owners } = useMultisig( + masterSafes?.[0], // TODO: all master safes should have the same address, but dirty implementation + ); + const { goto } = usePageState(); - const truncatedBackupSafeAddress: string | undefined = useMemo(() => { - if (backupSafeAddress) { - return truncateAddress(backupSafeAddress); + const masterSafeBackupAddresses = useMemo>(() => { + if (!owners) return; + if (!masterEoa) return; + + // TODO: handle edge cases where there are multiple owners due to middleware failure, or user interaction via safe.global + return owners.filter((owner) => owner !== masterEoa.address); + }, [owners, masterEoa]); + + const masterSafeBackupAddress = useMemo>(() => { + if (isNil(masterSafeBackupAddresses)) return; + if (!masterSafeBackupAddresses?.[0]) return; + + return masterSafeBackupAddresses[0]; + }, [masterSafeBackupAddresses]); + + const truncatedBackupSafeAddress: Optional = useMemo(() => { + if (masterSafeBackupAddress && masterSafeBackupAddress?.length) { + return truncateAddress(masterSafeBackupAddress); } - }, [backupSafeAddress]); + }, [masterSafeBackupAddress]); return ( { {/* Wallet backup */} Backup wallet - {backupSafeAddress ? ( + {masterSafeBackupAddress ? ( {truncatedBackupSafeAddress} {UNICODE_SYMBOLS.EXTERNAL_LINK} diff --git a/frontend/hooks/useMultisig.ts b/frontend/hooks/useMultisig.ts index 3793eb5e..1e6df3a8 100644 --- a/frontend/hooks/useMultisig.ts +++ b/frontend/hooks/useMultisig.ts @@ -15,14 +15,17 @@ import { Address } from '@/types/Address'; * @returns multisig owners * @note extend with further multisig functions as needed */ -export const useMultisig = (safe: Safe) => { +export const useMultisig = (safe?: Safe) => { const { data: owners, isFetched: ownersIsFetched, isPending: ownersIsPending, - } = useQuery({ - queryKey: REACT_QUERY_KEYS.MULTISIG_GET_OWNERS_KEY(safe), + } = useQuery({ + queryKey: safe ? REACT_QUERY_KEYS.MULTISIG_GET_OWNERS_KEY(safe) : [], queryFn: async () => { + if (!safe) { + return null; + } const contract = new Contract( safe.address, GNOSIS_SAFE_ABI,