Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,17 @@ async fn naming_window_show(
// next time the pet's state is re-evaluated.
|main, w, h| notify_window_position(main, w, h),
)
.await
.await?;
// Nudge the naming window's React side to re-read `last_naming`
// and reset its local form state. The window itself is created
// once and reused via .hide()/.show() across hatching events —
// without this event the NamingView component (mounted once)
// keeps the FIRST pet's prompt forever, and worse, its
// `submitting` state stays stuck at `true` from the previous
// dismiss, disabling the input + buttons. See the matching
// listener in NamingView.tsx.
let _ = app.emit("naming://refresh", ());
Ok(())
}

#[tauri::command]
Expand Down
44 changes: 31 additions & 13 deletions desktop/src/views/NamingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* NotifyView.
*/

import { useEffect, useRef, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import "./NamingView.css";

interface NamingPrompt {
Expand All @@ -40,22 +41,34 @@ export function NamingView() {
const inputRef = useRef<HTMLInputElement | null>(null);
const lastSizeRef = useRef<{ w: number; h: number } | null>(null);

// Pull the current prompt on mount. Naming windows are one-shot —
// no need to subscribe to live updates (each hatching gets a fresh
// `naming_window_show` call which replaces the stored prompt).
useEffect(() => {
let cancelled = false;
// Pull the current prompt on mount AND on every `naming://refresh`
// emitted by `naming_window_show`. The Tauri naming window is
// created once and reused via .hide()/.show() across hatching
// events — so this React component is the SAME instance for every
// subsequent popup. Without the listener, the prompt + form state
// would freeze at whatever the first hatching set (stale pet name,
// and worse, `submitting=true` carried over from the previous
// dismiss would lock out the input + buttons).
const refreshFromBackend = useCallback(() => {
invoke<NamingPrompt | null>("naming_current")
.then((p) => {
if (!cancelled && p) setPrompt(p);
if (p) {
setPrompt(p);
setName(""); // clear stale input value
setSubmitting(false); // unstick the previous dismiss
setError(null);
}
})
.catch((e) => {
if (!cancelled) setError(String(e));
});
.catch((e) => setError(String(e)));
}, []);

useEffect(() => {
refreshFromBackend();
const unlistenP = listen("naming://refresh", () => refreshFromBackend());
return () => {
cancelled = true;
unlistenP.then((unlisten) => unlisten()).catch(() => {});
};
}, []);
}, [refreshFromBackend]);

// Focus the input once the prompt has rendered.
useEffect(() => {
Expand Down Expand Up @@ -98,7 +111,12 @@ export function NamingView() {
name: confirmed && name.trim() !== "" ? name.trim() : null,
confirmed,
});
// Don't reset `submitting` — the window is about to hide.
// Reset `submitting` immediately. The window is about to hide,
// but the React instance lives on (Tauri reuses the window) —
// leaving `submitting=true` would lock out the buttons on the
// NEXT hatching event before our naming://refresh listener
// has a chance to fire. Defense in depth.
setSubmitting(false);
} catch (e) {
setError(String(e));
setSubmitting(false);
Expand Down
Loading