feat(devices): scope targets and sessions to their hosts (#10854)
This commit is contained in:
1 parent
dca7b59bea
commit
e022fa430e
19 files changed
+649
-257
No files matched your search
@@ -32,15 +32,17 @@ const RIGHT_PANEL_KINDS = [
|
||||
] as const;
|
||||
export type RightPanelKind = (typeof RIGHT_PANEL_KINDS)[number];
|
||||
|
||||
export interface DeviceTabTarget {
|
||||
hostId: string;
|
||||
deviceId: string;
|
||||
platform: "ios" | "android";
|
||||
name: string;
|
||||
}
|
||||
|
||||
export type RightPanelSurface =
|
||||
| { id: `browser:${string}`; kind: "preview"; resourceId: string }
|
||||
| { id: "browser:new"; kind: "preview"; resourceId: null }
|
||||
/**
|
||||
* One Device tab per thread. The tab is the surface; which device it shows
|
||||
* comes from the thread's server-side device sessions, so an agent opening a
|
||||
* device from another client lands in the same tab.
|
||||
*/
|
||||
| { id: "device"; kind: "device" }
|
||||
| { id: "device" | `device:${string}`; kind: "device"; target?: DeviceTabTarget; title?: string }
|
||||
| {
|
||||
id: `terminal:${string}`;
|
||||
kind: "terminal";
|
||||
@@ -90,7 +92,7 @@ const RIGHT_PANEL_STORAGE_KEY = "t3code:right-panel-state:v2";
|
||||
// v10 keys pull-request surfaces by reference instead of a singleton tab.
|
||||
// v11 stops persisting the pull-request list's shared panel, so a restart opens the page fresh.
|
||||
// v12 adds the device surface.
|
||||
const RIGHT_PANEL_STORAGE_VERSION = 12;
|
||||
const RIGHT_PANEL_STORAGE_VERSION = 13;
|
||||
|
||||
/** A fixed workspace-level ref: each PR surface carries its own real environment. */
|
||||
export const PULL_REQUESTS_PANEL_REF = scopeThreadRef(
|
||||
@@ -108,6 +110,7 @@ export interface ThreadRightPanelState {
|
||||
isOpen: boolean;
|
||||
activeSurfaceId: string | null;
|
||||
surfaces: RightPanelSurface[];
|
||||
dismissedDeviceSurfaceIds?: string[];
|
||||
}
|
||||
|
||||
interface RightPanelStoreState {
|
||||
@@ -128,6 +131,8 @@ interface RightPanelStoreState {
|
||||
ref: ScopedThreadRef,
|
||||
kind: Exclude<RightPanelKind, "file" | "terminal" | "pull-request">,
|
||||
) => void;
|
||||
openDevice: (ref: ScopedThreadRef, target: DeviceTabTarget, automatic?: boolean) => void;
|
||||
renameDevice: (ref: ScopedThreadRef, surfaceId: string, title: string) => void;
|
||||
openBrowser: (ref: ScopedThreadRef, tabId: string | null) => void;
|
||||
openFile: (ref: ScopedThreadRef, relativePath: string, line?: number) => void;
|
||||
openAttachment: (ref: ScopedThreadRef, attachment: ChatFileAttachment) => void;
|
||||
@@ -281,7 +286,12 @@ const updateThread = (
|
||||
): Record<string, ThreadRightPanelState> => {
|
||||
const current = byThreadKey[threadKey] ?? EMPTY_THREAD_STATE;
|
||||
const next = updater(current);
|
||||
if (!next.isOpen && next.activeSurfaceId === null && next.surfaces.length === 0) {
|
||||
if (
|
||||
!next.isOpen &&
|
||||
next.activeSurfaceId === null &&
|
||||
next.surfaces.length === 0 &&
|
||||
!next.dismissedDeviceSurfaceIds?.length
|
||||
) {
|
||||
if (!(threadKey in byThreadKey)) return byThreadKey;
|
||||
const { [threadKey]: _removed, ...rest } = byThreadKey;
|
||||
return rest;
|
||||
@@ -306,7 +316,25 @@ const userAction = (
|
||||
threadKey: string,
|
||||
updater: (current: ThreadRightPanelState) => ThreadRightPanelState,
|
||||
): Partial<RightPanelStoreState> => ({
|
||||
byThreadKey: updateThread(state.byThreadKey, threadKey, updater),
|
||||
byThreadKey: updateThread(state.byThreadKey, threadKey, (current) => {
|
||||
const next = updater(current);
|
||||
const removed = current.surfaces.filter(
|
||||
(surface) =>
|
||||
surface.kind === "device" &&
|
||||
surface.target &&
|
||||
!next.surfaces.some((entry) => entry.id === surface.id),
|
||||
);
|
||||
if (removed.length === 0) return next;
|
||||
return {
|
||||
...next,
|
||||
dismissedDeviceSurfaceIds: [
|
||||
...new Set([
|
||||
...(next.dismissedDeviceSurfaceIds ?? []),
|
||||
...removed.map((surface) => surface.id),
|
||||
]),
|
||||
],
|
||||
};
|
||||
}),
|
||||
userActionRevisionByThreadKey: {
|
||||
...state.userActionRevisionByThreadKey,
|
||||
[threadKey]: (state.userActionRevisionByThreadKey[threadKey] ?? 0) + 1,
|
||||
@@ -426,7 +454,22 @@ export function migratePersistedRightPanelState(persistedState: unknown): {
|
||||
// first survivor instead of rendering an open empty panel.
|
||||
const activeSurfaceId =
|
||||
persistedActiveSurfaceId ?? (isOpen ? (surfaces[0]?.id ?? null) : null);
|
||||
return [threadKey, { isOpen, surfaces, activeSurfaceId }];
|
||||
return [
|
||||
threadKey,
|
||||
{
|
||||
isOpen,
|
||||
surfaces,
|
||||
activeSurfaceId,
|
||||
...(Array.isArray(validThreadState?.dismissedDeviceSurfaceIds)
|
||||
? {
|
||||
dismissedDeviceSurfaceIds:
|
||||
validThreadState.dismissedDeviceSurfaceIds.filter(
|
||||
(id): id is string => typeof id === "string",
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
];
|
||||
}),
|
||||
)
|
||||
: {};
|
||||
@@ -472,6 +515,40 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
|
||||
return upsertSurface(current, singletonSurface(kind));
|
||||
}),
|
||||
),
|
||||
openDevice: (ref, target, automatic = false) =>
|
||||
set((state) =>
|
||||
(automatic ? automaticUpdate : userAction)(state, scopedThreadKey(ref), (current) => {
|
||||
const id =
|
||||
`device:${encodeURIComponent(target.hostId)}:${encodeURIComponent(target.deviceId)}` as const;
|
||||
if (automatic && current.dismissedDeviceSurfaceIds?.includes(id)) return current;
|
||||
const surface: RightPanelSurface = { id, kind: "device", target };
|
||||
const existing = current.surfaces.find((entry) => entry.id === id);
|
||||
const surfaces = existing
|
||||
? current.surfaces.filter((entry) => entry.id !== "device")
|
||||
: current.surfaces.map((entry) => (entry.id === "device" ? surface : entry));
|
||||
return upsertSurface(
|
||||
{
|
||||
...current,
|
||||
surfaces,
|
||||
dismissedDeviceSurfaceIds: (current.dismissedDeviceSurfaceIds ?? []).filter(
|
||||
(entry) => entry !== id,
|
||||
),
|
||||
},
|
||||
existing ?? surface,
|
||||
);
|
||||
}),
|
||||
),
|
||||
renameDevice: (ref, surfaceId, title) =>
|
||||
set((state) =>
|
||||
userAction(state, scopedThreadKey(ref), (current) => ({
|
||||
...current,
|
||||
surfaces: current.surfaces.map((surface) =>
|
||||
surface.id === surfaceId && surface.kind === "device"
|
||||
? { ...surface, title: title.trim() || surface.target?.name || "Device" }
|
||||
: surface,
|
||||
),
|
||||
})),
|
||||
),
|
||||
openBrowser: (ref, tabId) =>
|
||||
set((state) =>
|
||||
userAction(state, scopedThreadKey(ref), (current) => {
|
||||
|
||||
Reference in new issue
Block a user