Open History to see the last commit
1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import type { DevicePlatform, ScopedThreadRef } from "@t3tools/contracts";
3import { create } from "zustand";
4
5export interface PreviewMiniPlayerPosition {
6 readonly x: number;
7 readonly y: number;
8}
9
10export interface PreviewMiniPlayerSize {
11 readonly width: number;
12 readonly height: number;
13}
14
15export type PreviewMiniPlayerSource =
16 | { readonly kind: "browser"; readonly tabId: string }
17 | {
18 readonly kind: "device";
19 readonly hostId: string;
20 readonly deviceId: string;
21 readonly platform: DevicePlatform;
22 readonly name: string;
23 }
24 | { readonly kind: "computer" };
25
26export interface PreviewMiniPlayerState {
27 readonly source: PreviewMiniPlayerSource;
28 readonly position: PreviewMiniPlayerPosition | null;
29 readonly width: number | null;
30 readonly lastInteraction: "drag" | "resize";
31}
32
33interface PreviewMiniPlayerStoreState {
34 readonly byThreadKey: Record<string, PreviewMiniPlayerState>;
35 readonly open: (ref: ScopedThreadRef, source: PreviewMiniPlayerSource) => void;
36 readonly close: (ref: ScopedThreadRef) => void;
37 /** `sourceKey` guards against a drag that outlives the source it started on. */
38 readonly move: (
39 ref: ScopedThreadRef,
40 sourceKey: string,
41 position: PreviewMiniPlayerPosition,
42 ) => void;
43 readonly resize: (
44 ref: ScopedThreadRef,
45 sourceKey: string,
46 width: number,
47 position?: PreviewMiniPlayerPosition,
48 ) => void;
49 readonly removeThread: (ref: ScopedThreadRef) => void;
50}
51
52export function previewMiniPlayerSourceKey(source: PreviewMiniPlayerSource): string {
53 switch (source.kind) {
54 case "browser":
55 return `browser:${source.tabId}`;
56 case "device":
57 return `device:${encodeURIComponent(source.hostId)}:${encodeURIComponent(source.deviceId)}`;
58 case "computer":
59 return "computer";
60 }
61}
62
63export const COMPUTER_MINI_PLAYER_SOURCE: PreviewMiniPlayerSource = { kind: "computer" };
64
65export const browserMiniPlayerSource = (tabId: string): PreviewMiniPlayerSource => ({
66 kind: "browser",
67 tabId,
68});
69
70export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((set) => ({
71 byThreadKey: {},
72 open: (ref, source) =>
73 set((state) => {
74 const threadKey = scopedThreadKey(ref);
75 const current = state.byThreadKey[threadKey];
76 if (
77 current &&
78 previewMiniPlayerSourceKey(current.source) === previewMiniPlayerSourceKey(source)
79 ) {
80 return state;
81 }
82 return {
83 byThreadKey: {
84 ...state.byThreadKey,
85 [threadKey]: {
86 source,
87 position: current?.position ?? null,
88 width: current?.width ?? null,
89 lastInteraction: current?.lastInteraction ?? "drag",
90 },
91 },
92 };
93 }),
94 close: (ref) =>
95 set((state) => {
96 const threadKey = scopedThreadKey(ref);
97 if (!(threadKey in state.byThreadKey)) return state;
98 const { [threadKey]: _closed, ...byThreadKey } = state.byThreadKey;
99 return { byThreadKey };
100 }),
101 move: (ref, sourceKey, position) =>
102 set((state) => {
103 const threadKey = scopedThreadKey(ref);
104 const current = state.byThreadKey[threadKey];
105 if (!current || previewMiniPlayerSourceKey(current.source) !== sourceKey) return state;
106 if (
107 current.position?.x === position.x &&
108 current.position.y === position.y &&
109 current.lastInteraction === "drag"
110 )
111 return state;
112 return {
113 byThreadKey: {
114 ...state.byThreadKey,
115 [threadKey]: { ...current, position, lastInteraction: "drag" },
116 },
117 };
118 }),
119 resize: (ref, sourceKey, width, position) =>
120 set((state) => {
121 const threadKey = scopedThreadKey(ref);
122 const current = state.byThreadKey[threadKey];
123 if (
124 !current ||
125 previewMiniPlayerSourceKey(current.source) !== sourceKey ||
126 (current.width === width &&
127 current.lastInteraction === "resize" &&
128 (!position || (current.position?.x === position.x && current.position.y === position.y)))
129 ) {
130 return state;
131 }
132 return {
133 byThreadKey: {
134 ...state.byThreadKey,
135 [threadKey]: {
136 ...current,
137 width,
138 position: position ?? current.position,
139 lastInteraction: "resize",
140 },
141 },
142 };
143 }),
144 removeThread: (ref) =>
145 set((state) => {
146 const threadKey = scopedThreadKey(ref);
147 if (!(threadKey in state.byThreadKey)) return state;
148 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
149 return { byThreadKey };
150 }),
151}));
152
153export function selectThreadPreviewMiniPlayer(
154 byThreadKey: Record<string, PreviewMiniPlayerState>,
155 ref: ScopedThreadRef | null | undefined,
156): PreviewMiniPlayerState | null {
157 if (!ref) return null;
158 return byThreadKey[scopedThreadKey(ref)] ?? null;
159}
160
161export function selectThreadPreviewMiniPlayerTabId(
162 byThreadKey: Record<string, PreviewMiniPlayerState>,
163 ref: ScopedThreadRef | null | undefined,
164): string | null {
165 const source = selectThreadPreviewMiniPlayer(byThreadKey, ref)?.source;
166 return source?.kind === "browser" ? source.tabId : null;
167}