feat(web): float device streams over chat (#11285)

Co-authored-by: Claude Code <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Code authored and GitHub committed 2026-09-11 14:07:40 -07:00
1 parent e22040dfc1
commit 8bbe2bf660
15 files changed
+612 -167

No files matched your search

+60 -13
View File
@@ -1,5 +1,5 @@
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
import type { ScopedThreadRef } from "@t3tools/contracts";
import type { DevicePlatform, ScopedThreadRef } from "@t3tools/contracts";
import { create } from "zustand";
export interface PreviewMiniPlayerPosition {
@@ -12,34 +12,66 @@ export interface PreviewMiniPlayerSize {
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 tabId: string;
readonly source: PreviewMiniPlayerSource;
readonly position: PreviewMiniPlayerPosition | null;
/** Height always follows the previewed viewport's aspect ratio. */
/** Height always follows the mirrored source's aspect ratio. */
readonly width: number | null;
}
interface PreviewMiniPlayerStoreState {
readonly byThreadKey: Record<string, PreviewMiniPlayerState>;
readonly open: (ref: ScopedThreadRef, tabId: string) => void;
readonly open: (ref: ScopedThreadRef, source: PreviewMiniPlayerSource) => void;
readonly close: (ref: ScopedThreadRef) => void;
readonly move: (ref: ScopedThreadRef, tabId: string, position: PreviewMiniPlayerPosition) => void;
readonly resize: (ref: ScopedThreadRef, tabId: string, width: number) => 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) => 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, tabId) =>
open: (ref, source) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const current = state.byThreadKey[threadKey];
if (current?.tabId === tabId) return state;
if (
current &&
previewMiniPlayerSourceKey(current.source) === previewMiniPlayerSourceKey(source)
) {
return state;
}
return {
byThreadKey: {
...state.byThreadKey,
[threadKey]: {
tabId,
source,
position: current?.position ?? null,
width: current?.width ?? null,
},
@@ -53,11 +85,11 @@ export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((
const { [threadKey]: _closed, ...byThreadKey } = state.byThreadKey;
return { byThreadKey };
}),
move: (ref, tabId, position) =>
move: (ref, sourceKey, position) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const current = state.byThreadKey[threadKey];
if (!current || current.tabId !== tabId) return state;
if (!current || previewMiniPlayerSourceKey(current.source) !== sourceKey) return state;
if (current.position?.x === position.x && current.position.y === position.y) return state;
return {
byThreadKey: {
@@ -66,11 +98,17 @@ export const usePreviewMiniPlayerStore = create<PreviewMiniPlayerStoreState>()((
},
};
}),
resize: (ref, tabId, width) =>
resize: (ref, sourceKey, width) =>
set((state) => {
const threadKey = scopedThreadKey(ref);
const current = state.byThreadKey[threadKey];
if (!current || current.tabId !== tabId || current.width === width) return state;
if (
!current ||
previewMiniPlayerSourceKey(current.source) !== sourceKey ||
current.width === width
) {
return state;
}
return {
byThreadKey: {
...state.byThreadKey,
@@ -94,3 +132,12 @@ export function selectThreadPreviewMiniPlayer(
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;
}