Open History to see the last commit
1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import type { 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 interface PreviewMiniPlayerState {
16 readonly tabId: string;
17 readonly position: PreviewMiniPlayerPosition | null;
18 /** Height always follows the previewed viewport's aspect ratio. */
19 readonly width: number | null;
20}
21
22interface PreviewMiniPlayerStoreState {
23 readonly byThreadKey: Record<string, PreviewMiniPlayerState>;
24 readonly open: (ref: ScopedThreadRef, tabId: string) => void;
25 readonly close: (ref: ScopedThreadRef) => void;
26 readonly move: (ref: ScopedThreadRef, tabId: string, position: PreviewMiniPlayerPosition) => void;
27 readonly resize: (ref: ScopedThreadRef, tabId: string, width: number) => void;
28 readonly removeThread: (ref: ScopedThreadRef) => void;
29}
30
31export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((set) => ({
32 byThreadKey: {},
33 open: (ref, tabId) =>
34 set((state) => {
35 const threadKey = scopedThreadKey(ref);
36 const current = state.byThreadKey[threadKey];
37 if (current?.tabId === tabId) return state;
38 return {
39 byThreadKey: {
40 ...state.byThreadKey,
41 [threadKey]: {
42 tabId,
43 position: current?.position ?? null,
44 width: current?.width ?? null,
45 },
46 },
47 };
48 }),
49 close: (ref) =>
50 set((state) => {
51 const threadKey = scopedThreadKey(ref);
52 if (!(threadKey in state.byThreadKey)) return state;
53 const { [threadKey]: _closed, ...byThreadKey } = state.byThreadKey;
54 return { byThreadKey };
55 }),
56 move: (ref, tabId, position) =>
57 set((state) => {
58 const threadKey = scopedThreadKey(ref);
59 const current = state.byThreadKey[threadKey];
60 if (!current || current.tabId !== tabId) return state;
61 if (current.position?.x === position.x && current.position.y === position.y) return state;
62 return {
63 byThreadKey: {
64 ...state.byThreadKey,
65 [threadKey]: { ...current, position },
66 },
67 };
68 }),
69 resize: (ref, tabId, width) =>
70 set((state) => {
71 const threadKey = scopedThreadKey(ref);
72 const current = state.byThreadKey[threadKey];
73 if (!current || current.tabId !== tabId || current.width === width) return state;
74 return {
75 byThreadKey: {
76 ...state.byThreadKey,
77 [threadKey]: { ...current, width },
78 },
79 };
80 }),
81 removeThread: (ref) =>
82 set((state) => {
83 const threadKey = scopedThreadKey(ref);
84 if (!(threadKey in state.byThreadKey)) return state;
85 const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
86 return { byThreadKey };
87 }),
88}));
89
90export function selectThreadPreviewMiniPlayer(
91 byThreadKey: Record<string, PreviewMiniPlayerState>,
92 ref: ScopedThreadRef | null | undefined,
93): PreviewMiniPlayerState | null {
94 if (!ref) return null;
95 return byThreadKey[scopedThreadKey(ref)] ?? null;
96}