"use client"; import { useEffect, useState } from "react"; import { XIcon } from "lucide-react"; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import type { DialogueCondition, DialogueFlagOp, DialogueNext, DialogueNode, DialogueNodeData, NpcDialogueEffect, NpcDialogueNodeType, } from "@/lib/npc-dialogue/dialogue"; import { EFFECT_TYPES } from "@/lib/npc-dialogue/dialogue"; interface NodeEditDialogProps { readonly node: DialogueNode | null; /** Every node in the flow, used to build the "goes to" target lists. */ readonly allNodes: DialogueNode[]; readonly open: boolean; readonly onOpenChange: (open: boolean) => void; readonly onSave: (nodeId: string, data: DialogueNodeData) => void; } const EFFECT_LABELS: Record = { notify: "Notify user (in-app)", emit: "Log game event", reward: "Reward (unconfigured)", flag: "Set player flag", }; const FLAG_OP_LABELS: Record = { set_true: "Set to true", set_false: "Set to false", set_number: "Set number to", set_text: "Set text to", add: "Add to number", }; const CONDITION_OP_LABELS: Record = { is_true: "is true", is_false: "is false", gte: "is at least", lte: "is at most", gt: "is greater than", lt: "is less than", eq: "equals", }; /** Ops that compare against a number (everything except is_true/is_false). */ const NUMERIC_OPS: DialogueCondition["op"][] = ["gte", "lte", "gt", "lt", "eq"]; /** Short readable label for a node in the target pickers, with a text snippet. */ function nodeLabel(node: DialogueNode): string { const d = node.data; let preview: string; switch (node.type) { case "dialogue": { const speaker = d.speaker?.trim(); const body = d.body?.trim(); preview = speaker && body ? `${speaker}: ${body}` : speaker || body || "Empty line"; break; } case "choice": { const lead = d.leadIn?.trim(); const count = d.choices?.length ?? 0; const tail = `${count} choice${count === 1 ? "" : "s"}`; preview = lead ? `${lead} (${tail})` : tail; break; } case "event": preview = d.body?.trim() || d.effect?.message?.trim() || (d.effect ? EFFECT_LABELS[d.effect.type] : "Narration"); break; case "delay": preview = d.delaySeconds ? `${d.delaySeconds}s pause` : "Pause"; break; case "condition": { const c = d.condition; preview = c ? `flag "${c.key || "?"}" ${CONDITION_OP_LABELS[c.op]}` : "Condition"; break; } default: preview = d.closing?.trim() || "Conversation ends"; } const trimmed = preview.length > 48 ? `${preview.slice(0, 48)}…` : preview; return `${node.type} · ${trimmed}`; } /** Inline editor for a single dialogue node's data. */ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: NodeEditDialogProps) { const [draft, setDraft] = useState(null); useEffect(() => { setDraft(node?.data ?? null); }, [node?.id, open]); if (!node || !draft) return null; const patch = (partial: Partial) => setDraft((prev) => (prev ? { ...prev, ...partial } : prev)); const patchEffect = (partial: Partial) => patch({ effect: { type: "notify", ...draft.effect, ...partial } }); const patchCondition = (partial: Partial) => patch({ condition: { kind: "flag", key: "", op: "is_true", ...draft.condition, ...partial }, }); const setChoice = (index: number, next: DialogueNext) => patch({ choices: (draft.choices ?? []).map((choice, i) => (i === index ? next : choice)) }); const addChoice = () => patch({ choices: [...(draft.choices ?? []), { label: "", target: "" }] }); const removeChoice = (index: number) => patch({ choices: (draft.choices ?? []).filter((_, i) => i !== index) }); /** Toggle/update a choice's visibility gate; null removes the gate. */ const setChoiceCondition = (index: number, partial: Partial | null) => patch({ choices: (draft.choices ?? []).map((c, i) => { if (i !== index) return c; if (partial === null) return { ...c, condition: undefined }; const base: DialogueCondition = c.condition ?? { kind: "flag", key: "", op: "is_true" }; return { ...c, condition: { ...base, ...partial } }; }), }); const targetOptions = allNodes.filter((n) => n.id !== node.id); return ( e.stopPropagation()} > Edit {node.type} onOpenChange(false)} className="p-2 -m-2 hover:bg-zinc-100 dark:hover:bg-zinc-800 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-zinc-400 rounded-full" aria-label="Close" >
{node.type === "dialogue" && (
patch({ speaker: e.target.value || undefined })} placeholder="Defaults to the NPC's name" className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30" />
)} {node.type === "dialogue" && (