fix: multiple bugs

* Hard-refresh saves coords before disconnect
* Position persisted between sessions
* Scaling isn't assumed on initial, it waits for calculations to finish
This commit is contained in:
Evan Carroll 2026-01-13 23:42:22 -06:00
parent a96581cbf0
commit 98f38c9714
3 changed files with 99 additions and 8 deletions

View file

@ -61,6 +61,9 @@ pub fn RealmPage() -> impl IntoView {
// Loose props state
let (loose_props, set_loose_props) = signal(Vec::<LooseProp>::new());
// Track user's current position for saving on beforeunload
let (current_position, set_current_position) = signal((400.0_f64, 300.0_f64));
let realm_data = LocalResource::new(move || {
let slug = slug.get();
async move {
@ -233,6 +236,8 @@ pub fn RealmPage() -> impl IntoView {
// Handle position update via WebSocket
#[cfg(feature = "hydrate")]
let on_move = Callback::new(move |(x, y): (f64, f64)| {
// Track position for saving on beforeunload
set_current_position.set((x, y));
ws_sender.with_value(|sender| {
if let Some(send_fn) = sender {
send_fn(ClientMessage::UpdatePosition { x, y });
@ -359,6 +364,34 @@ pub fn RealmPage() -> impl IntoView {
// Store the closure for cleanup
*closure_holder_clone.borrow_mut() = Some(closure);
});
// Save position on page unload (beforeunload event)
Effect::new({
let ws_sender = ws_sender.clone();
move |_| {
let Some(window) = web_sys::window() else {
return;
};
let handler = Closure::<dyn Fn(web_sys::BeforeUnloadEvent)>::new({
let ws_sender = ws_sender.clone();
move |_: web_sys::BeforeUnloadEvent| {
let (x, y) = current_position.get_untracked();
ws_sender.with_value(|sender| {
if let Some(send_fn) = sender {
send_fn(ClientMessage::UpdatePosition { x, y });
}
});
}
});
let _ = window.add_event_listener_with_callback(
"beforeunload",
handler.as_ref().unchecked_ref(),
);
handler.forget();
}
});
}
// Callback for chat focus changes