/** Accessible HUX-04 artifact UI. This module never fetches or executes content. */ import { Code2, Download, FileText, GitCompare, History, Image as ImageIcon, Pencil, Share2, Sparkles, } from "lucide-react"; import { useMemo, useState } from "react"; import { artifactsEnabled, normalizeArtifactWorkspace, normalizePreview } from "./model.ts"; import type { Artifact, ArtifactAuthorization, ArtifactMutationIntent, ArtifactPreview, ArtifactScope, DiffIntent, HuxIdentity, PromoteIntent, RawArtifactEnvelope, RawPreviewPayload, } from "./types.ts"; interface ArtifactWorkspaceProps { flags?: Iterable; identity: HuxIdentity; scope: ArtifactScope; page: RawArtifactEnvelope; previews?: readonly RawPreviewPayload[]; busy?: boolean; error?: string | null; onLoadPreview?: (intent: ArtifactMutationIntent & {version: number}) => void; onContinueEdit?: (intent: ArtifactMutationIntent) => void; onDiff?: (intent: DiffIntent) => void; onAuthorize?: (action: "download" | "share", intent: ArtifactMutationIntent) => void; onDownload?: (intent: ArtifactMutationIntent & {version: number}) => void; onShare?: (intent: ArtifactMutationIntent & {version: number}) => void; onPromote?: (intent: PromoteIntent) => void; } function intent(artifact: Artifact, scope: ArtifactScope): ArtifactMutationIntent { return {artifactId: artifact.id, expectedVersion: artifact.currentVersion, scope}; } function RendererIcon({artifact}: {artifact: Artifact}) { if (artifact.renderer === "image") return ; if (artifact.renderer === "code" || artifact.renderer === "data") return ; return ; } function Preview({artifact, preview}: {artifact: Artifact; preview: ArtifactPreview | null}) { if (!preview) return

Preview not loaded. Content stays behind its verified content reference.

; if (preview.renderer === "image") { return
{`Preview
{preview.mime}; local authorized preview
; } if (preview.renderer === "audio") { return
{preview.mime}; local authorized preview
; } const className = `hux-artifact-${preview.renderer}`; return
{preview.renderer === "report" &&

Report preview

}
{preview.text}
; } function AuthorizationAction({action, state, busy, onAuthorize, onAction}: { action: "download" | "share"; state: ArtifactAuthorization["download"]; busy: boolean; onAuthorize: () => void; onAction: () => void; }) { const label = action === "download" ? "Download" : "Share"; const Icon = action === "download" ? Download : Share2; if (state === "authorized") return ; if (state === "requires_approval") return ; return ; } function VersionHistory({artifact, selectedVersion, setSelectedVersion, busy, scope, onDiff}: { artifact: Artifact; selectedVersion: number; setSelectedVersion: (version: number) => void; busy: boolean; scope: ArtifactScope; onDiff?: ArtifactWorkspaceProps["onDiff"]; }) { return

Immutable version history

    {[...artifact.versions].reverse().map((version) =>
  1. {version.version > 1 && onDiff && }
  2. )}
; } /** Render nothing unless all negotiated HUX-04 dependencies are active. */ export function ArtifactWorkspace(props: ArtifactWorkspaceProps) { const enabled = artifactsEnabled(props.flags); const page = useMemo(() => enabled ? normalizeArtifactWorkspace(props.page, props.identity, props.scope) : null, [enabled, props.identity, props.page, props.scope]); const [selectedId, setSelectedId] = useState(null); const [selectedVersion, setSelectedVersion] = useState(null); if (!enabled || !page) return null; const artifact = page.artifacts.find((item) => item.id === selectedId) || page.artifacts[0] || null; const version = artifact ? Math.min(selectedVersion || artifact.currentVersion, artifact.currentVersion) : null; const rawPreview = artifact && version ? props.previews?.find((item) => item.artifact_id === artifact.id && item.version === version) : undefined; const preview = artifact && rawPreview ? normalizePreview(rawPreview, artifact, props.identity, props.scope) : null; const attachments = artifact ? page.attachments.find((item) => item.artifactId === artifact.id) : null; const authorization = artifact ? page.authorizations.find((item) => item.artifactId === artifact.id) : null; function selectArtifact(item: Artifact) { setSelectedId(item.id); setSelectedVersion(item.currentVersion); props.onLoadPreview?.({...intent(item, props.scope), version: item.currentVersion}); } function selectVersion(next: number) { if (!artifact) return; setSelectedVersion(next); props.onLoadPreview?.({...intent(artifact, props.scope), version: next}); } return
{props.error &&

{props.error}

} {page.rejected > 0 &&

Some artifacts were withheld because their owner or workspace binding was invalid.

} {artifact && version ? <>
{artifact.renderer}

{artifact.title}

Version {version} · {artifact.sensitivity} · {artifact.language || artifact.type}

props.onAuthorize?.("download", intent(artifact, props.scope))} onAction={() => props.onDownload?.({...intent(artifact, props.scope), version})} /> props.onAuthorize?.("share", intent(artifact, props.scope))} onAction={() => props.onShare?.({...intent(artifact, props.scope), version})} />
{attachments?.sourceCount || 0}Sources attached
{attachments?.citationCount || 0}Citations attached
:

Select an artifact to inspect it.

}

{props.busy ? "Artifact workspace is updating" : `${page.artifacts.length} artifacts available`}

; }