[PR #275] [MERGED] fix(chat-bg): make Remove background actually stick across switches #4540

Closed
opened 2026-10-22 04:08:32 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/275
Author: @LukaTheHero
Created: 4/28/2026
Status: ✅ Merged
Merged: 4/28/2026
Merged by: @LukaTheHero

Base: main ← Head: fix/chat-background-remove-persist


📝 Commits (1)

  • c6d3442 fix(chat-bg): make Remove background actually stick across switches

📊 Changes

3 files changed (+60 additions, -16 deletions)

View changed files

📝 packages/client/src/components/chat/ChatArea.tsx (+28 -12)
📝 packages/client/src/components/panels/SettingsPanel.tsx (+20 -2)
📝 packages/client/src/hooks/use-chats.ts (+12 -2)

📄 Description

Summary

Closes #260.

Removing a chat background via the Settings panel did not survive chat switches (and sometimes did not survive a page refresh either). Three compounding issues were responsible:

1. Stale React Query cache after metadata writes

useUpdateChatMetadata only called qc.invalidateQueries(...) on success and never wrote the server response into the cache. React Query invalidation only triggers a refetch when an active observer is mounted; if the user fired the mutation and then navigated away, the cache was marked stale but never refreshed. Coming back to that chat served the pre-mutation snapshot, and the restore effect re-applied the supposedly-removed background.

Fix: the hook now setQueryData(chatKeys.detail(id), data) directly with the mutation response, so the cache is always fresh regardless of observer state. Falls back to invalidate if the response is null.

2. Restore effect was a one-way sync

ChatArea's background-restore effect only set chatBackground when the chat's metadata had a value — it never cleared the global UI-store value. Switching to a chat whose metadata was empty (or had been cleared) left whatever URL was last in the store, including from the previous chat.

Fix: the effect now mirrors metadata exactly, setting null when metadata has no background. Brand-new chats no longer carry over the previous chat's background as a "don't flash to black" preview, but a fresh-empty chat is the more honest behavior.

3. Remove button leaned on a fragile debounced effect chain

The Remove handler in SettingsPanel only called setChatBackground(null). Persistence relied on a useEffect in ChatArea to observe that change and (after a 500 ms debounce) fire a metadata patch. That chain had timing/closure ambiguities that occasionally dropped the write.

Fix: the handler now calls useUpdateChatMetadata directly from the click, alongside the UI-store update — no debounce, no effect dep race.

The ChatArea persist effect's null-write branch is left in place because slash commands, scene events, and agent paths still clear the background without going through Settings.

Files

  • packages/client/src/hooks/use-chats.ts — useUpdateChatMetadata.onSuccess writes response into the detail cache.
  • packages/client/src/components/chat/ChatArea.tsx — restore effect now syncs to null when metadata is empty.
  • packages/client/src/components/panels/SettingsPanel.tsx — Remove / pick handler fires the mutation directly.

Side benefits

Every other consumer of useUpdateChatMetadata (sprite settings, branch labels, chat setup wizard, summary popover, game-state sync, etc.) now sees its mutation result reflected in the cache immediately rather than after a refetch round-trip. No behavior changes for those callers; they just get consistent freshness.

Game mode is unaffected by the restore-effect change — GameSurface deliberately ignores chatBackground (_chatBackground underscore-prefixed at line 565, with an accompanying comment) and renders its own scene-derived background from the asset manifest.

Test plan

  • Manually verify in browser: set a background on a chat, click Remove. Background disappears.
  • Manually verify in browser: F5 after Remove. Background stays gone.
  • Manually verify in browser: set bg on chat A → click Remove → switch to chat B → switch back to chat A. Background stays gone (this was the flaky case).
  • Manually verify in browser: set different backgrounds on chats A and B; switch between them; each chat keeps its own bg.
  • Manually verify in browser: pick a background, immediately click another chat (while debounce would still be pending). Switch back. Bg should persist.
  • Manually verify in browser: switch repeatedly between an RP chat and a Conversation chat — bg state stays correct on both sides.
  • Manually verify in game mode: starting / continuing a game session does not trigger a stray bg change in non-game chats.
  • pnpm check passes (no new TS / ESLint regressions).

Notes

No linked feature request beyond issue #260, which is the bug report itself.

Summary by CodeRabbit

  • Bug Fixes
    • Chat background settings now properly persist when switching between different chats
    • Clearing a background now correctly maintains the removed state across chat switches and application reloads

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/275 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 4/28/2026 **Status:** ✅ Merged **Merged:** 4/28/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `fix/chat-background-remove-persist` --- ### 📝 Commits (1) - [`c6d3442`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c6d3442931761f501a4a6ddc9c1faaa8792bbb45) fix(chat-bg): make Remove background actually stick across switches ### 📊 Changes **3 files changed** (+60 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/chat/ChatArea.tsx` (+28 -12) 📝 `packages/client/src/components/panels/SettingsPanel.tsx` (+20 -2) 📝 `packages/client/src/hooks/use-chats.ts` (+12 -2) </details> ### 📄 Description ## Summary Closes #260. Removing a chat background via the Settings panel did not survive chat switches (and sometimes did not survive a page refresh either). Three compounding issues were responsible: ### 1. Stale React Query cache after metadata writes `useUpdateChatMetadata` only called `qc.invalidateQueries(...)` on success and never wrote the server response into the cache. React Query invalidation only triggers a refetch when an active observer is mounted; if the user fired the mutation and then navigated away, the cache was marked stale but never refreshed. Coming back to that chat served the pre-mutation snapshot, and the restore effect re-applied the supposedly-removed background. **Fix:** the hook now `setQueryData(chatKeys.detail(id), data)` directly with the mutation response, so the cache is always fresh regardless of observer state. Falls back to invalidate if the response is null. ### 2. Restore effect was a one-way sync `ChatArea`'s background-restore effect only set `chatBackground` when the chat's metadata had a value — it never cleared the global UI-store value. Switching to a chat whose metadata was empty (or had been cleared) left whatever URL was last in the store, including from the previous chat. **Fix:** the effect now mirrors metadata exactly, setting `null` when metadata has no background. Brand-new chats no longer carry over the previous chat's background as a "don't flash to black" preview, but a fresh-empty chat is the more honest behavior. ### 3. Remove button leaned on a fragile debounced effect chain The Remove handler in `SettingsPanel` only called `setChatBackground(null)`. Persistence relied on a `useEffect` in `ChatArea` to observe that change and (after a 500 ms debounce) fire a metadata patch. That chain had timing/closure ambiguities that occasionally dropped the write. **Fix:** the handler now calls `useUpdateChatMetadata` directly from the click, alongside the UI-store update — no debounce, no effect dep race. The `ChatArea` persist effect's null-write branch is left in place because slash commands, scene events, and agent paths still clear the background without going through Settings. ## Files - `packages/client/src/hooks/use-chats.ts` — `useUpdateChatMetadata.onSuccess` writes response into the detail cache. - `packages/client/src/components/chat/ChatArea.tsx` — restore effect now syncs to null when metadata is empty. - `packages/client/src/components/panels/SettingsPanel.tsx` — Remove / pick handler fires the mutation directly. ## Side benefits Every other consumer of `useUpdateChatMetadata` (sprite settings, branch labels, chat setup wizard, summary popover, game-state sync, etc.) now sees its mutation result reflected in the cache immediately rather than after a refetch round-trip. No behavior changes for those callers; they just get consistent freshness. Game mode is unaffected by the restore-effect change — `GameSurface` deliberately ignores `chatBackground` (`_chatBackground` underscore-prefixed at line 565, with an accompanying comment) and renders its own scene-derived background from the asset manifest. ## Test plan - [x] Manually verify in browser: set a background on a chat, click Remove. Background disappears. - [x] Manually verify in browser: F5 after Remove. Background stays gone. - [x] Manually verify in browser: set bg on chat A → click Remove → switch to chat B → switch back to chat A. Background stays gone (this was the flaky case). - [x] Manually verify in browser: set different backgrounds on chats A and B; switch between them; each chat keeps its own bg. - [x] Manually verify in browser: pick a background, immediately click another chat (while debounce would still be pending). Switch back. Bg should persist. - [x] Manually verify in browser: switch repeatedly between an RP chat and a Conversation chat — bg state stays correct on both sides. - [x] Manually verify in game mode: starting / continuing a game session does not trigger a stray bg change in non-game chats. - [x] `pnpm check` passes (no new TS / ESLint regressions). ## Notes No linked feature request beyond issue #260, which is the bug report itself. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Chat background settings now properly persist when switching between different chats * Clearing a background now correctly maintains the removed state across chat switches and application reloads <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:08:32 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4540
No description provided.