-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
256 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useMemo } from 'react' | ||
import { ListItemButton, Box, Typography } from '@mui/material' | ||
import Link from 'next/link' | ||
import SafeIcon from '@/components/common/SafeIcon' | ||
import Track from '@/components/common/Track' | ||
import { OPEN_SAFE_LABELS, OVERVIEW_EVENTS } from '@/services/analytics' | ||
import { AppRoutes } from '@/config/routes' | ||
import { useAppSelector } from '@/store' | ||
import { selectChainById } from '@/store/chainsSlice' | ||
import ChainIndicator from '@/components/common/ChainIndicator' | ||
import css from './styles.module.css' | ||
import { selectAllAddressBooks } from '@/store/addressBookSlice' | ||
import { shortenAddress } from '@/utils/formatters' | ||
|
||
type AccountItemProps = { | ||
chainId: string | ||
address: string | ||
threshold?: number | ||
owners?: number | ||
} | ||
|
||
const getSafeHref = (prefix: string, address: string) => ({ | ||
pathname: AppRoutes.home, | ||
query: { safe: `${prefix}:${address}` }, | ||
}) | ||
|
||
const AccountItem = ({ chainId, address, ...rest }: AccountItemProps) => { | ||
const chain = useAppSelector((state) => selectChainById(state, chainId)) | ||
|
||
const href = useMemo(() => { | ||
return chain ? getSafeHref(chain.shortName, address) : '' | ||
}, [chain, address]) | ||
|
||
const name = useAppSelector(selectAllAddressBooks)[chainId]?.[address] | ||
|
||
return ( | ||
<Track {...OVERVIEW_EVENTS.OPEN_SAFE} label={OPEN_SAFE_LABELS.login_page}> | ||
<Link href={href}> | ||
<ListItemButton className={css.listItem}> | ||
<SafeIcon address={address} {...rest} /> | ||
|
||
<Typography variant="body2" component="div"> | ||
{name && ( | ||
<Typography fontWeight="bold" fontSize="inherit"> | ||
{name} | ||
</Typography> | ||
)} | ||
<b>{chain?.shortName}: </b> | ||
<Typography color="text.secondary" fontSize="inherit" component="span"> | ||
{shortenAddress(address)} | ||
</Typography> | ||
</Typography> | ||
|
||
<Box flex={1} /> | ||
|
||
<ChainIndicator chainId={chainId} /> | ||
</ListItemButton> | ||
</Link> | ||
</Track> | ||
) | ||
} | ||
|
||
export default AccountItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Button } from '@mui/material' | ||
import Link from 'next/link' | ||
import { AppRoutes } from '@/config/routes' | ||
import { useCurrentChain } from '@/hooks/useChains' | ||
|
||
const CreateButton = () => { | ||
const currentChain = useCurrentChain() | ||
|
||
return ( | ||
<Link href={{ pathname: AppRoutes.newSafe.create, query: { chain: currentChain?.shortName } }}> | ||
<Button disableElevation size="small" variant="contained"> | ||
Create account | ||
</Button> | ||
</Link> | ||
) | ||
} | ||
|
||
export default CreateButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useMemo, useState } from 'react' | ||
import { Button, Box, Paper, Typography } from '@mui/material' | ||
import madProps from '@/utils/mad-props' | ||
import AccountItem from './AccountItem' | ||
import CreateButton from './CreateButton' | ||
import useAllSafes, { type SafeItems } from './useAllSafes' | ||
|
||
type AccountsListProps = { | ||
safes: SafeItems | ||
} | ||
|
||
const DEFAULT_SHOWN = 5 | ||
const MAX_DEFAULT_SHOWN = 7 | ||
|
||
const AccountsList = ({ safes }: AccountsListProps) => { | ||
const [maxShown, setMaxShown] = useState<number>(DEFAULT_SHOWN) | ||
|
||
const shownSafes = useMemo(() => { | ||
if (safes.length <= MAX_DEFAULT_SHOWN) { | ||
return safes | ||
} | ||
return safes.slice(0, maxShown) | ||
}, [safes, maxShown]) | ||
|
||
const onShowMore = () => { | ||
const pageSize = 100 // DEFAULT_SHOWN | ||
setMaxShown((prev) => prev + pageSize) | ||
} | ||
|
||
return ( | ||
<Box display="flex" justifyContent="center"> | ||
<Box width={600} m={2}> | ||
<Box display="flex" justifyContent="space-between" py={3}> | ||
<Typography variant="h1" fontWeight={700}> | ||
My Safe accounts | ||
<Typography component="span" color="text.secondary" fontSize="inherit" fontWeight="normal"> | ||
{' '} | ||
({safes.length}) | ||
</Typography> | ||
</Typography> | ||
|
||
<CreateButton /> | ||
</Box> | ||
|
||
<Paper sx={{ p: 3, pb: 2 }}> | ||
{shownSafes.map((item) => ( | ||
<AccountItem {...item} key={item.chainId + item.address} /> | ||
))} | ||
|
||
{safes.length > shownSafes.length && ( | ||
<Box display="flex" justifyContent="center"> | ||
<Button onClick={onShowMore}>Show more</Button> | ||
</Box> | ||
)} | ||
</Paper> | ||
</Box> | ||
</Box> | ||
) | ||
} | ||
|
||
const MyAccounts = madProps(AccountsList, { | ||
safes: useAllSafes, | ||
}) | ||
|
||
export default MyAccounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.listItem { | ||
gap: var(--space-2); | ||
border: 1px solid var(--color-border-light); | ||
border-radius: var(--space-1); | ||
margin-bottom: 12px; | ||
padding-top: var(--space-2); | ||
padding-bottom: var(--space-2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { getAllOwnedSafes } from '@safe-global/safe-gateway-typescript-sdk' | ||
import useAsync from '@/hooks/useAsync' | ||
import useWallet from '@/hooks/wallets/useWallet' | ||
|
||
const useAllOwnedSafes = () => { | ||
const { address = '' } = useWallet() || {} | ||
|
||
return useAsync(() => { | ||
if (!address) return | ||
return getAllOwnedSafes(address) | ||
}, [address]) | ||
} | ||
|
||
export default useAllOwnedSafes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useMemo } from 'react' | ||
import uniq from 'lodash/uniq' | ||
import { useAppSelector } from '@/store' | ||
import { selectAllAddedSafes } from '@/store/addedSafesSlice' | ||
import useAllOwnedSafes from './useAllOwnedSafes' | ||
import useChains from '@/hooks/useChains' | ||
import useChainId from '@/hooks/useChainId' | ||
|
||
export type SafeItems = Array<{ | ||
chainId: string | ||
address: string | ||
threshold?: number | ||
owners?: number | ||
}> | ||
|
||
const useAddedSafes = () => { | ||
const allAdded = useAppSelector(selectAllAddedSafes) | ||
return allAdded | ||
} | ||
|
||
const useAllSafes = (): SafeItems => { | ||
const [allOwned = {}] = useAllOwnedSafes() | ||
const allAdded = useAddedSafes() | ||
const { configs } = useChains() | ||
const currentChainId = useChainId() | ||
|
||
return useMemo<SafeItems>(() => { | ||
const chains = uniq([currentChainId].concat(Object.keys(allAdded)).concat(Object.keys(allOwned))) | ||
|
||
return chains.flatMap((chainId) => { | ||
if (!configs.some((item) => item.chainId === chainId)) return [] | ||
const addedOnChain = Object.keys(allAdded[chainId] || {}) | ||
const ownedOnChain = allOwned[chainId] | ||
const uniqueAddresses = uniq(addedOnChain.concat(ownedOnChain)).filter(Boolean) | ||
|
||
return uniqueAddresses.map((address) => ({ | ||
address, | ||
chainId, | ||
threshold: allAdded[chainId]?.[address]?.threshold, | ||
owners: allAdded[chainId]?.[address]?.owners.length, | ||
})) | ||
}) | ||
}, [configs, allAdded, allOwned, currentChainId]) | ||
} | ||
|
||
export default useAllSafes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { NextPage } from 'next' | ||
import Head from 'next/head' | ||
import MyAccounts from '@/components/welcome/MyAccounts' | ||
|
||
const Accounts: NextPage = () => { | ||
return ( | ||
<> | ||
<Head> | ||
<title>{'Safe{Wallet} – My accounts'}</title> | ||
</Head> | ||
|
||
<MyAccounts /> | ||
</> | ||
) | ||
} | ||
|
||
export default Accounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters