115 lines
5.1 KiB
TypeScript
115 lines
5.1 KiB
TypeScript
// AK-P4a Interim-UI (AD-28) — Files upload preview screen composed from ~/ui.
|
|
// Presentational: the chunked upload pipeline + queue state live in the route and are
|
|
// injected as `items` + `onFiles`. The dashed dropzone is a custom interactive target
|
|
// (no i18n-able ~/ui dropzone exists — the ~/ui FileUpload component bakes in English
|
|
// copy), everything else (Button, Progress, Alert) is composed from ~/ui.
|
|
|
|
import { useRef } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherUpload } from "@subframe/core";
|
|
import type { FileUploadVM } from "@hivegrid/view-models";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Progress } from "~/ui/components/Progress";
|
|
|
|
export type FileUploadDisplayItem = {
|
|
clientId: string;
|
|
filename: string;
|
|
sizeBytes: number;
|
|
status: "queued" | "uploading" | "processing" | "done" | "error";
|
|
progressPercent: number;
|
|
errorKey: string | null;
|
|
};
|
|
|
|
export type FileUploadActions = {
|
|
onFiles: (files: FileList | null) => void;
|
|
};
|
|
|
|
export type FileUploadPageProps = {
|
|
vm: FileUploadVM;
|
|
items: readonly FileUploadDisplayItem[];
|
|
actions: FileUploadActions;
|
|
};
|
|
|
|
function progressState(status: FileUploadDisplayItem["status"]): "uploading" | "success" | "error" {
|
|
if (status === "done") return "success";
|
|
if (status === "error") return "error";
|
|
return "uploading";
|
|
}
|
|
|
|
export function FileUploadPage({ vm, items, actions }: FileUploadPageProps) {
|
|
const { t } = useTranslation();
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.files.upload" />;
|
|
}
|
|
|
|
const { dropzoneHeadlineKey, dropzoneBodyKey, heicNoticeKey } = vm.data;
|
|
|
|
function statusText(item: FileUploadDisplayItem): string {
|
|
if (item.status === "error") return t(item.errorKey ?? "files.error.uploadIncomplete");
|
|
if (item.status === "done") return t("files.upload.status.done");
|
|
if (item.status === "processing") return t("files.upload.status.processing");
|
|
if (item.status === "queued") return t("files.upload.status.queued");
|
|
return `${item.progressPercent}%`;
|
|
}
|
|
|
|
return (
|
|
<div className="flex w-full max-w-[720px] flex-col gap-6 p-6" data-ui-id="ui.files.upload">
|
|
<div className="flex flex-col gap-0">
|
|
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("files.upload.title")}</span>
|
|
</div>
|
|
|
|
{/* Dropzone (custom interactive target; i18n copy from the VM) */}
|
|
<button
|
|
type="button"
|
|
onClick={() => inputRef.current?.click()}
|
|
onDragOver={(event) => event.preventDefault()}
|
|
onDrop={(event) => {
|
|
event.preventDefault();
|
|
actions.onFiles(event.dataTransfer.files);
|
|
}}
|
|
className="flex w-full flex-col items-center gap-4 rounded-lg border-2 border-dashed border-border-strong bg-surface-neutral-primary px-6 py-16 hover:border-brand-brand-border-solid-hover hover:bg-surface-neutral-secondary"
|
|
data-ui-id="ui.files.upload.dropzone"
|
|
>
|
|
<FeatherUpload className="text-heading-1 font-heading-1 text-icon-neutral-secondary" />
|
|
<div className="flex flex-col items-center gap-1">
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t(dropzoneHeadlineKey)}</span>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">{t(dropzoneBodyKey)}</span>
|
|
</div>
|
|
<Button icon={<FeatherUpload />} variant="tertiary">
|
|
{t("files.upload.actions.select")}
|
|
</Button>
|
|
</button>
|
|
<input ref={inputRef} type="file" multiple className="hidden" onChange={(event) => actions.onFiles(event.target.files)} data-ui-id="ui.files.upload.input" />
|
|
|
|
<Alert variant="information" textSize="small" description={t(heicNoticeKey)} notDismissible />
|
|
|
|
<ul className="flex flex-col gap-4" data-ui-id="ui.files.upload.queue">
|
|
{items.length === 0 ? (
|
|
<li className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("files.upload.queue.empty")}</li>
|
|
) : (
|
|
items.map((item) => (
|
|
<li key={item.clientId} className="flex flex-col gap-2" data-row-id={item.clientId} data-status={item.status}>
|
|
<div className="flex items-center justify-between gap-3">
|
|
<span className="line-clamp-1 text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{item.filename}</span>
|
|
<span
|
|
className={
|
|
item.status === "error"
|
|
? "text-body-small-regular-0 font-body-small-regular-0 text-text-feedback-negative"
|
|
: "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary"
|
|
}
|
|
>
|
|
{statusText(item)}
|
|
</span>
|
|
</div>
|
|
<Progress className="w-full" value={item.progressPercent} state={progressState(item.status)} hideLabel />
|
|
</li>
|
|
))
|
|
)}
|
|
</ul>
|
|
</div>
|
|
);
|
|
}
|