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 readonly size: PreviewMiniPlayerSize | null;
19}
20
21interface PreviewMiniPlayerStoreState {
22 readonly byThreadKey: Record<string, PreviewMiniPlayerState>;
23 readonly open: (ref: ScopedThreadRef, tabId: string) => void;
24 readonly close: (ref: ScopedThreadRef) => void;
25 readonly move: (ref: ScopedThreadRef, tabId: string, position: PreviewMiniPlayerPosition) => void;
26 readonly resize: (ref: ScopedThreadRef, tabId: string, size: PreviewMiniPlayerSize) => void;
27 readonly removeThread: (ref: ScopedThreadRef) => void;
28}
29
30export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((set) => ({
31 byThreadKey: {},
32 open: (ref, tabId) =>
33 set((state) => {
34 const threadKey = scopedThreadKey(ref);
35 const current = state.byThreadKey[threadKey];
36 if (current?.tabId === tabId) return state;
37 return {
38 byThreadKey: {
39 ...state.byThreadKey,
40 [threadKey]: {
41 tabId,
42 position: current?.position ?? null,
43 size: current?.size ?? null,
44 },
45 },
46 };
47 }),
48 close: (ref) =>
49 set((state) => {
50 const threadKey = scopedThreadKey(ref);
51 if (!(threadKey in state.byThreadKey)) return state;
52 const { [threadKey]: _closed, ...byThreadKey } = state.byThreadKey;
53 return { byThreadKey };
54 }),
55 move: (ref, tabId, position) =>
56 set((state) => {
57 const threadKey = scopedThreadKey(ref);
58 const current = state.byThreadKey[threadKey];
59 if (!current || current.tabId !== tabId) return state;
60 if (current.position?.x === position.x && current.position.y === position.y) return state;
61 return {
62 byThreadKey: {
63 ...state.byThreadKey,
64 [threadKey]: { ...current, position },
65 },
66 };
67 }),
68 resize: (ref, tabId, size) =>
69 set((state) => {
70 const threadKey = scopedThreadKey(ref);
71 const current = state.byThreadKey[threadKey];
72 if (!current || current.tabId !== tabId) return state;
73 if (current.size?.width === size.width && current.size.height === size.height) return state;
74 return {
75 byThreadKey: {
76 ...state.byThreadKey,
77 [threadKey]: { ...current, size },
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}