1
0
Fork 0
polaris-task-force/src/components/frontend/personnel/DialogueAttachments.tsx
Z8MB1E a1aaed913d feat(npc-dialogue): attach files to conversation lines
Nodes can carry pointers to media docs (transcripts, sealed orders,
images). Authors upload from the node editor (gated on
npc-dialogues:update); the runtime resolves ids to file URLs server-side
and players open them in a dossier-styled viewer modal with download
and new-tab escape hatches. Deleted media resolve to nothing instead of
dead chips.
2026-10-14 16:05:15 -04:00

111 lines
4.3 KiB
TypeScript

"use client";
import { useState } from "react";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
import {
DownloadIcon,
ExternalLinkIcon,
PaperclipIcon,
XIcon,
} from "lucide-react";
import type { ResolvedAttachment } from "@/lib/npc-dialogue/dialogue";
/**
* Attachment chips for a conversation line. Each chip opens the file in a
* dossier-styled modal (images render directly, everything else through an
* iframe, which gives PDFs and plain text their native browser viewer) with
* download and new-tab escape hatches. Mirrors how DialogueLink presents
* linked pages, so attachments and links feel like one system.
*/
export function DialogueAttachments({
attachments,
}: {
attachments: ResolvedAttachment[] | undefined;
}) {
const [active, setActive] = useState<ResolvedAttachment | null>(null);
if (!attachments || attachments.length === 0) return null;
const isImage = (a: ResolvedAttachment) => a.mimeType.startsWith("image/");
return (
<>
<div className="flex flex-wrap gap-1.5 pt-0.5">
{attachments.map((attachment) => (
<button
key={attachment.url}
type="button"
onClick={() => setActive(attachment)}
title={`Open ${attachment.label}`}
className="inline-flex max-w-full items-center gap-1.5 rounded-none border border-cyan-500/30 bg-cyan-500/10 px-2 py-0.5 font-mono text-[10px] uppercase tracking-widest text-cyan-200 transition-colors hover:border-cyan-400/60 hover:text-cyan-100"
>
<PaperclipIcon className="size-3 shrink-0" />
<span className="truncate">{attachment.label}</span>
</button>
))}
</div>
<Dialog open={active !== null} onOpenChange={(next) => !next && setActive(null)}>
<DialogContent
showCloseButton={false}
className="flex h-[85vh] w-[92vw] max-w-4xl flex-col gap-0 overflow-hidden rounded-none border border-white/20 bg-zinc-950 p-0 text-white"
>
<div className="flex items-center justify-between gap-4 border-b border-white/15 bg-black/50 px-4 py-2 backdrop-blur-sm">
<DialogTitle className="min-w-0 truncate font-mono text-[10px] font-normal uppercase tracking-widest text-white/60">
Attachment // {active?.label}
</DialogTitle>
<div className="flex shrink-0 items-center gap-3">
{active && (
<>
<a
href={active.url}
target="_blank"
rel="noreferrer"
className="flex items-center gap-1 font-mono text-[10px] uppercase tracking-widest text-white/40 transition-colors hover:text-cyan-400"
>
<ExternalLinkIcon className="size-3" />
New tab
</a>
<a
href={active.url}
download={active.filename}
className="flex items-center gap-1 font-mono text-[10px] uppercase tracking-widest text-white/40 transition-colors hover:text-cyan-400"
>
<DownloadIcon className="size-3" />
Download
</a>
</>
)}
<button
type="button"
onClick={() => setActive(null)}
aria-label="Close attachment"
className="text-white/40 transition-colors hover:text-white/90"
>
<XIcon className="size-4" />
</button>
</div>
</div>
{active && isImage(active) ? (
<div className="min-h-0 flex-1 overflow-auto bg-zinc-900 p-4">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={active.url}
alt={active.label}
className="mx-auto max-h-full max-w-full object-contain"
/>
</div>
) : (
active && (
<iframe
src={active.url}
title={active.label}
className="min-h-0 flex-1 bg-zinc-900"
sandbox="allow-scripts allow-same-origin allow-forms allow-popups"
/>
)
)}
</DialogContent>
</Dialog>
</>
);
}