import { useState, type ReactNode } from "react"; import { GripVertical, Plus, Trash2 } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; import type { DropdownOption, DropdownSetting, } from "@/types/dropdownSettings"; export interface ManageDropdownOptionsDialogProps { setting: DropdownSetting; children: ReactNode; } function makeEmptyOption(settingCode: string, idx: number): DropdownOption { return { id: `${settingCode}-new-${Date.now()}-${idx}`, value: "", label: "", order: idx + 1, }; } export default function ManageDropdownOptionsDialog({ setting, children, }: ManageDropdownOptionsDialogProps) { const sortedInitial = [...setting.children].sort( (a, b) => (a.order ?? 0) - (b.order ?? 0), ); const [options, setOptions] = useState(sortedInitial); const update = (i: number, patch: Partial) => setOptions((prev) => prev.map((o, idx) => (idx === i ? { ...o, ...patch } : o)), ); const updateMeta = ( i: number, patch: Partial>, ) => setOptions((prev) => prev.map((o, idx) => idx === i ? { ...o, meta: { ...(o.meta ?? {}), ...patch } } : o, ), ); const remove = (i: number) => setOptions((prev) => prev.filter((_, idx) => idx !== i)); const add = () => setOptions((prev) => [ ...prev, makeEmptyOption(setting.code, prev.length), ]); const move = (i: number, dir: -1 | 1) => setOptions((prev) => { const next = [...prev]; const target = i + dir; if (target < 0 || target >= next.length) return prev; const a = next[i] as DropdownOption; const b = next[target] as DropdownOption; next[i] = { ...b, order: i + 1 }; next[target] = { ...a, order: target + 1 }; return next; }); return ( {children} Manage Options · {setting.label} Add, edit, reorder, or remove options for{" "} {setting.code}.

{options.length} option{options.length === 1 ? "" : "s"}

{options.length === 0 ? (
No options yet. Click Add Option to start.
) : (
{options.map((opt, i) => (
update(i, { label: e.target.value })} placeholder="Display label" />
update(i, { value: e.target.value })} placeholder="Stored value" className="font-mono" />
update(i, { note: e.target.value })} placeholder="Helper text" />
updateMeta(i, { badge: e.target.value })} placeholder="—" className="w-20" />
updateMeta(i, { color: e.target.value })} placeholder="#…" className="w-24 font-mono" />
))}
)}
); }