409 lines
17 KiB
TypeScript
409 lines
17 KiB
TypeScript
// AK-P4a Interim-UI (AD-28) — Files browser preview screen composed from ~/ui.
|
|
// Pure presentational component: renders the server-built FileBrowserVM passed as
|
|
// a prop and reports filter/view changes through injected actions. No useLoaderData,
|
|
// no server imports. The final Subframe optics land as a tracked design-nachzug once
|
|
// David's D-P4 delta ships; this interim screen keeps the VM/loader boundary stable.
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherDownload, FeatherFile, FeatherGrid, FeatherList, FeatherUpload } from "@subframe/core";
|
|
import type { FileBrowserRow, FileBrowserVM, FilePreviewStatus, FileScanStatus, FileViewMode } from "@hivegrid/view-models";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { Checkbox } from "~/ui/components/Checkbox";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { Table } from "~/ui/components/Table";
|
|
import { Tag_Label } from "~/ui/components/Tag_Label";
|
|
import { ToggleGroup } from "~/ui/components/ToggleGroup";
|
|
|
|
export type FilesBrowserQuery = {
|
|
mimeGroup: string | null;
|
|
entityType: string | null;
|
|
tag: string | null;
|
|
assignedToUserId: string | null;
|
|
includeArchived: boolean;
|
|
viewMode: FileViewMode;
|
|
};
|
|
|
|
export type FilesBrowserActions = {
|
|
/** Navigate (GET) to the same route with the given filter/view state. */
|
|
onApply: (query: FilesBrowserQuery) => void;
|
|
/** Toggle a single row in the bulk selection. */
|
|
onToggleSelect: (id: string) => void;
|
|
/** Select or clear all currently-listed rows. */
|
|
onToggleSelectAll: (ids: readonly string[], selectAll: boolean) => void;
|
|
};
|
|
|
|
export type FilesBrowserPageProps = {
|
|
vm: FileBrowserVM;
|
|
uploadHref: string;
|
|
/** Href for the next cursor page, or null on the last page. */
|
|
nextHref: string | null;
|
|
/** Absolute action URL the bulk-ZIP form POSTs to. */
|
|
bulkZipAction: string;
|
|
/** Currently selected row ids (for the bulk-ZIP download). */
|
|
selectedIds: readonly string[];
|
|
actions: FilesBrowserActions;
|
|
};
|
|
|
|
const MIME_ALL = "all";
|
|
// Shared "no filter" sentinel for the entity/tag/assignee Selects (empty string
|
|
// is not a valid Select value, so a non-empty sentinel maps back to null).
|
|
const FILTER_ALL = "__all__";
|
|
|
|
type StatusBadge = { farbe: "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-neutral"; key: string };
|
|
|
|
function previewBadge(status: FilePreviewStatus): StatusBadge {
|
|
switch (status) {
|
|
case "ready":
|
|
return { farbe: "subtle-positive", key: "files.browser.status.preview.ready" };
|
|
case "generating":
|
|
return { farbe: "subtle-warning", key: "files.browser.status.preview.generating" };
|
|
case "failed":
|
|
return { farbe: "subtle-negative", key: "files.browser.status.preview.failed" };
|
|
case "not_supported":
|
|
return { farbe: "subtle-neutral", key: "files.browser.status.preview.notSupported" };
|
|
case "pending":
|
|
default:
|
|
return { farbe: "subtle-warning", key: "files.browser.status.preview.pending" };
|
|
}
|
|
}
|
|
|
|
function scanBadge(status: FileScanStatus): StatusBadge | null {
|
|
if (status === "infected") return { farbe: "subtle-negative", key: "files.browser.status.scan.infected" };
|
|
if (status === "pending") return { farbe: "subtle-warning", key: "files.browser.status.scan.pending" };
|
|
return null;
|
|
}
|
|
|
|
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]}`;
|
|
}
|
|
|
|
export function FilesBrowserPage({ vm, uploadHref, nextHref, bulkZipAction, selectedIds, actions }: FilesBrowserPageProps) {
|
|
const { t, i18n } = useTranslation();
|
|
|
|
// Loading / not-ready: keep the marker so the shell + e2e see a stable surface.
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.files.browser" />;
|
|
}
|
|
|
|
const { rows, viewMode, filter, filterOptions, canWrite } = vm.data;
|
|
const dateFormatter = new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium" });
|
|
|
|
function applyPatch(patch: Partial<FilesBrowserQuery>) {
|
|
actions.onApply({
|
|
mimeGroup: filter.mimeGroup,
|
|
entityType: filter.entityType,
|
|
tag: filter.tags[0] ?? null,
|
|
assignedToUserId: filter.assignedToUserId,
|
|
includeArchived: filter.includeArchived,
|
|
viewMode,
|
|
...patch,
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.files.browser">
|
|
{/* Header: title + subtitle (0px rhythm), view toggle + upload CTA */}
|
|
<div className="flex flex-wrap items-start justify-between gap-4">
|
|
<div className="flex flex-col gap-0">
|
|
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("files.browser.title")}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
|
|
{t("files.browser.subtitle")}
|
|
</span>
|
|
</div>
|
|
<div className="flex items-center gap-3">
|
|
<ToggleGroup
|
|
value={viewMode}
|
|
onValueChange={(value: string) => {
|
|
if (value === "table" || value === "grid") applyPatch({ viewMode: value });
|
|
}}
|
|
>
|
|
<ToggleGroup.Item icon={<FeatherList />} value="table" aria-label={t("files.browser.viewMode.table")}>
|
|
{t("files.browser.viewMode.table")}
|
|
</ToggleGroup.Item>
|
|
<ToggleGroup.Item icon={<FeatherGrid />} value="grid" aria-label={t("files.browser.viewMode.grid")}>
|
|
{t("files.browser.viewMode.grid")}
|
|
</ToggleGroup.Item>
|
|
</ToggleGroup>
|
|
{canWrite ? (
|
|
<a href={uploadHref} className="no-underline" data-ui-id="ui.files.browser.upload">
|
|
<Button icon={<FeatherUpload />}>{t("files.browser.actions.upload")}</Button>
|
|
</a>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Filter bar (server-side, navigation on change) */}
|
|
<div className="flex flex-wrap items-center gap-4" data-ui-id="ui.files.browser.filter">
|
|
<Select
|
|
className="max-w-[240px]"
|
|
label={t("files.browser.filter.mime.label")}
|
|
showLabel
|
|
value={filter.mimeGroup ?? MIME_ALL}
|
|
onValueChange={(value: string) => applyPatch({ mimeGroup: value === MIME_ALL ? null : value })}
|
|
>
|
|
<Select.Item value={MIME_ALL}>{t("files.browser.filter.mime.all")}</Select.Item>
|
|
<Select.Item value="image">{t("files.browser.filter.mime.image")}</Select.Item>
|
|
<Select.Item value="pdf">{t("files.browser.filter.mime.pdf")}</Select.Item>
|
|
<Select.Item value="text">{t("files.browser.filter.mime.text")}</Select.Item>
|
|
</Select>
|
|
{filterOptions.entityTypes.length > 0 ? (
|
|
<Select
|
|
className="max-w-[240px]"
|
|
label={t("files.browser.filter.entity.label")}
|
|
showLabel
|
|
value={filter.entityType ?? FILTER_ALL}
|
|
onValueChange={(value: string) => applyPatch({ entityType: value === FILTER_ALL ? null : value })}
|
|
>
|
|
<Select.Item value={FILTER_ALL}>{t("files.browser.filter.entity.all")}</Select.Item>
|
|
{filterOptions.entityTypes.map((type) => (
|
|
<Select.Item key={type} value={type}>
|
|
{t(`files.entityType.${type}`, { defaultValue: type })}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
) : null}
|
|
{filterOptions.tags.length > 0 ? (
|
|
<Select
|
|
className="max-w-[240px]"
|
|
label={t("files.browser.filter.tag.label")}
|
|
showLabel
|
|
value={filter.tags[0] ?? FILTER_ALL}
|
|
onValueChange={(value: string) => applyPatch({ tag: value === FILTER_ALL ? null : value })}
|
|
>
|
|
<Select.Item value={FILTER_ALL}>{t("files.browser.filter.tag.all")}</Select.Item>
|
|
{filterOptions.tags.map((tag) => (
|
|
<Select.Item key={tag} value={tag}>
|
|
{tag}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
) : null}
|
|
{filterOptions.assignees.length > 0 ? (
|
|
<Select
|
|
className="max-w-[240px]"
|
|
label={t("files.browser.filter.assignee.label")}
|
|
showLabel
|
|
value={filter.assignedToUserId ?? FILTER_ALL}
|
|
onValueChange={(value: string) => applyPatch({ assignedToUserId: value === FILTER_ALL ? null : value })}
|
|
>
|
|
<Select.Item value={FILTER_ALL}>{t("files.browser.filter.assignee.all")}</Select.Item>
|
|
{filterOptions.assignees.map((assignee) => (
|
|
<Select.Item key={assignee.id} value={assignee.id}>
|
|
{assignee.name}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
) : null}
|
|
<Checkbox
|
|
label={t("files.browser.filter.archived")}
|
|
checked={filter.includeArchived}
|
|
onCheckedChange={(checked: boolean) => applyPatch({ includeArchived: checked })}
|
|
/>
|
|
</div>
|
|
|
|
{rows.length > 0 && viewMode === "table" && selectedIds.length > 0 ? (
|
|
<form
|
|
method="post"
|
|
action={bulkZipAction}
|
|
className="flex flex-wrap items-center gap-3 rounded-md border border-solid border-border-default bg-surface-neutral-secondary px-4 py-3"
|
|
data-ui-id="ui.files.browser.bulkbar"
|
|
>
|
|
{selectedIds.map((id) => (
|
|
<input key={id} type="hidden" name="fileIds" value={id} />
|
|
))}
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
|
|
{t("files.browser.bulk.selected", { count: selectedIds.length })}
|
|
</span>
|
|
<Button type="submit" variant="secondary" icon={<FeatherDownload />}>
|
|
{t("files.browser.bulk.downloadZip")}
|
|
</Button>
|
|
</form>
|
|
) : null}
|
|
|
|
{rows.length === 0 ? (
|
|
<EmptyBrowser canWrite={canWrite} uploadHref={uploadHref} vm={vm} />
|
|
) : viewMode === "grid" ? (
|
|
<GridView rows={rows} />
|
|
) : (
|
|
<TableView
|
|
rows={rows}
|
|
dateFormatter={dateFormatter}
|
|
selectedIds={selectedIds}
|
|
onToggleSelect={actions.onToggleSelect}
|
|
onToggleSelectAll={actions.onToggleSelectAll}
|
|
/>
|
|
)}
|
|
|
|
{nextHref ? (
|
|
<div className="flex justify-center pt-2" data-ui-id="ui.files.browser.pagination">
|
|
<a href={nextHref} className="no-underline">
|
|
<Button variant="tertiary">{t("files.browser.pagination.next")}</Button>
|
|
</a>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function StatusBadges({ row }: { row: FileBrowserRow }) {
|
|
const { t } = useTranslation();
|
|
const preview = previewBadge(row.previewStatus);
|
|
const scan = scanBadge(row.scanStatus);
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-1">
|
|
{scan ? (
|
|
<BadgeFeedback farbe={scan.farbe} size="small">
|
|
{t(scan.key)}
|
|
</BadgeFeedback>
|
|
) : null}
|
|
<BadgeFeedback farbe={preview.farbe} size="small">
|
|
{t(preview.key)}
|
|
</BadgeFeedback>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function RowTags({ tags }: { tags: readonly string[] }) {
|
|
if (tags.length === 0) return null;
|
|
return (
|
|
<div className="flex flex-wrap items-center gap-1">
|
|
{tags.map((tag) => (
|
|
<Tag_Label key={tag} tone="neutral-subtle" size="small" noAction>
|
|
{tag}
|
|
</Tag_Label>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function TableView({
|
|
rows,
|
|
dateFormatter,
|
|
selectedIds,
|
|
onToggleSelect,
|
|
onToggleSelectAll,
|
|
}: {
|
|
rows: readonly FileBrowserRow[];
|
|
dateFormatter: Intl.DateTimeFormat;
|
|
selectedIds: readonly string[];
|
|
onToggleSelect: (id: string) => void;
|
|
onToggleSelectAll: (ids: readonly string[], selectAll: boolean) => void;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const selectedSet = new Set(selectedIds);
|
|
const allIds = rows.map((row) => row.id);
|
|
const allSelected = allIds.length > 0 && allIds.every((id) => selectedSet.has(id));
|
|
return (
|
|
<div className="w-full overflow-x-auto" data-ui-id="ui.files.browser.table">
|
|
<Table
|
|
header={
|
|
<Table.HeaderRow>
|
|
<Table.HeaderCell>
|
|
<Checkbox
|
|
aria-label={t("files.browser.select.all")}
|
|
checked={allSelected}
|
|
onCheckedChange={(checked: boolean) => onToggleSelectAll(allIds, checked)}
|
|
/>
|
|
</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("files.browser.column.name")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("files.browser.column.type")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("files.browser.column.size")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("files.browser.column.date")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("files.browser.column.status")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("files.browser.column.tags")}</Table.HeaderCell>
|
|
</Table.HeaderRow>
|
|
}
|
|
>
|
|
{rows.map((row) => (
|
|
<Table.Row key={row.id} data-row-id={row.id}>
|
|
<Table.Cell>
|
|
<Checkbox
|
|
aria-label={t("files.browser.select.row")}
|
|
checked={selectedSet.has(row.id)}
|
|
onCheckedChange={() => onToggleSelect(row.id)}
|
|
/>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<a href={row.detailHref} className="text-body-default-medium-0 font-body-default-medium-0 text-text-feedback-link-default">
|
|
{row.displayName}
|
|
</a>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">{row.mimeType}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">{formatBytes(row.sizeBytes)}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
|
|
{dateFormatter.format(new Date(row.createdAtIso))}
|
|
</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<StatusBadges row={row} />
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<RowTags tags={row.tags} />
|
|
</Table.Cell>
|
|
</Table.Row>
|
|
))}
|
|
</Table>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function GridView({ rows }: { rows: readonly FileBrowserRow[] }) {
|
|
return (
|
|
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4" data-ui-id="ui.files.browser.grid">
|
|
{rows.map((row) => (
|
|
<a key={row.id} href={row.detailHref} className="no-underline" data-row-id={row.id}>
|
|
<Card>
|
|
<div className="flex h-32 w-full items-center justify-center overflow-hidden rounded-md bg-surface-neutral-tertiary">
|
|
{row.thumbnailUrl ? (
|
|
<img src={row.thumbnailUrl} alt={row.displayName} className="h-full w-full object-cover" />
|
|
) : (
|
|
<FeatherFile className="text-heading-1 font-heading-1 text-icon-neutral-tertiary" />
|
|
)}
|
|
</div>
|
|
<div className="flex w-full flex-col gap-2">
|
|
<span className="line-clamp-1 text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{row.displayName}</span>
|
|
<StatusBadges row={row} />
|
|
<RowTags tags={row.tags} />
|
|
</div>
|
|
</Card>
|
|
</a>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function EmptyBrowser({ canWrite, uploadHref, vm }: { canWrite: boolean; uploadHref: string; vm: Extract<FileBrowserVM, { status: "ready" }> }) {
|
|
const { t } = useTranslation();
|
|
const headlineKey = vm.data.emptyHeadlineKey;
|
|
const bodyKey = vm.data.emptyBodyKey;
|
|
return (
|
|
<div className="flex flex-col items-center gap-4 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-6 py-16 text-center" data-ui-id="ui.files.browser.empty">
|
|
<FeatherFile className="text-heading-1 font-heading-1 text-icon-neutral-tertiary" />
|
|
<div className="flex flex-col gap-1">
|
|
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{t(headlineKey)}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t(bodyKey)}</span>
|
|
</div>
|
|
{canWrite ? (
|
|
<a href={uploadHref} className="no-underline">
|
|
<Button icon={<FeatherUpload />}>{t("files.browser.actions.upload")}</Button>
|
|
</a>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|