Hooks
useFileUpload
Headless React hook managing concurrent async file uploads, per-file progress, AbortController cancellation, and retries.
Import
import { useFileUpload } from "@/components/ui/hacline/hooks/use-file-upload";Interactive Example
Pair useFileUpload with FileInput via onValueChange={(files) => uploader.syncFiles(files)} to automatically queue new files, abort removed ones, and track per-file progress.
Drop files or load samples to test concurrent uploads
"use client";
import { useMemo } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
createFileInputController,
FileInput,
FileInputDropzone,
FileInputItem,
FileInputItemName,
FileInputItemPreview,
FileInputItemRemove,
FileInputItemSize,
FileInputList,
FileInputTrigger,
} from "@/components/ui/hacline/components/file-input";
import {
Progress,
ProgressIndicator,
ProgressTrack,
} from "@/components/ui/hacline/components/progress";
import { useFileUpload } from "@/components/ui/hacline/hooks/use-file-upload";
const UPLOAD_STEPS = [20, 48, 76, 100] as const;
const STEP_DELAY_MS = 220;
const simulateUpload = (
file: File,
{
onProgress,
signal,
}: { onProgress: (percent: number) => void; signal: AbortSignal }
) =>
// oxlint-disable-next-line promise/avoid-new -- Simulate staged network upload with abort support.
new Promise<{ url: string }>((resolve, reject) => {
let stepIndex = 0;
const handleAbort = () => {
reject(new DOMException("Upload aborted", "AbortError"));
};
if (signal.aborted) {
handleAbort();
return;
}
signal.addEventListener("abort", handleAbort, { once: true });
const tick = () => {
if (signal.aborted) {
return;
}
const next = UPLOAD_STEPS[stepIndex];
stepIndex += 1;
if (next !== undefined) {
onProgress(next);
}
if (stepIndex >= UPLOAD_STEPS.length) {
signal.removeEventListener("abort", handleAbort);
resolve({ url: `/uploads/${file.name}` });
return;
}
setTimeout(tick, STEP_DELAY_MS);
};
setTimeout(tick, STEP_DELAY_MS);
});
const createSampleFiles = () => [
new File(["# Release Notes\nAll checks passed."], "release-notes.md", {
type: "text/markdown",
}),
new File(['{ "name": "hacline" }'], "tokens.json", {
type: "application/json",
}),
];
const statusLabel = (status: string, progress: number) => {
if (status === "uploading") {
return `${progress}%`;
}
if (status === "success") {
return "Uploaded";
}
if (status === "error") {
return "Failed";
}
if (status === "canceled") {
return "Canceled";
}
return "Queued";
};
export const Example = () => {
const controller = useMemo(() => createFileInputController(), []);
const uploader = useFileUpload({
concurrency: 2,
upload: simulateUpload,
});
return (
<div className="grid w-full max-w-md gap-3">
<FileInput
controller={controller}
multiple
onValueChange={(files) => {
uploader.syncFiles(files);
}}
>
<FileInputDropzone variant="compact">
<span className="text-hl-fg-muted text-xs">
Drop files or load samples to test concurrent uploads
</span>
<div className="flex items-center gap-1.5">
<Button
onClick={() => {
controller.addFiles(createSampleFiles());
}}
size="sm"
variant="outline"
>
Add sample files
</Button>
<FileInputTrigger className="min-h-8 px-2.5 text-xs">
Browse
</FileInputTrigger>
</div>
</FileInputDropzone>
<FileInputList aria-label="Upload queue">
<FileInputItem className="flex-col items-stretch gap-2">
{({ file }) => {
const entry = uploader.getEntry(file);
const status = entry?.status ?? "idle";
const progress = entry?.progress ?? 0;
return (
<>
<div className="flex items-center gap-2.5">
<FileInputItemPreview size="sm" />
<div className="min-w-0 flex-1">
<FileInputItemName />
<div className="flex items-center gap-2">
<FileInputItemSize />
<span className="text-hl-fg-muted text-xs">
· {statusLabel(status, progress)}
</span>
</div>
</div>
{entry && status === "uploading" ? (
<Button
onClick={() => uploader.cancel(entry.id)}
size="sm"
variant="ghost"
>
Cancel
</Button>
) : null}
{entry && (status === "error" || status === "canceled") ? (
<Button
onClick={() => uploader.retry(entry.id)}
size="sm"
variant="outline"
>
Retry
</Button>
) : null}
<FileInputItemRemove />
</div>
{status === "uploading" ? (
<Progress
aria-label={`Uploading ${file.name}`}
value={progress}
>
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
) : null}
</>
);
}}
</FileInputItem>
</FileInputList>
</FileInput>
</div>
);
};Options
Prop
Type
Return Value
Prop
Type