Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/files/FileDetailPage.tsx
T
2026-07-08 16:15:37 +02:00

255 lines
12 KiB
TypeScript

// AK-P4a Interim-UI (AD-28) — File detail preview screen composed from ~/ui.
// Presentational: renders the unwrapped FileDetailData; mutations stay server-driven
// (<form method="post"> + intent, exactly like the baseline route action). Modals are
// real ~/ui Dialogs (David standard), not Card fakes. No useLoaderData / server imports.
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherArchive, FeatherDownload, FeatherLink, FeatherRefreshCw, FeatherTrash2, FeatherX } from "@subframe/core";
import type { FileDetailData } from "@hivegrid/view-models";
import { Alert } from "~/ui/components/Alert";
import { Breadcrumbs } from "~/ui/components/Breadcrumbs";
import { Button } from "~/ui/components/Button";
import { Card } from "~/ui/components/Card";
import { Dialog } from "~/ui/components/Dialog";
import { IconButton } from "~/ui/components/IconButton";
import { Select } from "~/ui/components/Select";
import { Tag_Label } from "~/ui/components/Tag_Label";
import { TextInput } from "~/ui/components/TextInput";
export type FileDetailPageProps = {
data: FileDetailData;
/** Allowed polymorphic entity types for the add-link form. */
entityTypes: readonly string[];
/** Href back to the files browser (breadcrumb). */
filesHref: string;
};
function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
const units = ["KB", "MB", "GB"];
let value = bytes / 1024;
let unitIndex = 0;
while (value >= 1024 && unitIndex < units.length - 1) {
value /= 1024;
unitIndex += 1;
}
return `${value.toFixed(1)} ${units[unitIndex]}`;
}
function MetaRow({ label, value }: { label: string; value: string }) {
return (
<div className="flex flex-col gap-1">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{label}</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{value}</span>
</div>
);
}
export function FileDetailPage({ data, entityTypes, filesHref }: FileDetailPageProps) {
const { t, i18n } = useTranslation();
const [addLinkOpen, setAddLinkOpen] = useState(false);
const [deleteOpen, setDeleteOpen] = useState(false);
const dateFormatter = new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium" });
return (
<div className="flex w-full max-w-[960px] flex-col gap-6 p-6" data-ui-id="ui.files.detail">
<Breadcrumbs>
<a href={filesHref} className="no-underline">
<Breadcrumbs.Item>{t("files.nav.title")}</Breadcrumbs.Item>
</a>
<Breadcrumbs.Divider />
<Breadcrumbs.Item active>{data.displayName}</Breadcrumbs.Item>
</Breadcrumbs>
{/* Header + action bar */}
<div className="flex flex-wrap items-start justify-between gap-4">
<div className="flex flex-col gap-1">
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{data.displayName}</span>
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">
{data.mimeType} · {dateFormatter.format(new Date(data.createdAtIso))}
</span>
</div>
<div className="flex flex-wrap items-center gap-2">
<a href={data.downloadUrl} className="no-underline" data-ui-id="ui.files.detail.download">
<Button variant="tertiary" icon={<FeatherDownload />}>
{t("files.detail.actions.download")}
</Button>
</a>
{data.canWrite && data.canRegeneratePreview ? (
<form method="post">
<Button type="submit" name="intent" value="regeneratePreview" variant="tertiary" icon={<FeatherRefreshCw />}>
{t("files.detail.actions.regeneratePreview")}
</Button>
</form>
) : null}
{data.canWrite ? (
<form method="post">
<Button type="submit" name="intent" value="archive" variant="tertiary" icon={<FeatherArchive />} data-ui-id="ui.files.detail.archive">
{t("files.detail.actions.archive")}
</Button>
</form>
) : null}
{data.canDelete ? (
<Button type="button" variant="destructive" icon={<FeatherTrash2 />} onClick={() => setDeleteOpen(true)} data-ui-id="ui.files.detail.delete">
{t("files.detail.actions.delete")}
</Button>
) : null}
</div>
</div>
{data.scanNoticeKey ? (
<div data-ui-id="ui.files.detail.scanNotice">
<Alert variant={data.scanStatus === "infected" ? "error" : "warning"} description={t(data.scanNoticeKey)} notDismissible />
</div>
) : null}
{/* Preview */}
<div data-ui-id="ui.files.detail.preview">
{data.previewUrl ? (
<div className="flex w-full items-center justify-center overflow-hidden rounded-md border border-solid border-border-default bg-surface-neutral-tertiary p-4">
<img src={data.previewUrl} alt={data.displayName} className="max-h-96 w-auto rounded-sm object-contain" />
</div>
) : data.previewFallbackKey ? (
<Alert variant="neutral" description={t(data.previewFallbackKey)} notDismissible />
) : null}
</div>
{/* Metadata */}
<Card>
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{t("files.detail.section.metadata")}</span>
<div className="grid w-full grid-cols-1 gap-4 sm:grid-cols-2">
<MetaRow label={t("files.detail.meta.type")} value={data.mimeType} />
<MetaRow label={t("files.detail.meta.size")} value={formatBytes(data.sizeBytes)} />
<MetaRow label={t("files.detail.meta.created")} value={dateFormatter.format(new Date(data.createdAtIso))} />
<MetaRow label={t("files.detail.meta.source")} value={data.source} />
</div>
{data.notes ? (
<div className="flex flex-col gap-1">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{t("files.detail.section.notes")}</span>
<span className="whitespace-pre-wrap text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{data.notes}</span>
</div>
) : null}
</Card>
{/* Tags */}
{data.tags.length > 0 || data.canWrite ? (
<div className="flex flex-col gap-2" data-ui-id="ui.files.detail.tags">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{t("files.detail.section.tags")}</span>
{data.tags.length > 0 ? (
<div className="flex flex-wrap items-center gap-1">
{data.tags.map((tag) => (
<Tag_Label key={tag} tone="neutral-subtle" size="small" noAction>
{tag}
</Tag_Label>
))}
</div>
) : null}
{data.canWrite ? (
<form method="post" className="flex flex-wrap items-end gap-2" data-ui-id="ui.files.detail.tags.edit">
<input type="hidden" name="intent" value="updateMeta" />
<TextInput label={t("files.detail.tags.editLabel")} showLabel className="min-w-[240px] flex-1">
<TextInput.Input name="tags" defaultValue={data.tags.join(", ")} placeholder={t("files.detail.tags.placeholder")} />
</TextInput>
<Button type="submit" variant="secondary">
{t("files.detail.tags.save")}
</Button>
</form>
) : null}
</div>
) : null}
{/* Linked entities */}
<div className="flex flex-col gap-3" data-ui-id="ui.files.detail.links">
<div className="flex items-center justify-between gap-3">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{t("files.detail.section.links")}</span>
{data.canWrite ? (
<Button type="button" variant="tertiary" size="small" icon={<FeatherLink />} onClick={() => setAddLinkOpen(true)}>
{t("files.detail.actions.link")}
</Button>
) : null}
</div>
{data.linkedEntities.length > 0 ? (
<ul className="flex flex-col gap-2">
{data.linkedEntities.map((link) => (
<li
key={`${link.entityType}-${link.entityId}`}
className="flex items-center justify-between gap-3 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-3 py-2"
data-link-id={`${link.entityType}-${link.entityId}`}
>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{link.entityType}
{link.linkRole ? ` · ${link.linkRole}` : ""}
</span>
{data.canWrite ? (
<form method="post">
<input type="hidden" name="intent" value="unlink" />
<input type="hidden" name="entityType" value={link.entityType} />
<input type="hidden" name="entityId" value={link.entityId} />
<IconButton type="submit" variant="ghost" size="small" icon={<FeatherX />} aria-label={t("files.detail.actions.unlink")} />
</form>
) : null}
</li>
))}
</ul>
) : (
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("files.detail.links.empty")}</span>
)}
</div>
{/* Add-link dialog (real ~/ui Dialog, size S) */}
<Dialog open={addLinkOpen} onOpenChange={setAddLinkOpen}>
<Dialog.Content className="w-[560px] max-w-[90vw] p-8">
<form method="post" className="flex w-full flex-col gap-6" data-ui-id="ui.files.detail.linkForm">
<input type="hidden" name="intent" value="link" />
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("files.detail.addLink.title")}</span>
<div className="flex flex-col gap-4">
<Select name="entityType" label={t("files.detail.link.entityType")} showLabel defaultValue={entityTypes[0]}>
{entityTypes.map((type) => (
<Select.Item key={type} value={type}>
{type}
</Select.Item>
))}
</Select>
<TextInput label={t("files.detail.link.entityId")} showLabel>
<TextInput.Input name="entityId" required />
</TextInput>
<TextInput label={t("files.detail.link.role")} showLabel>
<TextInput.Input name="linkRole" />
</TextInput>
</div>
<div className="flex items-center justify-end gap-2">
<Button type="button" variant="tertiary" onClick={() => setAddLinkOpen(false)}>
{t("files.detail.dialog.cancel")}
</Button>
<Button type="submit">{t("files.detail.actions.link")}</Button>
</div>
</form>
</Dialog.Content>
</Dialog>
{/* Delete confirmation dialog (real ~/ui Dialog, size XS) */}
<Dialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<Dialog.Content className="w-[420px] max-w-[90vw] p-8">
<div className="flex w-full flex-col gap-6">
<div className="flex flex-col gap-2">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("files.detail.delete.title")}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t("files.detail.delete.body")}</span>
</div>
<div className="flex items-center justify-end gap-2">
<Button type="button" variant="tertiary" onClick={() => setDeleteOpen(false)}>
{t("files.detail.dialog.cancel")}
</Button>
<form method="post">
<Button type="submit" name="intent" value="delete" variant="destructive" icon={<FeatherTrash2 />}>
{t("files.detail.delete.confirm")}
</Button>
</form>
</div>
</div>
</Dialog.Content>
</Dialog>
</div>
);
}