[GH-ISSUE #3028] [Issue]: Preset variable option label-edit and move-up/down rebuild from stale props, silently dropping an in-flight option edit #1597

Closed
opened 2026-10-21 05:40:25 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3028

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
In the preset-variable editor (VariableCard), most option mutations build on the latest local copy of the options array (currentOpts()), which holds in-flight edits while a save round-trips. But the option LABEL commit and the move-up/down handler instead rebuild from the props-derived opts, which lags behind any mutation still being saved. So if you edit one option and then commit a label edit (or reorder) before the first edit's PATCH lands, the new array is built from the stale array and the in-flight edit is silently discarded. The same defect wipes a freshly added option if you edit a label before the create refetches.

How to reproduce (try it yourself)

  1. Open a preset -> Sections tab -> expand a Preset Variable with options A and B.
  2. Edit option A's Value field, then immediately Tab/click to option B's Label field and commit (blur/Enter) before A's PATCH round-trips (easy with the 600ms debounce or a slow connection).

You'll see: option A's value reverts to its old value.
Alternatively: click "Add Option" to create C, then immediately edit an existing option's Label and commit before the create refetch lands — C disappears.
Expected: concurrent option edits are preserved; the label/move paths carry the in-flight array.

Where it is
packages/client/src/components/presets/PresetEditor.tsx — option label onCommit at lines 2323-2326 (const next = [...opts]; next[oi] = {...}; updateOpts(next);) and moveOptionByOffset at lines 1962-1966 (reorderItems(opts, ...)). Intended source-of-truth pattern at 1908-1928 (optsRef/sync guard/currentOpts/updateOpts); render maps over props opts at 2231.

What's happening
optsRef.current/currentOpts() is the intended source of truth during in-flight saves (the sync guard only resyncs from props when !onUpdateVariable.isPending). useUpdateVariable has no optimistic onMutate, so props opts stays at the last server-confirmed value until the PATCH resolves. Two mutation paths bypass currentOpts() and read the lagging props array, overwriting the server with a snapshot that misses the concurrent edit.

Suggested fix
Make both bypassing paths read currentOpts() like every other option mutation.

  1. Option label commit (~2323-2327): reuse the existing id-keyed helper, identical to the value field one line below:
onCommit={(v) => updateOptionField(opt.id, "label", v)}

updateOptionField already reads currentOpts() and keys by id, avoiding index drift against a diverged array.
2. moveOptionByOffset (~1962-1966): build from currentOpts(), mirroring the accepted drag path:

const moveOptionByOffset = (optionIdx: number, offset: number) => {
  if (optionOrderIsAlphabetical) return;
  const next = reorderItems(currentOpts(), optionIdx, optionIdx + offset);
  if (next) updateOpts(next);
};

Key the label commit by opt.id (not the render index oi) so it stays correct when currentOpts() has diverged in order/length from props. Leave the read-only/display uses of props opts (2050, 2052, 2105, 2231, 2236-2237, the disabled guard at 2310) unchanged.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3028 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** In the preset-variable editor (`VariableCard`), most option mutations build on the latest local copy of the options array (`currentOpts()`), which holds in-flight edits while a save round-trips. But the option LABEL commit and the move-up/down handler instead rebuild from the props-derived `opts`, which lags behind any mutation still being saved. So if you edit one option and then commit a label edit (or reorder) before the first edit's PATCH lands, the new array is built from the stale array and the in-flight edit is silently discarded. The same defect wipes a freshly added option if you edit a label before the create refetches. **How to reproduce (try it yourself)** 1. Open a preset -> Sections tab -> expand a Preset Variable with options A and B. 2. Edit option A's Value field, then immediately Tab/click to option B's Label field and commit (blur/Enter) before A's PATCH round-trips (easy with the 600ms debounce or a slow connection). You'll see: option A's value reverts to its old value. Alternatively: click "Add Option" to create C, then immediately edit an existing option's Label and commit before the create refetch lands — C disappears. Expected: concurrent option edits are preserved; the label/move paths carry the in-flight array. **Where it is** `packages/client/src/components/presets/PresetEditor.tsx` — option label `onCommit` at lines 2323-2326 (`const next = [...opts]; next[oi] = {...}; updateOpts(next);`) and `moveOptionByOffset` at lines 1962-1966 (`reorderItems(opts, ...)`). Intended source-of-truth pattern at 1908-1928 (`optsRef`/sync guard/`currentOpts`/`updateOpts`); render maps over props `opts` at 2231. **What's happening** `optsRef.current`/`currentOpts()` is the intended source of truth during in-flight saves (the sync guard only resyncs from props when `!onUpdateVariable.isPending`). `useUpdateVariable` has no optimistic `onMutate`, so props `opts` stays at the last server-confirmed value until the PATCH resolves. Two mutation paths bypass `currentOpts()` and read the lagging props array, overwriting the server with a snapshot that misses the concurrent edit. **Suggested fix** Make both bypassing paths read `currentOpts()` like every other option mutation. 1. Option label commit (~2323-2327): reuse the existing id-keyed helper, identical to the value field one line below: ```tsx onCommit={(v) => updateOptionField(opt.id, "label", v)} ``` `updateOptionField` already reads `currentOpts()` and keys by id, avoiding index drift against a diverged array. 2. `moveOptionByOffset` (~1962-1966): build from `currentOpts()`, mirroring the accepted drag path: ```tsx const moveOptionByOffset = (optionIdx: number, offset: number) => { if (optionOrderIsAlphabetical) return; const next = reorderItems(currentOpts(), optionIdx, optionIdx + offset); if (next) updateOpts(next); }; ``` Key the label commit by `opt.id` (not the render index `oi`) so it stays correct when `currentOpts()` has diverged in order/length from props. Leave the read-only/display uses of props `opts` (2050, 2052, 2105, 2231, 2236-2237, the disabled guard at 2310) unchanged. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.

<!-- gh-comment-id:4834697822 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1597
No description provided.