fix(lint): avoid effect self-references in async loaders
This commit is contained in:
@@ -67,7 +67,7 @@ export default function NotificationsPage() {
|
|||||||
const [loadingError, setLoadingError] = useState<string | null>(null)
|
const [loadingError, setLoadingError] = useState<string | null>(null)
|
||||||
const [markingAll, setMarkingAll] = useState(false)
|
const [markingAll, setMarkingAll] = useState(false)
|
||||||
|
|
||||||
const loadNotifications = useCallback(async () => {
|
const loadNotifications = useCallback(async function loadNotifications() {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setLoadingError(null)
|
setLoadingError(null)
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ export default function NotificationsPage() {
|
|||||||
}
|
}
|
||||||
}, [loadNotifications])
|
}, [loadNotifications])
|
||||||
|
|
||||||
const markAllAsRead = useCallback(async () => {
|
const markAllAsRead = useCallback(async function markAllAsRead() {
|
||||||
if (markAllPendingRef.current) return
|
if (markAllPendingRef.current) return
|
||||||
|
|
||||||
markAllPendingRef.current = true
|
markAllPendingRef.current = true
|
||||||
|
|||||||
@@ -59,9 +59,12 @@ export default function WalletPage() {
|
|||||||
})
|
})
|
||||||
}, [isConsumer, transactions])
|
}, [isConsumer, transactions])
|
||||||
|
|
||||||
const loadWalletData = useCallback(async (options?: { showToast?: boolean }) => {
|
const loadWalletData = useCallback(
|
||||||
|
async (options?: { showToast?: boolean; silent?: boolean }) => {
|
||||||
|
if (!options?.silent) {
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
setLoadError(null)
|
setLoadError(null)
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await requestWithAuth(async () => {
|
const res = await requestWithAuth(async () => {
|
||||||
@@ -84,11 +87,40 @@ export default function WalletPage() {
|
|||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
}, [])
|
},
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void loadWalletData()
|
let cancelled = false
|
||||||
}, [loadWalletData])
|
|
||||||
|
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 handleTopUp = (rawAmount?: number) => {
|
||||||
const amount = rawAmount ?? Number(customAmount)
|
const amount = rawAmount ?? Number(customAmount)
|
||||||
|
|||||||
@@ -24,9 +24,12 @@ export function PostComments({ postId }: PostCommentsProps) {
|
|||||||
const [pendingLike, setPendingLike] = useState<Record<string, boolean>>({})
|
const [pendingLike, setPendingLike] = useState<Record<string, boolean>>({})
|
||||||
const { requireAuth } = useRequireAuth()
|
const { requireAuth } = useRequireAuth()
|
||||||
|
|
||||||
const refresh = useCallback(async () => {
|
const refresh = useCallback(
|
||||||
|
async (showLoading = true) => {
|
||||||
|
if (showLoading) {
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setLoadError(null)
|
setLoadError(null)
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const items = await listCommentsByPost(postId)
|
const items = await listCommentsByPost(postId)
|
||||||
setComments(items)
|
setComments(items)
|
||||||
@@ -35,11 +38,30 @@ export function PostComments({ postId }: PostCommentsProps) {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}, [postId])
|
},
|
||||||
|
[postId],
|
||||||
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
void refresh()
|
let cancelled = false
|
||||||
}, [refresh])
|
|
||||||
|
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 (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
|
|||||||
Reference in New Issue
Block a user