-
Notifications
You must be signed in to change notification settings - Fork 184
Fix race on opening task detail page #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,85 @@ | ||
| 'use client' | ||
|
|
||
| import { useState, useEffect, useCallback } from 'react' | ||
| import { useState, useEffect, useCallback, useRef } from 'react' | ||
| import { Task } from '@/lib/db/schema' | ||
|
|
||
| export function useTask(taskId: string) { | ||
| const [task, setTask] = useState<Task | null>(null) | ||
| const [isLoading, setIsLoading] = useState(true) | ||
| const [error, setError] = useState<string | null>(null) | ||
| const attemptCountRef = useRef(0) | ||
| const hasFoundTaskRef = useRef(false) | ||
|
|
||
| const fetchTask = useCallback(async () => { | ||
| let errorOccurred = false | ||
| try { | ||
| const response = await fetch(`/api/tasks/${taskId}`) | ||
| if (response.ok) { | ||
| const data = await response.json() | ||
| setTask(data.task) | ||
| setError(null) | ||
| hasFoundTaskRef.current = true | ||
| } else if (response.status === 404) { | ||
| setError('Task not found') | ||
| setTask(null) | ||
| // Only set error after multiple failed attempts (to handle race condition on task creation) | ||
| // Wait for at least 3 attempts (up to ~6 seconds) before showing "Task not found" | ||
| attemptCountRef.current += 1 | ||
| if (attemptCountRef.current >= 3 || hasFoundTaskRef.current) { | ||
| setError('Task not found') | ||
| setTask(null) | ||
| errorOccurred = true | ||
| } | ||
| // If we haven't hit the attempt threshold yet, keep loading state | ||
| } else { | ||
| setError('Failed to fetch task') | ||
| errorOccurred = true | ||
| } | ||
| } catch (err) { | ||
| console.error('Error fetching task:', err) | ||
| setError('Failed to fetch task') | ||
| errorOccurred = true | ||
| } finally { | ||
| setIsLoading(false) | ||
| // Only stop loading after we've either found the task or exceeded attempt threshold | ||
| if (hasFoundTaskRef.current || attemptCountRef.current >= 3 || errorOccurred) { | ||
| setIsLoading(false) | ||
| } | ||
| } | ||
| }, [taskId]) | ||
|
|
||
| // Initial fetch | ||
| // Initial fetch with retry logic | ||
| useEffect(() => { | ||
| attemptCountRef.current = 0 | ||
| hasFoundTaskRef.current = false | ||
| setIsLoading(true) | ||
| setError(null) | ||
|
|
||
| // Fetch immediately | ||
| fetchTask() | ||
| }, [fetchTask]) | ||
|
|
||
| // Poll for updates every 5 seconds | ||
| // If task isn't found on first try, retry more aggressively initially | ||
| // This handles the race condition where we navigate to the task page before the DB insert completes | ||
| const retryInterval = setInterval(() => { | ||
| if (!hasFoundTaskRef.current && attemptCountRef.current < 3) { | ||
| fetchTask() | ||
| } else { | ||
| clearInterval(retryInterval) | ||
| } | ||
| }, 2000) // Check every 2 seconds for the first few attempts | ||
|
|
||
| return () => clearInterval(retryInterval) | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [taskId]) // fetchTask is intentionally not in deps to avoid recreating interval on every fetchTask change | ||
|
|
||
| // Poll for updates every 5 seconds after initial load | ||
| useEffect(() => { | ||
| const interval = setInterval(() => { | ||
| fetchTask() | ||
| }, 5000) | ||
| // Only start polling after we've found the task or given up | ||
| if (!isLoading) { | ||
| const interval = setInterval(() => { | ||
| fetchTask() | ||
| }, 5000) | ||
|
|
||
| return () => clearInterval(interval) | ||
| }, [fetchTask]) | ||
| return () => clearInterval(interval) | ||
| } | ||
| }, [fetchTask, isLoading]) | ||
|
|
||
| return { task, isLoading, error, refetch: fetchTask } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.