1
0
Fork 0

feat(npc-dialogue): reorder choices in the dialogue editor

Choice rows in the dialogue node editor now have up/down buttons so a
choice can be moved up or down into the desired order, which is the
order shown to the player. The swap lives in a pure reorderChoice
helper (no-op at the edges, input not mutated) with a focused test.
This commit is contained in:
Jason Fraley 2026-10-07 14:01:00 -04:00
parent 9f0f7db612
commit 538b7d4c4c
3 changed files with 84 additions and 1 deletions

View file

@ -1,7 +1,7 @@
"use client";
import { useCallback, useEffect, useRef, useState } from "react";
import { XIcon } from "lucide-react";
import { ChevronDownIcon, ChevronUpIcon, XIcon } from "lucide-react";
import {
Dialog,
@ -26,6 +26,7 @@ import type {
NpcDialogueNodeType,
} from "@/lib/npc-dialogue/dialogue";
import { EFFECT_TYPES } from "@/lib/npc-dialogue/dialogue";
import { reorderChoice } from "@/lib/npc-dialogue/reorder";
interface NodeEditDialogProps {
readonly node: DialogueNode | null;
@ -184,6 +185,13 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
const removeChoice = (index: number) =>
patch({ choices: (draft.choices ?? []).filter((_, i) => i !== index) });
/** Swap a choice with its neighbour so choices can be reordered. No-op at
* the ends, so the buttons can be disabled there. */
const moveChoice = (index: number, direction: -1 | 1) => {
const ordered = reorderChoice(draft.choices ?? [], index, direction);
if (ordered) patch({ choices: ordered });
};
/** Toggle/update a choice's visibility gate; null removes the gate. */
const setChoiceCondition = (index: number, partial: Partial<DialogueCondition> | null) =>
patch({
@ -285,6 +293,30 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
</div>
{(draft.choices ?? []).map((choice, index) => (
<div key={index} className="flex flex-col gap-1.5 border-t border-white/10 pt-2">
<div className="flex items-center justify-end gap-1">
<Button
type="button"
variant="outline"
size="icon"
aria-label="Move choice up"
disabled={index === 0}
onClick={() => moveChoice(index, -1)}
className="h-6 w-6 border-white/20 text-white/70 hover:bg-zinc-800 disabled:opacity-30"
>
<ChevronUpIcon className="size-3.5" />
</Button>
<Button
type="button"
variant="outline"
size="icon"
aria-label="Move choice down"
disabled={index === (draft.choices?.length ?? 0) - 1}
onClick={() => moveChoice(index, 1)}
className="h-6 w-6 border-white/20 text-white/70 hover:bg-zinc-800 disabled:opacity-30"
>
<ChevronDownIcon className="size-3.5" />
</Button>
</div>
<div className="flex flex-col gap-1.5">
<Label className="text-[9px] uppercase tracking-wider text-white/40">Button text</Label>
<Input

View file

@ -0,0 +1,19 @@
import type { DialogueNext } from "./dialogue";
/**
* Return a new choices array with the choice at `index` swapped with its
* neighbour in `direction` (-1 = up, +1 = down). Returns `null` when the
* choice is already at an edge and cannot move, so callers can disable the
* corresponding button. The input array is never mutated.
*/
export function reorderChoice(
choices: DialogueNext[],
index: number,
direction: -1 | 1,
): DialogueNext[] | null {
const target = index + direction;
if (target < 0 || target >= choices.length) return null;
const ordered = [...choices];
[ordered[index], ordered[target]] = [ordered[target], ordered[index]];
return ordered;
}

View file

@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { reorderChoice } from "@/lib/npc-dialogue/reorder";
import type { DialogueNext } from "@/lib/npc-dialogue/dialogue";
/** Build a choice with a unique target so reordering is observable. */
const choice = (label: string): DialogueNext => ({ label, target: `node-${label}` });
describe("reorderChoice (editor choice ordering)", () => {
const choices = [choice("a"), choice("b"), choice("c")];
it("swaps a choice with its neighbour", () => {
expect(reorderChoice(choices, 0, 1)).toEqual([choice("b"), choice("a"), choice("c")]);
// direction -1 from index 1 lands on the same swap
expect(reorderChoice(choices, 1, -1)).toEqual([choice("b"), choice("a"), choice("c")]);
expect(reorderChoice(choices, 2, -1)).toEqual([choice("a"), choice("c"), choice("b")]);
});
it("returns null at the edges so nothing moves", () => {
expect(reorderChoice(choices, 0, -1)).toBeNull();
expect(reorderChoice(choices, 2, 1)).toBeNull();
// A single-item list is an edge in every direction.
expect(reorderChoice([choice("only")], 0, 1)).toBeNull();
expect(reorderChoice([choice("only")], 0, -1)).toBeNull();
});
it("does not mutate the input array", () => {
const moved = reorderChoice(choices, 1, 1);
expect(moved).not.toBe(choices);
expect(choices).toEqual([choice("a"), choice("b"), choice("c")]);
});
});