79 lines
3.3 KiB
TypeScript
79 lines
3.3 KiB
TypeScript
// P4 Files (AK-P4a) §11 — Upload view with drag&drop + multi-file queue and
|
|
// per-file progress (chunked init -> chunk -> complete). Loader gates on
|
|
// files.write (404 if module off / missing).
|
|
//
|
|
// Interim-UI (AD-28): the client chunked pipeline + queue state stay in this route;
|
|
// the ~/ui preview screen FileUploadPage renders them (VM + items + onFiles as props).
|
|
|
|
import { useCallback, useState } from "react";
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { buildFileUploadVM, type FileUploadVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { loadFilesContext, notFoundResponse, requireFilesPermission } from "~/utils/integrations/fileAccess.server";
|
|
import { uploadFileInChunks, UploadFileClientError } from "~/utils/files/uploadFileClient";
|
|
import { FileUploadPage, type FileUploadDisplayItem } from "~/components/screens/files/FileUploadPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: FileUploadVM; tenantSlug: string };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadFilesContext({ request, params, requireModule: true });
|
|
try {
|
|
requireFilesPermission(ctx, "files.write");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const vm = buildFileUploadVM({ canWrite: true });
|
|
return Response.json({ title: `${t("files.upload.title")} | ${process.env.APP_NAME}`, vm, tenantSlug: params.tenant ?? "" } satisfies LoaderData);
|
|
};
|
|
|
|
type QueueItem = FileUploadDisplayItem;
|
|
|
|
export default function FilesUploadRoute() {
|
|
const { vm, tenantSlug } = useLoaderData<LoaderData>();
|
|
const [items, setItems] = useState<QueueItem[]>([]);
|
|
|
|
const update = useCallback((clientId: string, patch: Partial<QueueItem>) => {
|
|
setItems((current) => current.map((item) => (item.clientId === clientId ? { ...item, ...patch } : item)));
|
|
}, []);
|
|
|
|
const uploadOne = useCallback(
|
|
async (file: File, clientId: string) => {
|
|
try {
|
|
update(clientId, { status: "uploading", progressPercent: 0 });
|
|
await uploadFileInChunks({
|
|
tenantSlug,
|
|
file,
|
|
onProgress: (percent) => update(clientId, { progressPercent: percent }),
|
|
onProcessing: () => update(clientId, { status: "processing" }),
|
|
});
|
|
update(clientId, { status: "done", progressPercent: 100 });
|
|
} catch (error) {
|
|
update(clientId, { status: "error", errorKey: error instanceof UploadFileClientError ? error.errorKey : "files.error.uploadIncomplete" });
|
|
}
|
|
},
|
|
[tenantSlug, update],
|
|
);
|
|
|
|
const onFiles = useCallback(
|
|
(fileList: FileList | null) => {
|
|
if (!fileList) return;
|
|
const next: QueueItem[] = [];
|
|
for (const file of Array.from(fileList)) {
|
|
const clientId = `${file.name}-${file.size}-${file.lastModified}`;
|
|
next.push({ clientId, filename: file.name, sizeBytes: file.size, status: "queued", progressPercent: 0, errorKey: null });
|
|
void uploadOne(file, clientId);
|
|
}
|
|
setItems((current) => [...current, ...next]);
|
|
},
|
|
[uploadOne],
|
|
);
|
|
|
|
return <FileUploadPage vm={vm} items={items} actions={{ onFiles }} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|