"use client"; import { useCallback, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useToast } from "@/components/toast-provider"; import { useFileUpload, type UploadFilesResult } from "@/hooks/use-file-upload"; import { FileUploadConflictDialog } from "./file-upload-conflict-dialog"; export type UploadPickerMode = "files" | "folder"; /** * Owns the two hidden file inputs, the conflict dialog, and the result toasts, * so both Files panels get the same upload behavior from one place. * * The picked destination is captured when the picker opens, not when it * resolves, because the active folder can change while the OS dialog is up. */ export function useFileUploadEntryPoints(sessionId: string | null) { const { t } = useTranslation(); const { toast } = useToast(); const filesInputRef = useRef(null); const folderInputRef = useRef(null); const destinationRef = useRef(""); const pickerSessionRef = useRef(sessionId); const [, setPickerOpen] = useState(false); const { uploads, conflicts, uploadFiles, resolveConflicts, cancelConflicts } = useFileUpload(sessionId); const report = useCallback( (result: UploadFilesResult) => { if (result.cancelled) return; if (result.uploaded.length > 0) { toast({ title: t("task:uploadComplete", { count: result.uploaded.length }), description: result.uploaded.length === 1 ? t("task:uploadedTo", { path: result.uploaded[0].path }) : undefined, }); } if (result.failed > 0) { toast({ title: t("task:uploadPartiallyFailed", { count: result.failed }), variant: "error", }); } }, [t, toast], ); const openPicker = useCallback( (mode: UploadPickerMode, destination: string) => { destinationRef.current = destination; pickerSessionRef.current = sessionId; const input = mode === "folder" ? folderInputRef.current : filesInputRef.current; if (!input) return; // Reset first, or picking the same file twice in a row fires no change event. input.value = ""; setPickerOpen(true); input.click(); }, [sessionId], ); const handlePicked = useCallback( async (event: React.ChangeEvent) => { const files = event.target.files; setPickerOpen(false); if (!files || files.length === 0) return; if (pickerSessionRef.current !== sessionId) return; report(await uploadFiles(destinationRef.current, files)); }, [sessionId, uploadFiles, report], ); const handleResolve = useCallback( (choices: Parameters[0]) => void resolveConflicts(choices), [resolveConflicts], ); const elements = ( <> )} onChange={handlePicked} /> ); return { openPicker, elements, uploads }; }