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

browserSurfaceStore.ts

121 lines3.5 KB

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 content: BrowserSurfaceContentPresentation | null;
14 readonly updatedAt: number;
16​
17export interface BrowserSurfaceContentPresentation {
18 readonly x: number;
19 readonly y: number;
20 readonly width: number;
21 readonly height: number;
22 readonly scale: number;
23 readonly scrollLeft: number;
24 readonly scrollTop: number;
26​
27interface BrowserSurfaceStoreState {
28 readonly byTabId: Record<string, BrowserSurfacePresentation>;
29 readonly present: (tabId: string, rect: BrowserSurfaceRect, visible: boolean) => void;
30 readonly presentContent: (tabId: string, content: BrowserSurfaceContentPresentation) => void;
31 readonly hide: (tabId: string) => void;
33​
34export function resolveBrowserSurfacePanelRect(
35 byTabId: Readonly<Record<string, BrowserSurfacePresentation>>,
36 tabId: string,
37): BrowserSurfaceRect | null {
38 const current = byTabId[tabId];
39 if (current?.visible && current.rect) return current.rect;
40​
41 let latestVisible: BrowserSurfacePresentation | undefined;
42 for (const presentation of Object.values(byTabId)) {
43 if (
44 presentation.visible &&
45 presentation.rect &&
46 (!latestVisible || presentation.updatedAt > latestVisible.updatedAt)
47 ) {
48 latestVisible = presentation;
49 }
50 }
51 return latestVisible?.rect ?? current?.rect ?? null;
53​
54const rectEquals = (left: BrowserSurfaceRect | null, right: BrowserSurfaceRect): boolean =>
55 left !== null &&
56 left.x === right.x &&
57 left.y === right.y &&
58 left.width === right.width &&
59 left.height === right.height;
60​
61export const useBrowserSurfaceStore = create<BrowserSurfaceStoreState>()((set) => ({
62 byTabId: {},
63 present: (tabId, rect, visible) =>
64 set((state) => {
65 const current = state.byTabId[tabId];
66 if (current && current.visible === visible && rectEquals(current.rect, rect)) return state;
67 return {
68 byTabId: {
69 ...state.byTabId,
70 [tabId]: { rect, visible, content: current?.content ?? null, updatedAt: Date.now() },
71 },
72 };
73 }),
74 presentContent: (tabId, content) =>
75 set((state) => {
76 const current = state.byTabId[tabId];
77 if (!current) {
78 return {
79 byTabId: {
80 ...state.byTabId,
81 [tabId]: {
82 rect: null,
83 visible: false,
84 content,
85 updatedAt: Date.now(),
86 },
87 },
88 };
89 }
90 const previous = current.content;
91 if (
92 previous &&
93 previous.x === content.x &&
94 previous.y === content.y &&
95 previous.width === content.width &&
96 previous.height === content.height &&
97 previous.scale === content.scale &&
98 previous.scrollLeft === content.scrollLeft &&
99 previous.scrollTop === content.scrollTop
100 ) {
101 return state;
102 }
103 return {
104 byTabId: {
105 ...state.byTabId,
106 [tabId]: { ...current, content, updatedAt: Date.now() },
107 },
108 };
109 }),
110 hide: (tabId) =>
111 set((state) => {
112 const current = state.byTabId[tabId];
113 if (!current || !current.visible) return state;
114 return {
115 byTabId: {
116 ...state.byTabId,
117 [tabId]: { ...current, visible: false, updatedAt: Date.now() },
118 },
119 };
120 }),
121}));