File size: 905 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * registry/useChatExtensions.ts — React subscriptions for the chat extension registry.
 *
 * Wraps `useSyncExternalStore` so ChatPage re-renders whenever a plugin
 * registers/disposes. Snapshots are stable refs between mutations
 * (the registry replaces them only on change), so the useMemo
 * dep chain in ChatPage doesn't fire spuriously.
 */
import { useSyncExternalStore } from "react";
import {
  chatExtensions,
  type ChatListSnapshot,
  type ChatScalarSnapshot,
} from "./chatExtensions";

function subscribe(cb: () => void): () => void {
  return chatExtensions.subscribe(cb);
}

export function useChatScalarSnapshot(): ChatScalarSnapshot {
  return useSyncExternalStore(subscribe, () =>
    chatExtensions.getScalarSnapshot(),
  );
}

export function useChatListSnapshot(): ChatListSnapshot {
  return useSyncExternalStore(subscribe, () =>
    chatExtensions.getListSnapshot(),
  );
}