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
15/** What the floating player mirrors: a browser tab or a device stream. */
16export type PreviewMiniPlayerSource =
17 | { readonly kind: "browser"; readonly tabId: string }
18 | {
19 readonly kind: "device";
20 readonly hostId: string;
21 readonly deviceId: string;
22 readonly platform: DevicePlatform;
23 readonly name: string;
24 };
25
26export interface PreviewMiniPlayerState {
27 readonly source: PreviewMiniPlayerSource;
28 readonly position: PreviewMiniPlayerPosition | null;
29 /** Height always follows the mirrored source's aspect ratio. */
30 readonly width: number | null;
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: (ref: ScopedThreadRef, sourceKey: string, width: number) => void;
44 readonly removeThread: (ref: ScopedThreadRef) => void;
45}
46
47export function previewMiniPlayerSourceKey(source: PreviewMiniPlayerSource): string {
48 return source.kind === "browser"
49 ? `browser:${source.tabId}`
50 : `device:${encodeURIComponent(source.hostId)}:${encodeURIComponent(source.deviceId)}`;
51}
52
53export const browserMiniPlayerSource = (tabId: string): PreviewMiniPlayerSource => ({
54 kind: "browser",
55 tabId,
56});
57
58export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((set) => ({
59 byThreadKey: {},
60 open: (ref, source) =>
61 set((state) => {
62 const threadKey = scopedThreadKey(ref);
63 const current = state.byThreadKey[threadKey];
64 if (
65 current &&
66 previewMiniPlayerSourceKey(current.source) === previewMiniPlayerSourceKey(source)
67 ) {
68 return state;
69 }
70 return {
71 byThreadKey: {
72 ...state.byThreadKey,
73 [threadKey]: {
74 source,
75 position: current?.position ?? null,
76 width: current?.width ?? null,
77 },
78 },
79 };
80 }),
81 close: (ref) =>
82 set((state) => {
83 const threadKey = scopedThreadKey(ref);
84 if (!(threadKey in state.byThreadKey)) return state;
85 const { [threadKey]: _closed, ...byThreadKey } = state.byThreadKey;
86 return { byThreadKey };
87 }),
88 move: (ref, sourceKey, position) =>
89 set((state) => {
90 const threadKey = scopedThreadKey(ref);
91 const current = state.byThreadKey[threadKey];
92 if (!current || previewMiniPlayerSourceKey(current.source) !== sourceKey) return state;
93 if (current.position?.x === position.x && current.position.y === position.y) return state;
94 return {
95 byThreadKey: {
96 ...state.byThreadKey,
97 [threadKey]: { ...current, position },
98 },
99 };
100 }),
101 resize: (ref, sourceKey, width) =>
102 set((state) => {
103 const threadKey = scopedThreadKey(ref);
104 const current = state.byThreadKey[threadKey];
105 if (
106 !current ||
107 previewMiniPlayerSourceKey(current.source) !== sourceKey ||
108 current.width === width
109 ) {
110 return state;
111 }
112 return {
113 byThreadKey: {
114 ...state.byThreadKey,
115 [threadKey]: { ...current, width },
116 },
117 };
118 }),
119 removeThread: (ref) =>
120 set((state) => {
121 const threadKey = scopedThreadKey(ref);
122 if (!(threadKey in state.byThreadKey)) return state;
123 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
124 return { byThreadKey };
125 }),
126}));
127
128export function selectThreadPreviewMiniPlayer(
129 byThreadKey: Record<string, PreviewMiniPlayerState>,
130 ref: ScopedThreadRef | null | undefined,
131): PreviewMiniPlayerState | null {
132 if (!ref) return null;
133 return byThreadKey[scopedThreadKey(ref)] ?? null;
134}
135
136/** The floating browser tab, or null when nothing floats or a device does. */
137export function selectThreadPreviewMiniPlayerTabId(
138 byThreadKey: Record<string, PreviewMiniPlayerState>,
139 ref: ScopedThreadRef | null | undefined,
140): string | null {
141 const source = selectThreadPreviewMiniPlayer(byThreadKey, ref)?.source;
142 return source?.kind === "browser" ? source.tabId : null;
143}