From 3ffa405ae94c6133c77ffd77eb4ff29377430d34 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Fri, 5 Dec 2025 16:24:36 -0300 Subject: [PATCH] fixed onboarding upload to be linked to the task id --- .../_components/onboarding-upload.tsx | 48 +++++++++++++------ frontend/lib/upload-utils.ts | 3 ++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/frontend/app/onboarding/_components/onboarding-upload.tsx b/frontend/app/onboarding/_components/onboarding-upload.tsx index ce7a0f91..fbb86909 100644 --- a/frontend/app/onboarding/_components/onboarding-upload.tsx +++ b/frontend/app/onboarding/_components/onboarding-upload.tsx @@ -21,6 +21,7 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => { const [isUploading, setIsUploading] = useState(false); const [currentStep, setCurrentStep] = useState(null); const [uploadedFilename, setUploadedFilename] = useState(null); + const [uploadedTaskId, setUploadedTaskId] = useState(null); const [shouldCreateFilter, setShouldCreateFilter] = useState(false); const [isCreatingFilter, setIsCreatingFilter] = useState(false); @@ -43,23 +44,26 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => { // Monitor tasks and call onComplete when file processing is done useEffect(() => { - if (currentStep === null || !tasks) { + if (currentStep === null || !tasks || !uploadedTaskId) { return; } - // Check if there are any active tasks (pending, running, or processing) - const activeTasks = tasks.find( - (task) => - task.status === "pending" || - task.status === "running" || - task.status === "processing", - ); + // Find the task by task ID from the upload response + const matchingTask = tasks.find((task) => task.task_id === uploadedTaskId); - // If no active tasks and we have more than 1 task (initial + new upload), complete it - if ( - (!activeTasks || (activeTasks.processed_files ?? 0) > 0) && - tasks.length > 1 - ) { + // If no matching task found, wait for it to appear + if (!matchingTask) { + return; + } + + // Check if the matching task is still active (pending, running, or processing) + const isTaskActive = + matchingTask.status === "pending" || + matchingTask.status === "running" || + matchingTask.status === "processing"; + + // If task is completed or has processed files, complete the onboarding step + if (!isTaskActive || (matchingTask.processed_files ?? 0) > 0) { // Set to final step to show "Done" setCurrentStep(STEP_LIST.length); @@ -125,7 +129,17 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => { onComplete(); }, 1000); } - }, [tasks, currentStep, onComplete, refetchNudges, shouldCreateFilter, uploadedFilename]); + }, [ + tasks, + currentStep, + onComplete, + refetchNudges, + shouldCreateFilter, + uploadedFilename, + uploadedTaskId, + createFilterMutation, + isCreatingFilter, + ]); const resetFileInput = () => { if (fileInputRef.current) { @@ -144,6 +158,11 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => { const result = await uploadFile(file, true, true); // Pass createFilter=true console.log("Document upload task started successfully"); + // Store task ID to track the specific upload task + if (result.taskId) { + setUploadedTaskId(result.taskId); + } + // Store filename and createFilter flag in state to create filter after ingestion succeeds if (result.createFilter && result.filename) { setUploadedFilename(result.filename); @@ -176,6 +195,7 @@ const OnboardingUpload = ({ onComplete }: OnboardingUploadProps) => { // Reset on error setCurrentStep(null); + setUploadedTaskId(null); } finally { setIsUploading(false); } diff --git a/frontend/lib/upload-utils.ts b/frontend/lib/upload-utils.ts index 9892bde7..ad09bb3b 100644 --- a/frontend/lib/upload-utils.ts +++ b/frontend/lib/upload-utils.ts @@ -12,6 +12,7 @@ export interface UploadFileResult { raw: unknown; createFilter?: boolean; filename?: string; + taskId?: string; } export async function duplicateCheck( @@ -158,6 +159,7 @@ export async function uploadFile( (uploadIngestJson as { upload?: { id?: string } }).upload?.id || (uploadIngestJson as { id?: string }).id || (uploadIngestJson as { task_id?: string }).task_id; + const taskId = (uploadIngestJson as { task_id?: string }).task_id; const filePath = (uploadIngestJson as { upload?: { path?: string } }).upload?.path || (uploadIngestJson as { path?: string }).path || @@ -197,6 +199,7 @@ export async function uploadFile( raw: uploadIngestJson, createFilter: shouldCreateFilter, filename, + taskId, }; return result;