Skip to content

Commit 065da09

Browse files
committed
Fix stale error closure in useTask by tracking a local error flag
1 parent 18e1d8b commit 065da09

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/hooks/use-task.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function useTask(taskId: string) {
1111
const hasFoundTaskRef = useRef(false)
1212

1313
const fetchTask = useCallback(async () => {
14+
let errorOccurred = false
1415
try {
1516
const response = await fetch(`/api/tasks/${taskId}`)
1617
if (response.ok) {
@@ -25,17 +26,20 @@ export function useTask(taskId: string) {
2526
if (attemptCountRef.current >= 3 || hasFoundTaskRef.current) {
2627
setError('Task not found')
2728
setTask(null)
29+
errorOccurred = true
2830
}
2931
// If we haven't hit the attempt threshold yet, keep loading state
3032
} else {
3133
setError('Failed to fetch task')
34+
errorOccurred = true
3235
}
3336
} catch (err) {
3437
console.error('Error fetching task:', err)
3538
setError('Failed to fetch task')
39+
errorOccurred = true
3640
} finally {
3741
// Only stop loading after we've either found the task or exceeded attempt threshold
38-
if (hasFoundTaskRef.current || attemptCountRef.current >= 3) {
42+
if (hasFoundTaskRef.current || attemptCountRef.current >= 3 || errorOccurred) {
3943
setIsLoading(false)
4044
}
4145
}

0 commit comments

Comments
 (0)