diff --git a/src/app/(frontend)/personnel/contacts/[id]/actions.ts b/src/app/(frontend)/personnel/contacts/[id]/actions.ts index f1e3727e..793ebf14 100644 --- a/src/app/(frontend)/personnel/contacts/[id]/actions.ts +++ b/src/app/(frontend)/personnel/contacts/[id]/actions.ts @@ -11,6 +11,8 @@ import { collectNodeTokens, resolveNodeTexts } from "@/lib/npc-dialogue/interpol import type { ResolvedNodeTexts } from "@/lib/npc-dialogue/interpolate"; import { applyFlagEffect, evaluateCondition, readFlag, resolveFlagOwner } from "@/lib/npc-dialogue/flags"; import { markNodeRead } from "@/lib/npc-dialogue/states"; +import { resolveAttachments } from "@/lib/npc-dialogue/attachments"; +import { hasPermission } from "@/utils/access-control/hasPermission"; import type { ChoiceGate, DialogueFlagOp, @@ -280,7 +282,13 @@ export async function resolveNpcDialogueTexts( // "done", missing ones render nothing. countdowns[key] = flag.activatesAt ?? (flag.exists ? "done" : ""); } - return { success: true, data: resolveNodeTexts(node, values, countdowns) }; + const resolved = resolveNodeTexts(node, values, countdowns); + // Attachments ride the same round trip: ids become renderable file + // references here, so the client never guesses media URLs and deleted + // files simply drop off instead of rendering dead chips. + const attachments = await resolveAttachments(payload, node.data.attachments); + if (attachments.length > 0) resolved.attachments = attachments; + return { success: true, data: resolved }; } catch (error) { const message = error instanceof Error ? error.message : "Unknown error"; if (message === "Unauthorized") { @@ -315,3 +323,69 @@ export async function markNpcDialogueNodeRead( } } +/** File types authors may attach to a conversation node. */ +const ATTACHMENT_TYPES = new Set([ + "text/plain", + "application/pdf", + "image/png", + "image/jpeg", + "image/webp", + "image/gif", +]); + +/** + * Upload a file to attach to a conversation node (intercept transcripts, + * sealed orders, reference images). The bytes land in `media`; flowData only + * keeps the returned pointer, and resolveNpcDialogueTexts turns it back into + * a URL per read. Gated like dialogue authoring (npc-dialogues:update) since + * this stores files the whole unit will be served. + */ +export async function uploadNpcDialogueAttachment( + formData: FormData, +): Promise> { + try { + const { payload, user } = await authenticate(); + const allowed = await hasPermission(payload, user, "npc-dialogues:update"); + if (!allowed) { + return { success: false, error: "You are not allowed to edit conversations." }; + } + + const file = formData.get("file"); + if (!(file instanceof File)) { + return { success: false, error: "No file was provided." }; + } + if (!ATTACHMENT_TYPES.has(file.type)) { + return { success: false, error: "Attachments must be text, PDF, or image files." }; + } + if (file.size > 8 * 1024 * 1024) { + return { success: false, error: "Attachments must be 8 MB or smaller." }; + } + + const doc = await payload.create({ + collection: "media", + data: { + alt: file.name.replace(/\.[^/.]+$/, "").replace(/[^a-zA-Z0-9 _-]/g, "").trim() || "Attachment", + }, + file: { + data: Buffer.from(await file.arrayBuffer()), + mimetype: file.type, + name: file.name, + size: file.size, + }, + overrideAccess: true, + }); + if (!doc.filename) { + return { success: false, error: "Upload failed: no file was stored." }; + } + return { + success: true, + data: { mediaId: doc.id, filename: doc.filename, mimeType: file.type }, + }; + } catch (error) { + const message = error instanceof Error ? error.message : "Unknown error"; + if (message === "Unauthorized") { + return { success: false, error: "You must be logged in." }; + } + return { success: false, error: message }; + } +} diff --git a/src/components/admin/npc-dialogue/NodeEditDialog.tsx b/src/components/admin/npc-dialogue/NodeEditDialog.tsx index 30084b6d..76471aba 100644 --- a/src/components/admin/npc-dialogue/NodeEditDialog.tsx +++ b/src/components/admin/npc-dialogue/NodeEditDialog.tsx @@ -17,6 +17,7 @@ import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import type { + DialogueAttachment, DialogueCondition, DialogueFlagOp, DialogueNext, @@ -27,6 +28,7 @@ import type { } from "@/lib/npc-dialogue/dialogue"; import { EFFECT_TYPES } from "@/lib/npc-dialogue/dialogue"; import { reorderChoice } from "@/lib/npc-dialogue/reorder"; +import { uploadNpcDialogueAttachment } from "@/app/(frontend)/personnel/contacts/[id]/actions"; interface NodeEditDialogProps { readonly node: DialogueNode | null; @@ -803,6 +805,15 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N )} + {(node.type === "dialogue" || node.type === "event") && ( + + patch({ attachments: attachments.length > 0 ? attachments : undefined }) + } + /> + )} + {(node.type === "dialogue" || node.type === "event" || node.type === "delay") && (