From 8325729f69d10f7a1d56a309786fa81e2518d484 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 1 Dec 2025 09:39:31 +0000 Subject: [PATCH] Remove optimistic task on error instead of refreshing all tasks Previously when task creation failed, the code would refresh all tasks from the server to remove the optimistic task. This was inefficient. Now we remove only the specific optimistic task that failed by adding a removeTaskOptimistically function to the TasksContext. Changes: - Add removeTaskOptimistically function to app-layout.tsx - Update TasksContextType interface to include the new function - Use removeTaskOptimistically in home-page-content.tsx error handlers - Remove TODO comments that are now resolved --- components/app-layout.tsx | 6 ++++++ components/home-page-content.tsx | 8 +++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/components/app-layout.tsx b/components/app-layout.tsx index 2dc156a..28c70b1 100644 --- a/components/app-layout.tsx +++ b/components/app-layout.tsx @@ -31,6 +31,7 @@ interface TasksContextType { installDependencies: boolean maxDuration: number }) => { id: string; optimisticTask: Task } + removeTaskOptimistically: (taskId: string) => void } const TasksContext = createContext(undefined) @@ -254,6 +255,10 @@ export function AppLayout({ children, initialSidebarWidth, initialSidebarOpen, i return { id, optimisticTask } } + const removeTaskOptimistically = (taskId: string) => { + setTasks((prevTasks) => prevTasks.filter((task) => task.id !== taskId)) + } + const closeSidebar = () => { updateSidebarOpen(false, false) // Don't save to cookie for mobile backdrop clicks } @@ -303,6 +308,7 @@ export function AppLayout({ children, initialSidebarWidth, initialSidebarOpen, i isSidebarOpen, isSidebarResizing: isResizing, addTaskOptimistically, + removeTaskOptimistically, }} > diff --git a/components/home-page-content.tsx b/components/home-page-content.tsx index 1ed9e0c..3502964 100644 --- a/components/home-page-content.tsx +++ b/components/home-page-content.tsx @@ -46,7 +46,7 @@ export function HomePageContent({ const [loadingGitHub, setLoadingGitHub] = useState(false) const router = useRouter() const searchParams = useSearchParams() - const { refreshTasks, addTaskOptimistically } = useTasks() + const { refreshTasks, addTaskOptimistically, removeTaskOptimistically } = useTasks() const setTaskPrompt = useSetAtom(taskPromptAtom) // Check which auth providers are enabled @@ -236,14 +236,12 @@ export function HomePageContent({ const error = await response.json() // Show detailed message for rate limits, or generic error message toast.error(error.message || error.error || 'Failed to create task') - // TODO: Remove the optimistic task on error - await refreshTasks() // For now, just refresh to remove the optimistic task + removeTaskOptimistically(id) } } catch (error) { console.error('Error creating task:', error) toast.error('Failed to create task') - // TODO: Remove the optimistic task on error - await refreshTasks() // For now, just refresh to remove the optimistic task + removeTaskOptimistically(id) } finally { setIsSubmitting(false) }