Files
pn-new-crm/src/hooks/realtime-invalidation-core.ts
Matt Ciaccio 8699f81879
Some checks failed
Build & Push Docker Images / lint (push) Failing after 1m18s
Build & Push Docker Images / build-and-push (push) Has been skipped
chore(style): codebase em-dash sweep + minor layout polish
Replaces every em-dash and en-dash with regular ASCII hyphens
across comments, JSX strings, and dev-facing logs. Mostly cosmetic
but stops the inconsistent mix that crept in over the last few
months (some files used em-dashes in comments, others didn't,
some used both).

Bundles two small dashboard-layout tweaks that touch a couple of
already-modified files:
- (dashboard)/layout.tsx main padding goes from p-6 to pt-3 px-6
  pb-6 so page content sits closer to the topbar.
- Sidebar now receives the ports list it needs for the footer
  port switcher.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 22:57:01 +02:00

53 lines
1.8 KiB
TypeScript

import type { QueryClient, QueryKey } from '@tanstack/react-query';
/** Minimum surface of socket.io's client we use here. Kept loose so the
* helper can be unit-tested with a stub object without dragging the full
* socket.io dependency into the test runtime. */
export interface SocketLike {
on(event: string, handler: (...args: unknown[]) => void): unknown;
off(event: string, handler: (...args: unknown[]) => void): unknown;
}
export type EventMap = Record<string, QueryKey[]>;
/**
* Pure subscription logic for `useRealtimeInvalidation`. Registers one
* handler per event key. Each handler reads the latest eventMap from the
* supplied getter so callers can pass a fresh object literal on every render
* without re-subscribing.
*
* Returns a cleanup function that removes the registered handlers.
*
* Lives in its own JSX-free file so it can be unit-tested under vitest's
* node environment without dragging the React provider into the bundle.
*/
export function subscribeRealtimeInvalidations(
socket: SocketLike,
eventKeys: string[],
queryClient: Pick<QueryClient, 'invalidateQueries'>,
getEventMap: () => EventMap,
): () => void {
const handlers: Array<{ event: string; handler: (...args: unknown[]) => void }> = [];
for (const event of eventKeys) {
const handler = () => {
// Read the LATEST map at fire-time - not at subscription time - so
// callers passing inline `{ 'client:created': [...] }` literals don't
// bind a stale snapshot if they re-render.
const queryKeys = getEventMap()[event];
if (!queryKeys) return;
for (const key of queryKeys) {
queryClient.invalidateQueries({ queryKey: key });
}
};
socket.on(event, handler);
handlers.push({ event, handler });
}
return () => {
for (const { event, handler } of handlers) {
socket.off(event, handler);
}
};
}