Skip to content

Fix naming popup showing stale state for second pet - #41

Merged
ppXD merged 1 commit into
mainfrom
fix/naming-popup-reuse-stale-state
May 19, 2026
Merged

ppXD merged 1 commit into
mainfrom
fix/naming-popup-reuse-stale-state

Conversation

@ppXD

@ppXD ppXD commented May 19, 2026

Copy link
Copy Markdown
Owner

Bug repro

  1. Create pet A, rename it via the hatching popup
  2. Switch to pet B (or create one)
  3. Pet B reaches L1 and the naming popup appears
  4. Popup shows pet A's data; input + buttons frozen, nothing responds

Root cause

The Tauri naming window is created ONCE via WebviewWindowBuilder and reused via .hide()/.show() across hatchings. NamingView (the React component inside) is therefore the SAME instance for every popup. Two stale-state pathologies followed:

  1. useEffect([], []) 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() left submitting=true permanently (comment said "window is about to hide"). On second show, both input and buttons read disabled={submitting}, locking out interaction.

Fix

Layer Change
Backend (lib.rs) After .show() in naming_window_show, emit naming://refresh
Frontend (NamingView.tsx) Subscribe to naming://refresh via @tauri-apps/api/event's listen. On every refresh: re-read naming_current + reset name, submitting, error to initial values
Frontend (defense in depth) Reset submitting=false in the success branch of dismiss() too

Resulting flow

Pet A hatches β†’ show β†’ prompt=A β†’ submit β†’ reset β†’ hide
Pet B hatches β†’ show + emit refresh β†’ prompt=B β†’ state clean βœ“

Test plan

πŸ€– Generated with Claude Code

# 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
@github-actions github-actions Bot added the fix Auto-applied by .github/workflows/auto-label.yml label May 19, 2026
@ppXD
ppXD merged commit 53f011f into main May 19, 2026
4 checks passed
@ppXD
ppXD deleted the fix/naming-popup-reuse-stale-state branch May 19, 2026 08:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Auto-applied by .github/workflows/auto-label.yml

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant