Open History to see the last commit
1import { create } from "zustand";
2
3export interface BrowserSurfaceRect {
4 readonly x: number;
5 readonly y: number;
6 readonly width: number;
7 readonly height: number;
8}
9
10export interface BrowserSurfacePresentation {
11 readonly rect: BrowserSurfaceRect | null;
12 readonly visible: boolean;
13 readonly updatedAt: number;
14}
15
16interface BrowserSurfaceStoreState {
17 readonly byTabId: Record<string, BrowserSurfacePresentation>;
18 readonly present: (tabId: string, rect: BrowserSurfaceRect, visible: boolean) => void;
19 readonly hide: (tabId: string) => void;
20}
21
22const rectEquals = (left: BrowserSurfaceRect | null, right: BrowserSurfaceRect): boolean =>
23 left !== null &&
24 left.x === right.x &&
25 left.y === right.y &&
26 left.width === right.width &&
27 left.height === right.height;
28
29export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) => ({
30 byTabId: {},
31 present: (tabId, rect, visible) =>
32 set((state) => {
33 const current = state.byTabId[tabId];
34 if (current && current.visible === visible && rectEquals(current.rect, rect)) return state;
35 return {
36 byTabId: {
37 ...state.byTabId,
38 [tabId]: { rect, visible, updatedAt: Date.now() },
39 },
40 };
41 }),
42 hide: (tabId) =>
43 set((state) => {
44 const current = state.byTabId[tabId];
45 if (!current || !current.visible) return state;
46 return {
47 byTabId: {
48 ...state.byTabId,
49 [tabId]: { ...current, visible: false, updatedAt: Date.now() },
50 },
51 };
52 }),
53}));