Skip to content

Commit 6f6d5ce

Browse files
authored
fix: remove initialize token image in provider (#559)
1 parent ec56113 commit 6f6d5ce

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

packages/adena-extension/src/common/provider/wallet/wallet-provider.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const WalletProvider: React.FC<React.PropsWithChildren<unknown>> = ({ chi
3131

3232
const [walletStatus, setWalletStatus] = useRecoilState(WalletState.state);
3333

34-
const [tokenMetainfos, setTokenMetainfos] = useRecoilState(TokenState.tokenMetainfos);
34+
const [tokenMetainfos] = useRecoilState(TokenState.tokenMetainfos);
3535

3636
const [networkMetainfos, setNetworkMetainfos] = useRecoilState(NetworkState.networkMetainfos);
3737

@@ -95,7 +95,6 @@ export const WalletProvider: React.FC<React.PropsWithChildren<unknown>> = ({ chi
9595
if (currentAccount) {
9696
setCurrentAccount(currentAccount);
9797
await accountService.changeCurrentAccount(currentAccount);
98-
await initTokenMetainfos(currentAccount.id);
9998
}
10099
return true;
101100
}
@@ -124,13 +123,6 @@ export const WalletProvider: React.FC<React.PropsWithChildren<unknown>> = ({ chi
124123
return true;
125124
}
126125

127-
async function initTokenMetainfos(accountId: string): Promise<void> {
128-
await tokenService.initAccountTokenMetainfos(accountId);
129-
const tokenMetainfos = await tokenService.getTokenMetainfosByAccountId(accountId);
130-
setTokenMetainfos(tokenMetainfos);
131-
balanceService.setTokenMetainfos(tokenMetainfos);
132-
}
133-
134126
async function changeNetwork(networkMetainfo: NetworkMetainfo): Promise<NetworkMetainfo> {
135127
const rpcUrl = networkMetainfo.rpcUrl;
136128
const gnoProvider = new GnoProvider(rpcUrl, networkMetainfo.networkId);

packages/adena-extension/src/components/atoms/button/index.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import mixins from '@styles/mixins';
44
import { getTheme } from '@styles/theme';
55

66
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
7-
type XOR<T, U> = T | U extends Record<string, unknown>
8-
? (Without<T, U> & U) | (Without<U, T> & T)
9-
: T | U;
7+
type XOR<T, U> =
8+
T | U extends Record<string, unknown> ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
109

1110
type ButtonHierarchy = 'normal' | 'primary' | 'ghost' | 'dark' | 'danger' | 'custom';
1211

@@ -95,8 +94,17 @@ export type ButtonProps = XOR<
9594
}
9695
>;
9796

98-
export const Button = (props: ButtonProps): JSX.Element => {
99-
return <ButtonWrapper {...props}>{props.children}</ButtonWrapper>;
97+
export const Button = ({
98+
disabled = false,
99+
hierarchy = 'primary',
100+
height = '48px',
101+
...props
102+
}: ButtonProps): JSX.Element => {
103+
return (
104+
<ButtonWrapper disabled={disabled} hierarchy={hierarchy} height={height} {...props}>
105+
{props.children}
106+
</ButtonWrapper>
107+
);
100108
};
101109

102110
const ButtonWrapper = styled.button<ButtonProps>`
@@ -127,9 +135,3 @@ const ButtonWrapper = styled.button<ButtonProps>`
127135
color: ${getTheme('neutral', '_1')};
128136
background-color: ${({ bgColor }): string | undefined => bgColor};
129137
`;
130-
131-
Button.defaultProps = {
132-
disabled: false,
133-
hierarchy: 'primary',
134-
height: '48px',
135-
};

packages/adena-extension/src/hooks/use-token-metainfo.tsx

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type UseTokenMetainfoReturn = {
2525
tokenMetainfos: TokenModel[];
2626
allTokenMetainfos: TokenModel[];
2727
currentTokenMetainfos: TokenModel[];
28+
tokenLogoMap: Record<string, string | null>;
2829
getTokenAmount: (amount: { value: string; denom: string }) => { value: string; denom: string };
2930
initTokenMetainfos: () => Promise<void>;
3031
updateTokenMetainfos: (account: Account, tokenMetainfos: TokenModel[]) => Promise<void>;
@@ -67,7 +68,6 @@ function makeTokenKey(token: TokenModel): string {
6768
export const useTokenMetainfo = (): UseTokenMetainfoReturn => {
6869
const { balanceService, tokenService } = useAdenaContext();
6970
const [tokenMetainfos, setTokenMetainfo] = useRecoilState(TokenState.tokenMetainfos);
70-
const [tokenLogoMap, setTokenLogoMap] = useRecoilState(TokenState.tokenLogoMap);
7171
const { currentAccount } = useCurrentAccount();
7272
const { currentNetwork } = useNetwork();
7373
const { data: grc20Tokens } = useGRC20Tokens();
@@ -105,21 +105,19 @@ export const useTokenMetainfo = (): UseTokenMetainfoReturn => {
105105
}, {});
106106
}, [allTokenMetainfos]);
107107

108+
const tokenLogoMap = useMemo(() => {
109+
return currentTokenMetainfos.reduce<Record<string, string | null>>((accum, current) => {
110+
const key = makeTokenKey(current);
111+
accum[key] = current.image || null;
112+
return accum;
113+
}, {});
114+
}, [currentTokenMetainfos]);
115+
108116
const initTokenMetainfos = async (): Promise<void> => {
109117
if (currentAccount) {
110118
await tokenService.initAccountTokenMetainfos(currentAccount.id);
111119
const tokenMetainfos = await tokenService.getTokenMetainfosByAccountId(currentAccount.id);
112120
setTokenMetainfo([...tokenMetainfos]);
113-
114-
const tokenLogoMap = tokenMetainfos.reduce<Record<string, string | null>>(
115-
(accum, current) => {
116-
const key = makeTokenKey(current);
117-
accum[key] = current.image || null;
118-
return accum;
119-
},
120-
{},
121-
);
122-
setTokenLogoMap(tokenLogoMap);
123121
}
124122
};
125123

@@ -174,20 +172,30 @@ export const useTokenMetainfo = (): UseTokenMetainfoReturn => {
174172
[tokenMetaMap],
175173
);
176174

177-
const getTokenImage = (token: TokenModel): string | null => {
178-
const key = makeTokenKey(token);
179-
return tokenLogoMap[key] || null;
180-
};
175+
const getTokenImage = useCallback(
176+
(token: TokenModel): string | null => {
177+
const key = makeTokenKey(token);
178+
return tokenLogoMap[key] || null;
179+
},
180+
[tokenLogoMap],
181+
);
181182

182-
const getTokenImageByDenom = (denom: string): string | null => {
183-
const key = makeTokenKeyByDenom(denom);
184-
return tokenLogoMap[key] || null;
185-
};
183+
const getTokenImageByDenom = useCallback(
184+
(denom: string): string | null => {
185+
const key = makeTokenKeyByDenom(denom);
186+
console.log(key, tokenLogoMap);
187+
return tokenLogoMap[key] || null;
188+
},
189+
[tokenLogoMap],
190+
);
186191

187-
const getTokenImageByPkgPath = (denom: string): string | null => {
188-
const key = makeTokenKeyByPackagePath(denom);
189-
return tokenLogoMap[key] || null;
190-
};
192+
const getTokenImageByPkgPath = useCallback(
193+
(packagePath: string): string | null => {
194+
const key = makeTokenKeyByPackagePath(packagePath);
195+
return tokenLogoMap[key] || null;
196+
},
197+
[tokenLogoMap],
198+
);
191199

192200
const addTokenMetainfo = async (tokenMetainfo: GRC20TokenModel): Promise<boolean> => {
193201
if (!currentAccount) {
@@ -236,13 +244,14 @@ export const useTokenMetainfo = (): UseTokenMetainfoReturn => {
236244
type: 'grc20',
237245
name,
238246
decimals,
239-
image: '',
247+
image: getTokenImageByPkgPath(path) || '',
240248
display: true,
241249
};
242250
return addTokenMetainfo(tokenMetainfo);
243251
};
244252

245253
return {
254+
tokenLogoMap,
246255
tokenMetainfos,
247256
allTokenMetainfos,
248257
currentTokenMetainfos,

0 commit comments

Comments
 (0)