From fd897ad39c68ead1d2d3d0e9aca7cd066996ec33 Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 29 May 2026 11:22:23 +0800 Subject: [PATCH] refactor(ui): use stable keys for extras target lists Replace array-index keys with data-derived keys: the Add Extra modal gives each TargetEntry a generated id (paths can be empty/duplicate while editing), and ExtraCard keys by the target's unique path. Prevents stale input/render state when rows are added or removed. --- ui/src/pages/ExtrasPage.tsx | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ui/src/pages/ExtrasPage.tsx b/ui/src/pages/ExtrasPage.tsx index 8b5faae4b..9dc5d1426 100644 --- a/ui/src/pages/ExtrasPage.tsx +++ b/ui/src/pages/ExtrasPage.tsx @@ -24,12 +24,21 @@ import { useT } from '../i18n'; // ─── AddExtraModal ──────────────────────────────────────────────────────────── interface TargetEntry { + id: string; // stable React key — paths can be empty/duplicate while editing path: string; mode: string; flatten: boolean; extension: string; } +const newTarget = (): TargetEntry => ({ + id: crypto.randomUUID(), + path: '', + mode: 'merge', + flatten: false, + extension: '', +}); + const MODE_OPTIONS = [ { value: 'merge', label: 'merge', description: 'Per-file symlinks, preserves local files' }, { value: 'copy', label: 'copy', description: 'Copy files to target directory' }, @@ -49,10 +58,10 @@ function AddExtraModal({ const t = useT(); const [name, setName] = useState(''); const [source, setSource] = useState(''); - const [targets, setTargets] = useState([{ path: '', mode: 'merge', flatten: false, extension: '' }]); + const [targets, setTargets] = useState(() => [newTarget()]); const [saving, setSaving] = useState(false); - const addTarget = () => setTargets((prev) => [...prev, { path: '', mode: 'merge', flatten: false, extension: '' }]); + const addTarget = () => setTargets((prev) => [...prev, newTarget()]); const updateTarget = (i: number, field: keyof TargetEntry, value: string | boolean) => { setTargets((prev) => prev.map((t, idx) => (idx === i ? { ...t, [field]: value } : t))); @@ -148,7 +157,7 @@ function AddExtraModal({ {targets.map((tgt, i) => { const fieldLabel = 'block text-xs font-medium text-pencil-light mb-1'; return ( -
+
{/* Path — full width, like Name / Source above */}
@@ -372,8 +381,8 @@ function ExtraCard({
{extra.targets.length > 0 ? ( - extra.targets.map((tgt, ti) => ( -
+ extra.targets.map((tgt) => ( +
{tgt.path}