Files
t3code/apps/web/src/previewMiniPlayerStore.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

163 lines
5.2 KiB
TypeScript

import { scopedThreadKey } from "@t3tools/client-runtime/environment";
import type { DevicePlatform, ScopedThreadRef } from "@t3tools/contracts";
import { create } from "zustand";
export interface PreviewMiniPlayerPosition {
readonly x: number;
readonly y: number;
}
export interface PreviewMiniPlayerSize {
readonly width: number;
readonly height: number;
}
/** What the floating player mirrors: a browser tab or a device stream. */
export type PreviewMiniPlayerSource =
| { readonly kind: "browser"; readonly tabId: string }
| {
readonly kind: "device";
readonly hostId: string;
readonly deviceId: string;
readonly platform: DevicePlatform;
readonly name: string;
};
export interface PreviewMiniPlayerState {
readonly source: PreviewMiniPlayerSource;
readonly position: PreviewMiniPlayerPosition | null;
/** Height always follows the mirrored source's aspect ratio. */
readonly width: number | null;
readonly lastInteraction: "drag" | "resize";
}
interface PreviewMiniPlayerStoreState {
readonly byThreadKey: Record<string, PreviewMiniPlayerState>;
readonly open: (ref: ScopedThreadRef, source: PreviewMiniPlayerSource) => void;
readonly close: (ref: ScopedThreadRef) => void;
/** `sourceKey` guards against a drag that outlives the source it started on. */
readonly move: (
ref: ScopedThreadRef,
sourceKey: string,
position: PreviewMiniPlayerPosition,
) => void;
readonly resize: (
ref: ScopedThreadRef,
sourceKey: string,
width: number,
position?: PreviewMiniPlayerPosition,
) => void;
readonly removeThread: (ref: ScopedThreadRef) => void;
}
export function previewMiniPlayerSourceKey(source: PreviewMiniPlayerSource): string {
return source.kind === "browser"
? `browser:${source.tabId}`
: `device:${encodeURIComponent(source.hostId)}:${encodeURIComponent(source.deviceId)}`;
}
export const browserMiniPlayerSource = (tabId: string): PreviewMiniPlayerSource => ({
kind: "browser",
tabId,
});
export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((set) => ({
byThreadKey: {},
open: (ref, source) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const current = state.byThreadKey[threadKey];
if (
current &&
previewMiniPlayerSourceKey(current.source) === previewMiniPlayerSourceKey(source)
) {
return state;
}
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: {
source,
position: current?.position ?? null,
width: current?.width ?? null,
lastInteraction: current?.lastInteraction ?? "drag",
},
},
};
}),
close: (ref) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
if (!(threadKey in state.byThreadKey)) return state;
const { [threadKey]: _closed, ...byThreadKey } = state.byThreadKey;
return { byThreadKey };
}),
move: (ref, sourceKey, position) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const current = state.byThreadKey[threadKey];
if (!current || previewMiniPlayerSourceKey(current.source) !== sourceKey) return state;
if (
current.position?.x === position.x &&
current.position.y === position.y &&
current.lastInteraction === "drag"
)
return state;
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: { ...current, position, lastInteraction: "drag" },
},
};
}),
resize: (ref, sourceKey, width, position) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const current = state.byThreadKey[threadKey];
if (
!current ||
previewMiniPlayerSourceKey(current.source) !== sourceKey ||
(current.width === width &&
current.lastInteraction === "resize" &&
(!position || (current.position?.x === position.x && current.position.y === position.y)))
) {
return state;
}
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: {
...current,
width,
position: position ?? current.position,
lastInteraction: "resize",
},
},
};
}),
removeThread: (ref) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
if (!(threadKey in state.byThreadKey)) return state;
const { [threadKey]: _removed, ...byThreadKey } = state.byThreadKey;
return { byThreadKey };
}),
}));
export function selectThreadPreviewMiniPlayer(
byThreadKey: Record<string, PreviewMiniPlayerState>,
ref: ScopedThreadRef | null | undefined,
): PreviewMiniPlayerState | null {
if (!ref) return null;
return byThreadKey[scopedThreadKey(ref)] ?? null;
}
/** The floating browser tab, or null when nothing floats or a device does. */
export function selectThreadPreviewMiniPlayerTabId(
byThreadKey: Record<string, PreviewMiniPlayerState>,
ref: ScopedThreadRef | null | undefined,
): string | null {
const source = selectThreadPreviewMiniPlayer(byThreadKey, ref)?.source;
return source?.kind === "browser" ? source.tabId : null;
}