"use client"; import { Handle, NodeProps, Position } from "@xyflow/react"; import { FileTextIcon, GitBranchIcon, GitCompareIcon, SparklesIcon, CircleStopIcon, TimerIcon, } from "lucide-react"; import type { DialogueNodeData, NpcDialogueNodeType } from "@/lib/npc-dialogue/dialogue"; import { conditionSummary, hasEmptyConditionKey } from "@/lib/npc-dialogue/dialogue"; import { DialogueText } from "@/lib/npc-dialogue/DialogueText"; import { DIALOGUE_NODES } from "./types"; const position: Position = Position.Right; const inputPosition: Position = Position.Left; /** Single reusable canvas node for every dialogue node type. */ export function NpcDialogueNode({ data, type, selected }: NodeProps) { const d = data as DialogueNodeData; const nodeType = type as NpcDialogueNodeType; const meta = DIALOGUE_NODES[nodeType]; // Only end nodes are truly terminal. A choice node keeps its generic source // handle while it has zero choices, otherwise there would be no way to drag // the first choice out of it; once choices exist each row gets its own // handle (litegraph-style per-output wiring). Condition nodes always use // dedicated true/false handles instead of the generic one. const choiceCount = nodeType === "choice" ? (d.choices?.length ?? 0) : 1; const terminal = nodeType === "end"; const genericSource = !terminal && nodeType !== "condition" && !(nodeType === "choice" && choiceCount > 0); return (
{meta.label} {d.isStart && ( start )}
{/* Choice/condition cards must not clip their per-output handles, which sit outside the card edge; other types keep scrollable content. */}
{nodeType === "dialogue" && (
{d.speaker && (

{d.speaker}

)}

{d.body ? ( ) : ( Empty line )}

)} {nodeType === "choice" && (
{d.leadIn && (

)}
    {(d.choices ?? []).map((choice, index) => (
  • {choice.label || "Unlabeled choice"} {choice.once && ( once )} {choice.condition && ( gated )} {/* Per-choice source handle: drag this row's dot to wire exactly this choice (litegraph-style per-output). */}
  • ))} {(d.choices?.length ?? 0) === 0 && (
  • Add a choice below.
  • )}
)} {nodeType === "event" && (
{d.body && (

)} {d.effect ? ( {d.effect.type === "notify" ? "Notify user" : d.effect.type === "emit" ? "Log game event" : d.effect.type === "flag" ? `Set flag "${d.effect.flagKey || "?"}"` : "Reward (unconfigured)"} {d.effect.message ? `: "${d.effect.message}"` : ""} ) : ( !d.body && ( No narration or effect. Leave the effect on “None” for pure narration. ) )}
)} {nodeType === "delay" && (
{d.delaySeconds ? ( Pause {d.delaySeconds}s before continuing. ) : ( Set a pause length. )}
)} {nodeType === "condition" && (
{d.condition && !hasEmptyConditionKey(d.condition) ? ( Flag {conditionSummary(d.condition)} ) : ( Set a flag key to branch on. )}
{/* Dedicated true/false source handles, one per branch. */}
true
false
)} {nodeType === "end" && (
{d.closing ? ( ) : ( No closing line )}
)}
{genericSource && ( )} {!d.isStart && ( )}
); } function NodeIcon({ type }: { type: NpcDialogueNodeType }) { const className = "size-3.5 text-white/50"; switch (type) { case "choice": return ; case "event": return ; case "delay": return ; case "condition": return ; case "end": return ; default: return ; } }