Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

chatSurfaceSlot.tsx

22 lines607 Bytes

1import { useSyncExternalStore, type ReactNode } from "react";
2​
3let surface: ReactNode = null;
4const listeners = new Set<() => void>();
5​
6function subscribe(listener: () => void) {
7 listeners.add(listener);
8 return () => {
9 listeners.delete(listener);
10 };
12​
13/** Register lazily in the root so full-page navigation preserves the chat surface. */
14export function registerChatSurface(element: ReactNode) {
15 if (surface !== null) return;
16 surface = element;
17 for (const listener of listeners) listener();
19​
20export function ChatSurfaceSlot() {
21 return useSyncExternalStore(subscribe, () => surface);