diff --git a/app/(account)/notifications/page.tsx b/app/(account)/notifications/page.tsx index aac9616..5ca6754 100644 --- a/app/(account)/notifications/page.tsx +++ b/app/(account)/notifications/page.tsx @@ -67,7 +67,7 @@ export default function NotificationsPage() { const [loadingError, setLoadingError] = useState(null) const [markingAll, setMarkingAll] = useState(false) - const loadNotifications = useCallback(async () => { + const loadNotifications = useCallback(async function loadNotifications() { setLoading(true) setLoadingError(null) @@ -100,7 +100,7 @@ export default function NotificationsPage() { } }, [loadNotifications]) - const markAllAsRead = useCallback(async () => { + const markAllAsRead = useCallback(async function markAllAsRead() { if (markAllPendingRef.current) return markAllPendingRef.current = true diff --git a/app/(account)/wallet/page.tsx b/app/(account)/wallet/page.tsx index c4240aa..8ac288a 100644 --- a/app/(account)/wallet/page.tsx +++ b/app/(account)/wallet/page.tsx @@ -59,36 +59,68 @@ export default function WalletPage() { }) }, [isConsumer, transactions]) - const loadWalletData = useCallback(async (options?: { showToast?: boolean }) => { - setIsLoading(true) - setLoadError(null) - - try { - const res = await requestWithAuth(async () => { - const [b, items] = await Promise.all([ - getWalletBalance(), - listWalletTransactions({ offset: 0, limit: 1000 }), - ]) - return { balance: b, transactions: items } - }) - if (!res) { - setLoadError("请先登录") - return + const loadWalletData = useCallback( + async (options?: { showToast?: boolean; silent?: boolean }) => { + if (!options?.silent) { + setIsLoading(true) + setLoadError(null) } - setBalance(res.balance) - setTransactions(res.transactions) - if (options?.showToast) notifySuccess("交易记录已刷新") - } catch (error) { - setLoadError(toApiError(error).msg) - } finally { - setIsLoading(false) - } - }, []) + try { + const res = await requestWithAuth(async () => { + const [b, items] = await Promise.all([ + getWalletBalance(), + listWalletTransactions({ offset: 0, limit: 1000 }), + ]) + return { balance: b, transactions: items } + }) + if (!res) { + setLoadError("请先登录") + return + } + + setBalance(res.balance) + setTransactions(res.transactions) + if (options?.showToast) notifySuccess("交易记录已刷新") + } catch (error) { + setLoadError(toApiError(error).msg) + } finally { + setIsLoading(false) + } + }, + [], + ) useEffect(() => { - void loadWalletData() - }, [loadWalletData]) + let cancelled = false + + void (async () => { + try { + const res = await requestWithAuth(async () => { + const [b, items] = await Promise.all([ + getWalletBalance(), + listWalletTransactions({ offset: 0, limit: 100 }), + ]) + return { balance: b, transactions: items } + }) + if (cancelled) return + if (!res) { + setLoadError("请先登录") + return + } + + setBalance(res.balance) + setTransactions(res.transactions) + } catch (error) { + if (cancelled) return + setLoadError(toApiError(error).msg) + } + })() + + return () => { + cancelled = true + } + }, []) const handleTopUp = (rawAmount?: number) => { const amount = rawAmount ?? Number(customAmount) diff --git a/components/post-comments.tsx b/components/post-comments.tsx index 97e6cc8..b4aa5b8 100644 --- a/components/post-comments.tsx +++ b/components/post-comments.tsx @@ -24,22 +24,44 @@ export function PostComments({ postId }: PostCommentsProps) { const [pendingLike, setPendingLike] = useState>({}) const { requireAuth } = useRequireAuth() - const refresh = useCallback(async () => { - setLoading(true) - setLoadError(null) - try { - const items = await listCommentsByPost(postId) - setComments(items) - } catch (err: unknown) { - setLoadError(toApiError(err).msg) - } finally { - setLoading(false) - } - }, [postId]) + const refresh = useCallback( + async (showLoading = true) => { + if (showLoading) { + setLoading(true) + setLoadError(null) + } + try { + const items = await listCommentsByPost(postId) + setComments(items) + } catch (err: unknown) { + setLoadError(toApiError(err).msg) + } finally { + setLoading(false) + } + }, + [postId], + ) useEffect(() => { - void refresh() - }, [refresh]) + let cancelled = false + + void (async () => { + try { + const items = await listCommentsByPost(postId) + if (cancelled) return + setComments(items) + } catch (err: unknown) { + if (cancelled) return + setLoadError(toApiError(err).msg) + } finally { + if (!cancelled) setLoading(false) + } + })() + + return () => { + cancelled = true + } + }, [postId]) return (