Fix naming popup showing stale state for second pet - #41
Merged
Merged
Conversation
# The bug
User created pet A, renamed it, switched to pet B. Pet B evolved.
The naming popup that appeared for pet B showed pet A's data and
neither the input nor buttons would respond.
# Root cause
The Tauri naming window is created ONCE via WebviewWindowBuilder
and reused via .hide()/.show() across hatching events. NamingView
(the React component inside that window) is therefore the same
instance for every popup. Two stale-state pathologies followed:
1. The `useEffect(() => { invoke('naming_current').then(setPrompt) },
[])` only fires once at initial mount. Subsequent `show()` calls
re-display the same component with pet A's old prompt frozen
in `useState`.
2. `dismiss()` called `setSubmitting(true)` before invoking
`naming_dismiss`, then explicitly DID NOT reset it on success
(comment: "the window is about to hide"). That left
`submitting=true` permanently. Both input and buttons read
`disabled={submitting}`, so on the second show the user
couldn't type or click.
# Fix
Backend (`naming_window_show`): after `.show()`, emit a
`naming://refresh` Tauri event. Plumbed through the existing
AppHandle.emit.
Frontend (`NamingView.tsx`):
- Subscribe to `naming://refresh` via `@tauri-apps/api/event`'s
`listen`. On every refresh, re-read `naming_current` and reset
`name`, `submitting`, and `error` to their initial values.
- Also reset `submitting=false` in the success branch of
`dismiss()` as defense-in-depth β the listener should normally
cover it but a successful dismiss leaving its own state clean
means no race window where the user could re-click before the
next refresh.
Resulting flow:
Pet A hatches β show, prompt=A, submit β reset, hide
Pet B hatches β show + emit refresh β prompt=B, state clean β
# Test plan
- [x] cargo test --lib clean (310/310)
- [x] pnpm tsc --noEmit clean
- [ ] User reproduces original scenario: create 2 pets, name #1,
let #2 evolve β popup shows #2's data, input + buttons work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug repro
Root cause
The Tauri naming window is created ONCE via
WebviewWindowBuilderand reused via.hide()/.show()across hatchings.NamingView(the React component inside) is therefore the SAME instance for every popup. Two stale-state pathologies followed:useEffect([], [])only fires once at initial mount. Subsequent.show()calls re-display the same component with pet A's old prompt frozen inuseState.dismiss()leftsubmitting=truepermanently (comment said "window is about to hide"). On second show, both input and buttons readdisabled={submitting}, locking out interaction.Fix
lib.rs).show()innaming_window_show, emitnaming://refreshNamingView.tsx)naming://refreshvia@tauri-apps/api/event'slisten. On every refresh: re-readnaming_current+ resetname,submitting,errorto initial valuessubmitting=falsein the success branch ofdismiss()tooResulting flow
Test plan
cargo test --lib(310/310 pass)pnpm tsc --noEmitcleanπ€ Generated with Claude Code