Skip to content
Merged
Changes from all 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
34 changes: 23 additions & 11 deletions miniapps/biobridge/src/hooks/useTokenInfoMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ export function useTokenInfoMap(targets: TokenInfoTarget[]) {
const [loadingMap, setLoadingMap] = useState<Record<string, true>>({})
const tokenInfoRef = useRef(tokenInfoMap)
const loadingRef = useRef(new Set<string>())
const mountedRef = useRef(true)

useEffect(() => {
tokenInfoRef.current = tokenInfoMap
}, [tokenInfoMap])

useEffect(() => {
return () => {
mountedRef.current = false
}
}, [])

const targetKeys = useMemo(() => {
return targets
.map((target) => {
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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])

Expand Down