diff --git a/miniapps/biobridge/src/hooks/useTokenInfoMap.ts b/miniapps/biobridge/src/hooks/useTokenInfoMap.ts index 55d699ced..0a81d1009 100644 --- a/miniapps/biobridge/src/hooks/useTokenInfoMap.ts +++ b/miniapps/biobridge/src/hooks/useTokenInfoMap.ts @@ -16,11 +16,18 @@ export function useTokenInfoMap(targets: TokenInfoTarget[]) { const [loadingMap, setLoadingMap] = useState>({}) const tokenInfoRef = useRef(tokenInfoMap) const loadingRef = useRef(new Set()) + const mountedRef = useRef(true) useEffect(() => { tokenInfoRef.current = tokenInfoMap }, [tokenInfoMap]) + useEffect(() => { + return () => { + mountedRef.current = false + } + }, []) + const targetKeys = useMemo(() => { return targets .map((target) => { @@ -48,6 +55,20 @@ export function useTokenInfoMap(targets: TokenInfoTarget[]) { if (toFetch.length === 0) return + const toFetchKeys = new Set(toFetch.map((entry) => entry.key)) + + const clearLoading = () => { + loadingRef.current = new Set([...loadingRef.current].filter((key) => !toFetchKeys.has(key))) + if (!mountedRef.current) return + setLoadingMap((prev) => { + const next = { ...prev } + toFetch.forEach((entry) => { + delete next[entry.key] + }) + return next + }) + } + toFetch.forEach((entry) => loadingRef.current.add(entry.key)) setLoadingMap((prev) => { const next = { ...prev } @@ -83,21 +104,12 @@ export function useTokenInfoMap(targets: TokenInfoTarget[]) { }) }) .finally(() => { - if (cancelled) return - loadingRef.current = new Set( - [...loadingRef.current].filter((key) => !toFetch.some((entry) => entry.key === key)), - ) - setLoadingMap((prev) => { - const next = { ...prev } - toFetch.forEach((entry) => { - delete next[entry.key] - }) - return next - }) + clearLoading() }) return () => { cancelled = true + clearLoading() } }, [targets, targetKeys])