Repository navigation
fix(mochi): restore Save as Preset focus after the close commits - #29959
Conversation
The Save as Preset field returned focus through setTimeout(0) after an awaited save. React schedules that update on its own task, so the timer could run first, see the input still focused, skip the restore, and leave focus on body once the input unmounted. MochiSavePresetFocus flaked on exactly that. Restore focus from a layout effect that runs after the commit that closes the field, the same pattern the crew-companion copy uses. Fixes #29897
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the focus restore now runs in a layout effect after the commit that remounts the trigger, which removes the timer race at its root. [DESIGN-REVIEWED] 5d4cfc3 Description read[DESCRIPTION-READ] fb762bde5b4eafecbc27b65f8ede5ad4ac07d6b6a067cda02fbe7c6afddc393c That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
UX Review (Opus 5.5) — ✅ PASSUX-level review of UX-Verdict: PASS Nothing to check. The diff only changes when focus returns to the Save as Preset button. It adds or changes no visible control, label or state, so the missing blind read leaves no evidence gap. [UX-REVIEWED] 5d4cfc3 Description read[DESCRIPTION-READ] c591cba39ab7ea4e719bc255ebd07a9b5f83c26462cf3080dc8d9880b236e9e2 That sha256 names everything this verdict read: the PR description, plus the 3 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The fix moves the focus restore onto React's commit, which is where the race comes from. The same timer-after-await pattern is still used in five other places. Notes
What this change shipsInventory (2 items) — 1 justifiedIntent: after saving a Mochi preset with Enter, keyboard focus reliably goes back to the Save as Preset button. This is a FIX (#29897, CI run linked).
[FIRST-PRINCIPLES-REVIEWED] 5d4cfc3 Description read[DESCRIPTION-READ] 706607c3a77519634556ba4a84261445c9f36802ba6710a374820bbd58475089 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Tier 1 auto-approve: fix (1 file). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, security checklist all-NO, no reviewer-lane findings, readiness signal fresh for this head SHA, declared category matches the diff. Category: Mochi Save as Preset focus is restored in a layout effect after the trigger remounts, replacing a timer that could fire before the commit.
Problem / Motivation
Goal: Saving a Mochi color preset with Enter always puts focus back on the Save as Preset button, so
MochiSavePresetFocusstops flaking.Frontend Tests (3)failed once on PR #10838 (run 41864791468, job 126702263259):Enter saves and returns focus to the Save as Preset buttonexpected the button to have focus and found<body>. That head already contained #28515, the earlierrequestAnimationFrame->setTimeout(0)attempt, so that fix did not close the race.Why it matters
The flake reds unrelated PRs and costs reruns. The same race can strand keyboard focus on
bodyfor a real user after a slow save.Not a goal
Intended? / Repro
Intended? No. The comment above
closeSaveFormon main says every close hands focus back to the trigger (website/src/apps/mochi/src/renderer/ColorCustomizer.tsx:136-153). Focus left onbodybreaks that contract.Repro: CI log above, commit
0e5052dfae3eea1aa5fd18ed284ceb84072b566b(608 commits ahead of #28515'sd6d40e23a2, so it carries the setTimeout fix). Input: typeSunsetin the name field, press Enter. Seen: focus on<body>instead of the Save as Preset button. Passed on rerun of the same job.What changed (motivation → approach → change)
Symptom: focus sometimes lands on
bodyafter an Enter save.Root cause:
handleSaveAsPresetawaitspresetsSaveCustom, then callscloseSaveForm. That update is outside any event handler, so React schedules its commit on a separate task.closeSaveFormqueued its focus check withsetTimeout(0), which is another separate task with no ordering guarantee against React's. When the timer ran first it saw the input still focused, skipped the restore, and then React unmounted the input and focus fell tobody.Change:
closeSaveFormnow sets a ref and closes the field. AuseLayoutEffectkeyed onshowSaveFormruns right after the commit that remounts the trigger and does the same "only if focus is on body" restore. This is the patternwebsite/src/apps/crew-companion/ColorCustomizerPanel.tsxalready uses on main.Alternative considered: keep the timer and retry it until the trigger is mounted. Rejected: it is still timing-based and needs a cap, while the effect runs exactly once after the commit.
Backwards compatibility
Compatible: same focus rule (restore only when focus was dropped to body), now applied after the commit instead of on a timer.
Removes nothing: no symbol, prop or behavior is removed.
Tests
Proof: not provable -- frontend test (prove.py runs Python only)
website/src/test/MochiSavePresetFocus.test.tsxalready covers every close path (Enter, Escape, confirm, cancel, slow save after the user moved away). It is unchanged; it was flaky only because the product code raced. Locally only static gates ran (tsc --noEmit,eslint); CI runs the tests.Manual verification
N/A — unit coverage sufficient: the test drives every close path through the real component.
Screenshots / video
Why no screenshot: Only the moment focus is restored changes; nothing renders differently.
Related Issues
Fixes #29897
Sibling sweep
Swept:
git grep -n "closeSaveForm" -- website/srcSibling: website/src/apps/crew-companion/ColorCustomizerPanel.tsx -- not affected -- already restores focus from a layout effect on main
Sibling: website/src/apps/mochi/src/renderer/ColorCustomizer.tsx -- changed here
Pattern harvest
Rule candidate: review-prompt
Pattern: focus restore scheduled with setTimeout/requestAnimationFrame after an await races React's commit; restore from a layout effect keyed on the state that remounts the target.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributions are accepted under the project's Apache-2.0 license.