Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/files/FileUploadDialog.tsx
T

127 lines
5.5 KiB
TypeScript

// Upload dialog of the files browser. Same chrome as the project quick-action
// upload (shared QuickActionDialog + the same dashed dropzone), so uploading a
// file looks the same wherever it is started. Unlike the project dialog it takes
// several files at once and shows the chunked pipeline's progress per file,
// which is what the standalone upload view offered before.
//
// Deliberate delta from the project dialog: no category/description fields.
// Those describe a file's role *on a project* and have nowhere to go on a file
// that is uploaded into the tenant's library.
//
// Pure/presentational: the queue and the upload pipeline live in the route and
// arrive as props. No useLoaderData/useFetcher, no server imports.
import { useRef } from "react";
import { useTranslation } from "react-i18next";
import { FeatherUpload, FeatherUploadCloud } from "@subframe/core";
import type { FileUploadDisplayItem } from "./FileUploadPage";
import { QuickActionDialog } from "../shared/QuickActionDialog";
import { Button } from "~/ui/components/Button";
import { Progress } from "~/ui/components/Progress";
export type FileUploadDialogProps = {
open: boolean;
onOpenChange: (open: boolean) => void;
/** Queue of the current dialog session, newest selection last. */
items: readonly FileUploadDisplayItem[];
/** Hand the picked/dropped files to the route's chunked upload pipeline. */
onFiles: (files: FileList | null) => void;
/** True while at least one file is still on its way. */
uploading: boolean;
};
function progressState(status: FileUploadDisplayItem["status"]): "uploading" | "success" | "error" {
if (status === "done") return "success";
if (status === "error") return "error";
return "uploading";
}
export function FileUploadDialog(props: FileUploadDialogProps) {
const { t } = useTranslation();
const inputRef = useRef<HTMLInputElement>(null);
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 (
<QuickActionDialog
open={props.open}
onOpenChange={props.onOpenChange}
icon={<FeatherUpload />}
iconVariant="neutral"
title={t("files.upload.title")}
subtitle={t("files.upload.dialogSubtitle")}
submitLabel={t("files.upload.actions.done")}
submitIcon={<FeatherUpload />}
submitting={props.uploading}
// Closing is the only way out while nothing has been picked yet; the
// primary button confirms that the finished uploads are accepted.
submitDisabled={props.items.length === 0}
onSubmit={() => props.onOpenChange(false)}
uiId="ui.files.browser.uploadDialog"
>
<div className="flex w-full flex-col items-start gap-2">
<input
ref={inputRef}
type="file"
multiple
className="sr-only"
aria-label={t("files.upload.actions.select")}
onChange={(event) => {
props.onFiles(event.target.files);
// The same file twice in a row must fire again.
event.target.value = "";
}}
data-ui-id="ui.files.browser.uploadDialog.input"
/>
<div
onDragOver={(event) => event.preventDefault()}
onDrop={(event) => {
event.preventDefault();
props.onFiles(event.dataTransfer.files);
}}
className="flex w-full flex-col items-center justify-center gap-5 rounded-lg border border-dashed border-border-strong bg-surface-neutral-primary px-6 py-8"
data-ui-id="ui.files.browser.uploadDialog.dropzone"
>
<FeatherUploadCloud className="text-subtitle font-subtitle text-icon-neutral-secondary" />
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
{t("files.upload.dropzone.headline")}
</span>
<Button variant="tertiary" onClick={() => inputRef.current?.click()} data-ui-id="ui.files.browser.uploadDialog.select">
{t("files.upload.actions.select")}
</Button>
</div>
</div>
{props.items.length > 0 ? (
<ul className="flex w-full flex-col gap-3" data-ui-id="ui.files.browser.uploadDialog.queue">
{props.items.map((item) => (
<li key={item.clientId} className="flex w-full flex-col gap-1" 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-small-regular-0 font-body-small-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>
) : null}
</QuickActionDialog>
);
}
export default FileUploadDialog;