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

1931 Part 2: 1710 Cross network favorites #1975

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 0 additions & 3 deletions src/components/ui/menus/DAOSearch/SearchDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { SafeDisplayRow } from '../../../../pages/home/SafeDisplayRow';
import { useFractal } from '../../../../providers/App/AppProvider';
import { useNetworkConfig } from '../../../../providers/NetworkConfig/NetworkConfigProvider';

interface ISearchDisplay {
loading: boolean;
Expand All @@ -23,7 +22,6 @@ export function SearchDisplay({
}: ISearchDisplay) {
const { t } = useTranslation(['common', 'dashboard']);
const { node } = useFractal();
const { addressPrefix } = useNetworkConfig();

const isCurrentSafe = useMemo(
() => !!node && !!node.daoAddress && node.daoAddress === address,
Expand Down Expand Up @@ -88,7 +86,6 @@ export function SearchDisplay({

<SafeDisplayRow
address={address}
network={addressPrefix}
onClick={() => {
onClickView();
if (closeDrawer) closeDrawer();
Expand Down
8 changes: 4 additions & 4 deletions src/components/ui/menus/SafesMenu/SafeMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import { DAO_ROUTES } from '../../../../constants/routes';
import { useGetDAOName } from '../../../../hooks/DAO/useGetDAOName';

export interface SafeMenuItemProps {
network: string;
address: string;
showAddress?: boolean;
onClick?: () => void;
}

export function SafeMenuItem({ network, address }: SafeMenuItemProps) {
const { daoName } = useGetDAOName({ address: getAddress(address) });
export function SafeMenuItem({ address }: SafeMenuItemProps) {
const [networkPrefix, daoAddress] = address.split(':');
const { daoName } = useGetDAOName({ address: getAddress(daoAddress) });
const navigate = useNavigate();

const { t } = useTranslation('dashboard');

const onClickNav = () => {
navigate(DAO_ROUTES.dao.relative(network, address));
navigate(DAO_ROUTES.dao.relative(networkPrefix, daoAddress));
};

return (
Expand Down
3 changes: 0 additions & 3 deletions src/components/ui/menus/SafesMenu/SafesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { Box, MenuList } from '@chakra-ui/react';
import { useTranslation } from 'react-i18next';
import { NEUTRAL_2_82_TRANSPARENT } from '../../../../constants/common';
import { useAccountFavorites } from '../../../../hooks/DAO/loaders/useFavorites';
import { useNetworkConfig } from '../../../../providers/NetworkConfig/NetworkConfigProvider';
import { SafeMenuItem } from './SafeMenuItem';

export function SafesList() {
const { favoritesList } = useAccountFavorites();
const { addressPrefix } = useNetworkConfig();

const { t } = useTranslation('dashboard');
return (
Expand All @@ -31,7 +29,6 @@ export function SafesList() {
{favoritesList.map(favorite => (
<SafeMenuItem
key={favorite}
network={addressPrefix}
address={favorite}
/>
))}
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/DAO/loaders/useFavorites.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useState } from 'react';
import { getAddress } from 'viem';
import { useNetworkConfig } from '../../../providers/NetworkConfig/NetworkConfigProvider';
import { CacheKeys, CacheExpiry } from '../../utils/cache/cacheDefaults';
import { getValue, setValue } from '../../utils/cache/useLocalStorage';

export const useAccountFavorites = () => {
const [favoritesList, setFavoritesList] = useState<string[]>(
getValue({ cacheName: CacheKeys.FAVORITES }),
);
const { addressPrefix } = useNetworkConfig();

const toggleFavorite = (address: string) => {
const normalizedAddress = getAddress(address);
Expand All @@ -16,7 +18,7 @@ export const useAccountFavorites = () => {
if (favorites.includes(normalizedAddress)) {
updatedFavorites = favorites.filter(favorite => favorite !== normalizedAddress);
} else {
updatedFavorites = favorites.concat([normalizedAddress]);
updatedFavorites = favorites.concat([addressPrefix + normalizedAddress]);
}

setValue({ cacheName: CacheKeys.FAVORITES }, updatedFavorites, CacheExpiry.NEVER);
Expand Down
4 changes: 0 additions & 4 deletions src/pages/home/AllSafesDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { useTranslation } from 'react-i18next';
import { BACKGROUND_SEMI_TRANSPARENT } from '../../constants/common';
import { CacheKeys } from '../../hooks/utils/cache/cacheDefaults';
import { getValue } from '../../hooks/utils/cache/useLocalStorage';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { SafeDisplayRow } from './SafeDisplayRow';

interface AllSafesDrawerProps {
Expand All @@ -22,8 +21,6 @@ interface AllSafesDrawerProps {
}

export function AllSafesDrawer({ isOpen, onClose }: AllSafesDrawerProps) {
const { addressPrefix } = useNetworkConfig();

const { t } = useTranslation('home');
const [drawerHeight, setDrawerHeight] = useState('50%');
const [isDragging, setIsDragging] = useState(false);
Expand Down Expand Up @@ -138,7 +135,6 @@ export function AllSafesDrawer({ isOpen, onClose }: AllSafesDrawerProps) {
{getValue({ cacheName: CacheKeys.FAVORITES }).map((favorite: string) => (
<SafeDisplayRow
key={favorite}
network={addressPrefix}
address={favorite}
/>
))}
Expand Down
3 changes: 0 additions & 3 deletions src/pages/home/MySafes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { Box, Button, Flex, Show, Text, useBreakpointValue, useDisclosure } from
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useAccountFavorites } from '../../hooks/DAO/loaders/useFavorites';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { AllSafesDrawer } from './AllSafesDrawer';
import { SafeDisplayRow } from './SafeDisplayRow';

export function MySafes() {
const { t } = useTranslation('home');
const { favoritesList } = useAccountFavorites();
const { addressPrefix } = useNetworkConfig();
const [showAll, setShowAll] = useState(false);
const { isOpen, onOpen, onClose } = useDisclosure();

Expand Down Expand Up @@ -52,7 +50,6 @@ export function MySafes() {
{favoritesToShow.map(favorite => (
<SafeDisplayRow
key={favorite}
network={addressPrefix}
address={favorite}
/>
))}
Expand Down
12 changes: 7 additions & 5 deletions src/pages/home/SafeDisplayRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { useGetDAOName } from '../../hooks/DAO/useGetDAOName';
import useAvatar from '../../hooks/utils/useAvatar';
import useDisplayName, { createAccountSubstring } from '../../hooks/utils/useDisplayName';
import { useNetworkConfig } from '../../providers/NetworkConfig/NetworkConfigProvider';
import { getNetworkIcon } from '../../utils/url';

export function SafeDisplayRow({ address, network, onClick, showAddress }: SafeMenuItemProps) {
export function SafeDisplayRow({ address, onClick, showAddress }: SafeMenuItemProps) {
const [networkPrefix, daoAddress] = address.split(':');
const { daoName } = useGetDAOName({ address: getAddress(address) });
const navigate = useNavigate();

Expand All @@ -21,7 +23,7 @@ export function SafeDisplayRow({ address, network, onClick, showAddress }: SafeM

const onClickNav = () => {
if (onClick) onClick();
navigate(DAO_ROUTES.dao.relative(network, address));
navigate(DAO_ROUTES.dao.relative(networkPrefix, daoAddress));
};

const nameColor = showAddress ? 'neutral-7' : 'white-0';
Expand Down Expand Up @@ -50,7 +52,7 @@ export function SafeDisplayRow({ address, network, onClick, showAddress }: SafeM
>
<Avatar
size="lg"
address={address}
address={daoAddress}
url={avatarURL}
/>
<Flex flexDir="column">
Expand All @@ -60,14 +62,14 @@ export function SafeDisplayRow({ address, network, onClick, showAddress }: SafeM
>
{daoName ?? t('loadingFavorite')}
</Text>
{showAddress && <Text textStyle="button-base">{createAccountSubstring(address)}</Text>}
{showAddress && <Text textStyle="button-base">{createAccountSubstring(daoAddress)}</Text>}
</Flex>

<Spacer />

{/* Network Icon */}
<Flex gap="0.5rem">
<Image src={networkConfig.nativeTokenIcon} />
<Image src={getNetworkIcon(networkPrefix)} />
<Show above="md">
<Text>{networkConfig.chain.name}</Text>
</Show>
Expand Down
33 changes: 33 additions & 0 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,36 @@ export const validateENSName = (ensAddress?: string): boolean => {
return false;
}
};

export const networkPrefix: { [key: number]: string } = {
1: 'eth',
1115511: 'sep',
10: 'oeth',
8453: 'base',
137: 'matic',
84532: 'basesep',
};

export const addNetworkPrefix = (address: string, chainId: number): string => {
return `${networkPrefix[chainId]}:${address}`;
};

export const networkIcons: { [key: number]: string } = {
1: '/images/coin-icon-eth.svg',
1115511: '/images/coin-icon-sep.svg',
10: '/images/coin-icon-op.svg',
8453: '/images/coin-icon-base.svg',
137: '/images/coin-icon-pol.svg',
84532: '/images/coin-icon-base-sep.svg',
};

export const getChainIdFromNetworkPrefix = (_networkPrefix: string): number => {
return Number(
Object.keys(networkPrefix).find(key => networkPrefix[parseInt(key)] === _networkPrefix),
);
};

export const getNetworkIcon = (_networkPrefix: string): string => {
const chainId = getChainIdFromNetworkPrefix(_networkPrefix);
return networkIcons[chainId];
};
Da-Colon marked this conversation as resolved.
Show resolved Hide resolved