From 8b81b7553b7a8848530d107e821d941e145fbaa2 Mon Sep 17 00:00:00 2001
From: Punyapal Shah <53343069+MrPunyapal@users.noreply.github.com>
Date: Thu, 5 Nov 2026 01:56:33 +0530
Subject: [PATCH] fix: sync the chat dock when another tab clears storage
---
resources/js/lib/chat-dock.ts | 5 ++-
tests/Unit/ChatDockStorage.test.tsx | 65 +++++++++++++++++++++++++++++
2 files changed, 69 insertions(+), 1 deletion(-)
create mode 100644 tests/Unit/ChatDockStorage.test.tsx
diff --git a/resources/js/lib/chat-dock.ts b/resources/js/lib/chat-dock.ts
index c99020a987..ea1d62886c 100644
--- a/resources/js/lib/chat-dock.ts
+++ b/resources/js/lib/chat-dock.ts
@@ -48,7 +48,10 @@ function subscribe(listener: () => void): () => void {
listeners.add(listener);
const onStorage = (event: StorageEvent) => {
- if (event.key === storageKey) {
+ if (
+ event.storageArea === window.localStorage &&
+ (event.key === storageKey || event.key === null)
+ ) {
dock = null;
listener();
}
diff --git a/tests/Unit/ChatDockStorage.test.tsx b/tests/Unit/ChatDockStorage.test.tsx
new file mode 100644
index 0000000000..e8621b9d9d
--- /dev/null
+++ b/tests/Unit/ChatDockStorage.test.tsx
@@ -0,0 +1,65 @@
+///
+import assert from 'node:assert/strict';
+import * as React from 'react';
+import { afterEach, test, vi } from 'vite-plus/test';
+
+const notify = vi.fn();
+let unsubscribe: () => void;
+vi.doMock('react', () => ({
+ ...React,
+ useSyncExternalStore: (
+ subscribe: (listener: () => void) => () => void,
+ snapshot: () => unknown,
+ ) => {
+ unsubscribe = subscribe(notify);
+ return snapshot();
+ },
+}));
+
+afterEach(() => {
+ unsubscribe?.();
+ vi.unstubAllGlobals();
+});
+
+test('clears the dock when another tab wipes local storage', async () => {
+ const values = new Map();
+ const listeners = new Map<
+ string,
+ (event: { key: string | null; storageArea: object | null }) => void
+ >();
+ const localStorage = {
+ getItem: (key: string) => values.get(key) ?? null,
+ setItem: (key: string, value: string) => values.set(key, value),
+ removeItem: (key: string) => values.delete(key),
+ };
+ values.set('chat-dock', JSON.stringify({
+ isDocked: true,
+ isExpanded: true,
+ conversation: 'alice',
+ }));
+ vi.stubGlobal('window', {
+ localStorage,
+ addEventListener: (event: string, callback: (event: { key: string | null; storageArea: object | null }) => void) =>
+ listeners.set(event, callback),
+ removeEventListener: (event: string) => listeners.delete(event),
+ });
+ const { useChatDock } = await import('../../resources/js/lib/chat-dock');
+
+ assert.equal(useChatDock().isDocked, true);
+ const onStorage = listeners.get('storage');
+ onStorage?.({ key: null, storageArea: {} });
+ assert.equal(notify.mock.calls.length, 0);
+ assert.equal(useChatDock().isDocked, true);
+
+ values.clear();
+ onStorage?.({ key: null, storageArea: localStorage });
+
+ assert.equal(notify.mock.calls.length, 1);
+ assert.deepEqual(useChatDock(), {
+ isDocked: false,
+ isExpanded: false,
+ conversation: null,
+ });
+ unsubscribe();
+ assert.equal(listeners.has('storage'), false);
+});