import type { VMState } from "../index.js"; /** * Files browser view-model (AK-P4a §9). Pure function: the loader builds it once * server-side, the UI only renders. No React, no Prisma, no app imports * (DoD A.3). All texts are i18n keys. * * URLs (`thumbnailUrl`, `detailHref`) ALWAYS point at the auth-proxy route * (AD-23) or internal routes, NEVER at a raw S3 URL — the loader builds them and * passes them in here so the VM stays routing-agnostic + pure. */ export type FilePreviewStatus = "pending" | "generating" | "ready" | "failed" | "not_supported"; export type FileScanStatus = "pending" | "clean" | "infected" | "skipped"; export type FileViewMode = "table" | "grid"; export interface FileBrowserRow { readonly id: string; readonly displayName: string; readonly mimeType: string; readonly sizeBytes: number; readonly createdAtIso: string; readonly tags: readonly string[]; readonly source: string; readonly previewStatus: FilePreviewStatus; readonly scanStatus: FileScanStatus; /** Proxy-route URL of the thumbnail (or null while not yet `ready`). */ readonly thumbnailUrl: string | null; /** Internal detail route (e.g. `/acme/files/`). */ readonly detailHref: string; /** Label of the linked target entity (i18n key) or null. */ readonly linkedEntityLabelKey: string | null; } export interface FileBrowserFilterState { readonly entityType: string | null; readonly mimeGroup: string | null; readonly tags: readonly string[]; readonly assignedToUserId: string | null; readonly includeArchived: boolean; } /** A selectable assignee (Verantwortlicher) filter option. */ export interface FileAssigneeOption { readonly id: string; readonly name: string; } /** * The option lists that populate the browser filter dropdowns. Built once by the * loader (tenant-scoped) and passed in; the VM stays pure. `entityTypes` are raw * enum keys — the UI maps them to i18n labels. */ export interface FileBrowserFilterOptions { readonly entityTypes: readonly string[]; readonly tags: readonly string[]; readonly assignees: readonly FileAssigneeOption[]; } const EMPTY_FILTER_OPTIONS: FileBrowserFilterOptions = { entityTypes: [], tags: [], assignees: [] }; export interface FileBrowserData { readonly viewMode: FileViewMode; readonly rows: readonly FileBrowserRow[]; readonly filter: FileBrowserFilterState; readonly filterOptions: FileBrowserFilterOptions; readonly canWrite: boolean; readonly canDelete: boolean; /** Cursor of the next page (null = last page). */ readonly nextCursor: string | null; readonly emptyHeadlineKey: string; readonly emptyBodyKey: string; } export type FileBrowserVM = VMState; /** * Normalized prop-model the UI implementation renders (M015 Subframe-adapter * contract). The ui-app adapter maps {@link FileBrowserData} -> this model once, * then the ui-subframe / ui-mock renderers stay dumb (hot-swap drill, M017). */ export interface FileBrowserModelRow { readonly id: string; readonly displayName: string; readonly mimeType: string; readonly sizeBytes: number; readonly detailHref: string; readonly thumbnailUrl: string | null; } export interface FileBrowserModel { readonly titleKey: string; readonly emptyHeadlineKey: string; readonly emptyBodyKey: string; readonly canWrite: boolean; readonly rows: readonly FileBrowserModelRow[]; readonly rowCount: number; } export interface BuildFileBrowserVMInput { readonly viewMode?: FileViewMode; readonly rows: readonly FileBrowserRow[]; readonly filter: FileBrowserFilterState; /** Option lists for the filter dropdowns. Defaults to empty when omitted. */ readonly filterOptions?: FileBrowserFilterOptions; readonly canWrite: boolean; readonly canDelete: boolean; readonly nextCursor?: string | null; } export function buildFileBrowserVM(input: BuildFileBrowserVMInput): FileBrowserVM { const viewMode: FileViewMode = input.viewMode ?? "table"; const filterOptions = input.filterOptions ?? EMPTY_FILTER_OPTIONS; if (input.rows.length === 0) { // Empty stays a `ready` state with a plain-text empty state (DoD 23) so the // filter/toggle bar and upload CTA remain visible. return { status: "ready", data: { viewMode, rows: [], filter: input.filter, filterOptions, canWrite: input.canWrite, canDelete: input.canDelete, nextCursor: null, emptyHeadlineKey: "files.browser.empty.headline", emptyBodyKey: "files.browser.empty.body", }, }; } return { status: "ready", data: { viewMode, rows: input.rows, filter: input.filter, filterOptions, canWrite: input.canWrite, canDelete: input.canDelete, nextCursor: input.nextCursor ?? null, emptyHeadlineKey: "files.browser.empty.headline", emptyBodyKey: "files.browser.empty.body", }, }; }