Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useRightPanelSurfaceClose.ts

147 lines5.8 KB

1import {
2 AuthPreviewOperateScope,
3 AuthTerminalOperateScope,
4 type ScopedThreadRef,
5} from "@t3tools/contracts";
6import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
7import { agentControlledBrowserCloseConfirmation } from "../components/ChatView.logic";
8import { closePreviewSession } from "../components/preview/closePreviewSession";
9import { useThreadTerminalActions } from "../components/terminal/useThreadTerminalActions";
10import { confirmTerminalClose } from "../lib/terminalCloseConfirm";
11import { readLocalApi } from "../localApi";
12import { setActivePreviewTab, useThreadPreviewState } from "../previewStateStore";
13import {
14 selectActiveRightPanelSurface,
15 selectThreadRightPanelState,
16 useRightPanelStore,
17 type RightPanelSurface,
18} from "../rightPanelStore";
19import { previewEnvironment } from "../state/preview";
20import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
21import { useAtomCommand } from "../state/use-atom-command";
22import { useTerminalUiStateStore } from "../terminalUiStateStore";
23import { useStableCallback } from "./useStableCallback";
24​
25/**
26 * Closes right panel surfaces for a thread. A surface's live resources go with
27 * it: a browser's preview session and a terminal tab's sessions. Closing a
28 * browser an agent is driving, or a terminal tab, asks first.
29 */
30export function useRightPanelSurfaceClose(input: {
31 readonly threadRef: ScopedThreadRef | null;
32 readonly terminalLabelsById: ReadonlyMap<string, string>;
33}) {
34 const { threadRef, terminalLabelsById } = input;
35 const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
36 const canOperatePreview = useEnvironmentScope(
37 threadRef?.environmentId ?? null,
38 AuthPreviewOperateScope,
39 );
40 const previewState = useThreadPreviewState(threadRef);
41 const terminalActions = useThreadTerminalActions(threadRef);
42 const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal);
43 const surfaces = useRightPanelStore(
44 (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
45 );
46​
47 const releaseSurfaceResources = (ref: ScopedThreadRef, surface: RightPanelSurface) => {
48 // Without preview access only the local surface goes away. The
49 // server record is in-memory bookkeeping, not a live view, and stays
50 // listed until a client with the scope closes it or the server restarts.
51 if (canOperatePreview && surface.kind === "preview" && surface.resourceId) {
52 void closePreviewSession({
53 closePreview,
54 snapshot: previewState.sessions[surface.resourceId] ?? null,
55 tabId: surface.resourceId,
56 threadRef: ref,
57 });
58 }
59 if (
60 surface.kind === "terminal" &&
61 readEnvironmentScope(ref.environmentId, AuthTerminalOperateScope)
62 ) {
63 for (const terminalId of surface.terminalIds) {
64 storeCloseTerminal(ref, terminalId);
65 terminalActions.closeTerminalSession(terminalId);
66 }
67 }
68 };
69 const closeAfterAgentBrowserConfirmation = (
70 toClose: readonly RightPanelSurface[],
71 closeSurfaces: () => void,
72 ) => {
73 const message = agentControlledBrowserCloseConfirmation(toClose, previewState.desktopByTabId);
74 if (!message) {
75 closeSurfaces();
76 return;
77 }
78 const localApi = readLocalApi();
79 if (!localApi) return;
80 void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
81 (confirmed) => {
82 if (confirmed) closeSurfaces();
83 },
84 () => undefined,
85 );
86 };
87 // The active surface closes last so the panel does not flash through it.
88 const finishClose = (toClose: readonly RightPanelSurface[]) => {
89 if (!threadRef) return;
90 const store = useRightPanelStore.getState();
91 const activeId = selectThreadRightPanelState(store.byThreadKey, threadRef).activeSurfaceId;
92 const ordered = toClose.toSorted(
93 (left, right) => Number(left.id === activeId) - Number(right.id === activeId),
94 );
95 for (const surface of ordered) {
96 releaseSurfaceResources(threadRef, surface);
97 store.closeSurface(threadRef, surface.id);
98 }
99 const nextActiveSurface = selectActiveRightPanelSurface(
100 useRightPanelStore.getState().byThreadKey,
101 threadRef,
102 );
103 if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
104 setActivePreviewTab(threadRef, nextActiveSurface.resourceId);
105 }
106 };
107 const labelFor = (terminalId: string) =>
108 terminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
109 const closeSurfaces = (toClose: readonly RightPanelSurface[]) => {
110 if (!threadRef) return;
111 closeAfterAgentBrowserConfirmation(toClose, () => finishClose(toClose));
112 };
113​
114 const closeSurface = useStableCallback((surface: RightPanelSurface) => {
115 if (!threadRef) return;
116 if (surface.kind === "preview") {
117 closeSurfaces([surface]);
118 return;
119 }
120 if (
121 surface.kind !== "terminal" ||
122 !readEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope)
123 ) {
124 finishClose([surface]);
125 return;
126 }
127 const otherLabels = surface.terminalIds
128 .filter((terminalId) => terminalId !== surface.activeTerminalId)
129 .map(labelFor);
130 void confirmTerminalClose([labelFor(surface.activeTerminalId), ...otherLabels]).then(
131 (confirmed) => {
132 if (confirmed) finishClose([surface]);
133 },
134 );
135 });
136 const closeOtherSurfaces = useStableCallback((surface: RightPanelSurface) =>
137 closeSurfaces(surfaces.filter((entry) => entry.id !== surface.id)),
138 );
139 const closeSurfacesToRight = useStableCallback((surface: RightPanelSurface) => {
140 const surfaceIndex = surfaces.findIndex((entry) => entry.id === surface.id);
141 if (surfaceIndex < 0) return;
142 closeSurfaces(surfaces.slice(surfaceIndex + 1));
143 });
144 const closeAllSurfaces = useStableCallback(() => closeSurfaces(surfaces));
145​
146 return { closeSurface, closeOtherSurfaces, closeSurfacesToRight, closeAllSurfaces };