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
6 changes: 6 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '@/lib/idleLock';
import { parseStellarQrPayload } from '@/utils/qr';
import { useOfflineQueueStore } from '@/stores/offlineQueueStore';
import { initNameWatchlistSync, cleanupNameWatchlistSync } from '@/store/nameWatchlistStore';

function SessionLock({ onUnlock }: { onUnlock: () => void }) {
const [passphrase, setPassphrase] = useState('');
Expand Down Expand Up @@ -157,6 +158,11 @@ export function App() {
return () => idleLock.stop();
}, [relockSession, sessionLocked]);

useEffect(() => {
initNameWatchlistSync();
return () => cleanupNameWatchlistSync();
}, []);

useEffect(() => {
if (location.pathname !== '/send') return;
const sharedText = new URLSearchParams(location.search).get('text');
Expand Down
13 changes: 11 additions & 2 deletions src/store/multiTabSync.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @vitest-environment jsdom */
import { describe, expect, it, beforeEach } from 'vitest';
import { describe, expect, it, beforeEach, afterEach } from 'vitest';
import { renderHook, act } from '@testing-library/react';
import { ContactsProvider, useContacts } from './contactsStore';
import {
Expand All @@ -8,12 +8,21 @@ import {
DEFAULT_SCAN_STRATEGY,
} from '../context/ScanStrategyContext';
import { SplitTemplatesProvider, useSplitTemplates } from './splitTemplatesStore';
import { useNameWatchlistStore } from './nameWatchlistStore';
import {
useNameWatchlistStore,
initNameWatchlistSync,
cleanupNameWatchlistSync,
} from './nameWatchlistStore';
import { useStealthLabels } from '../hooks/useStealthLabels';

describe('Multi-tab synchronization', () => {
beforeEach(() => {
localStorage.clear();
initNameWatchlistSync();
});

afterEach(() => {
cleanupNameWatchlistSync();
});

it('synchronizes contacts between tabs', () => {
Expand Down
49 changes: 45 additions & 4 deletions src/store/nameWatchlistStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi, afterEach } from 'vitest';

class MemoryStorage implements Storage {
private values = new Map<string, string>();
Expand Down Expand Up @@ -28,16 +28,56 @@ class MemoryStorage implements Storage {
}
}

class MockWindow {
localStorage: Storage;
private listeners: Map<string, ((e: StorageEvent) => void)[]> = new Map();

constructor(storage: Storage) {
this.localStorage = storage;
}

addEventListener(event: string, listener: (e: StorageEvent) => void) {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event)!.push(listener);
}

removeEventListener(event: string, listener: (e: StorageEvent) => void) {
const eventListeners = this.listeners.get(event);
if (eventListeners) {
const index = eventListeners.indexOf(listener);
if (index > -1) {
eventListeners.splice(index, 1);
}
}
}

dispatchEvent(event: StorageEvent) {
const eventListeners = this.listeners.get(event.type);
if (eventListeners) {
eventListeners.forEach((listener) => listener(event));
}
}
}

describe('name auction watchlist', () => {
beforeEach(() => {
vi.resetModules();
const storage = new MemoryStorage();
const mockWindow = new MockWindow(storage);
vi.stubGlobal('localStorage', storage);
vi.stubGlobal('window', { localStorage: storage });
vi.stubGlobal('window', mockWindow);
});

afterEach(async () => {
const { cleanupNameWatchlistSync } = await import('./nameWatchlistStore');
cleanupNameWatchlistSync();
});

it('restores watched auctions and sealed bid recovery data', async () => {
const { useNameWatchlistStore } = await import('./nameWatchlistStore');
const { useNameWatchlistStore, initNameWatchlistSync } = await import('./nameWatchlistStore');
initNameWatchlistSync();
const store = useNameWatchlistStore.getState();
store.watchAuction({ name: 'NOVA', endsAt: 1_800_000_000 });
store.saveBid({
Expand Down Expand Up @@ -66,7 +106,8 @@ describe('name auction watchlist', () => {

it('recovers from corrupt and unknown-version persisted data', async () => {
localStorage.setItem('wraith-name-auction-watchlist', '{not-json');
const { useNameWatchlistStore } = await import('./nameWatchlistStore');
const { useNameWatchlistStore, initNameWatchlistSync } = await import('./nameWatchlistStore');
initNameWatchlistSync();
await useNameWatchlistStore.persist.rehydrate();
expect(useNameWatchlistStore.getState().watchedAuctions).toEqual([]);
expect(useNameWatchlistStore.getState().bids).toEqual({});
Expand Down
23 changes: 20 additions & 3 deletions src/store/nameWatchlistStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,13 @@ export const useNameWatchlistStore = create<NameWatchlistState>()(
),
);

if (typeof window !== 'undefined') {
window.addEventListener('storage', (e) => {
let storageListener: ((e: StorageEvent) => void) | null = null;

export function initNameWatchlistSync() {
if (typeof window === 'undefined' || storageListener !== null) return;
if (typeof window.addEventListener !== 'function') return;

storageListener = (e: StorageEvent) => {
if (e.key === 'wraith-name-auction-watchlist') {
if (e.newValue) {
useNameWatchlistStore.persist.rehydrate();
Expand All @@ -149,5 +154,17 @@ if (typeof window !== 'undefined') {
} else if (e.key === null) {
useNameWatchlistStore.setState({ watchedAuctions: [], bids: {} });
}
});
};

window.addEventListener('storage', storageListener);
}

export function cleanupNameWatchlistSync() {
if (storageListener === null) return;
if (typeof window.removeEventListener !== 'function') {
storageListener = null;
return;
}
window.removeEventListener('storage', storageListener);
storageListener = null;
}
Loading