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

useRightPanelSurfaceClose.ts

141 lines5.6 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/** Closing releases preview and terminal sessions. Agent-driven browsers and terminal tabs require confirmation. */
26export function useRightPanelSurfaceClose(input: {
27 readonly threadRef: ScopedThreadRef | null;
28 readonly terminalLabelsById: ReadonlyMap<string, string>;
29}) {
30 const { threadRef, terminalLabelsById } = input;
31 const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
32 const canOperatePreview = useEnvironmentScope(
33 threadRef?.environmentId ?? null,
34 AuthPreviewOperateScope,
35 );
36 const previewState = useThreadPreviewState(threadRef);
37 const terminalActions = useThreadTerminalActions(threadRef);
38 const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal);
39 const surfaces = useRightPanelStore(
40 (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
41 );
42​
43 const releaseSurfaceResources = (ref: ScopedThreadRef, surface: RightPanelSurface) => {
44 // Without preview access, server state remains until an authorized close or restart.
45 if (canOperatePreview && surface.kind === "preview" && surface.resourceId) {
46 void closePreviewSession({
47 closePreview,
48 snapshot: previewState.sessions[surface.resourceId] ?? null,
49 tabId: surface.resourceId,
50 threadRef: ref,
51 });
52 }
53 if (
54 surface.kind === "terminal" &&
55 readEnvironmentScope(ref.environmentId, AuthTerminalOperateScope)
56 ) {
57 for (const terminalId of surface.terminalIds) {
58 storeCloseTerminal(ref, terminalId);
59 terminalActions.closeTerminalSession(terminalId);
60 }
61 }
62 };
63 const closeAfterAgentBrowserConfirmation = (
64 toClose: readonly RightPanelSurface[],
65 closeSurfaces: () => void,
66 ) => {
67 const message = agentControlledBrowserCloseConfirmation(toClose, previewState.desktopByTabId);
68 if (!message) {
69 closeSurfaces();
70 return;
71 }
72 const localApi = readLocalApi();
73 if (!localApi) return;
74 void localApi.dialogs.confirm(message, { variant: "destructive" }).then(
75 (confirmed) => {
76 if (confirmed) closeSurfaces();
77 },
78 () => undefined,
79 );
80 };
81 // The active surface closes last so the panel does not flash through it.
82 const finishClose = (toClose: readonly RightPanelSurface[]) => {
83 if (!threadRef) return;
84 const store = useRightPanelStore.getState();
85 const activeId = selectThreadRightPanelState(store.byThreadKey, threadRef).activeSurfaceId;
86 const ordered = toClose.toSorted(
87 (left, right) => Number(left.id === activeId) - Number(right.id === activeId),
88 );
89 for (const surface of ordered) {
90 releaseSurfaceResources(threadRef, surface);
91 store.closeSurface(threadRef, surface.id);
92 }
93 const nextActiveSurface = selectActiveRightPanelSurface(
94 useRightPanelStore.getState().byThreadKey,
95 threadRef,
96 );
97 if (nextActiveSurface?.kind === "preview" && nextActiveSurface.resourceId) {
98 setActivePreviewTab(threadRef, nextActiveSurface.resourceId);
99 }
100 };
101 const labelFor = (terminalId: string) =>
102 terminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
103 const closeSurfaces = (toClose: readonly RightPanelSurface[]) => {
104 if (!threadRef) return;
105 closeAfterAgentBrowserConfirmation(toClose, () => finishClose(toClose));
106 };
107​
108 const closeSurface = useStableCallback((surface: RightPanelSurface) => {
109 if (!threadRef) return;
110 if (surface.kind === "preview") {
111 closeSurfaces([surface]);
112 return;
113 }
114 if (
115 surface.kind !== "terminal" ||
116 !readEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope)
117 ) {
118 finishClose([surface]);
119 return;
120 }
121 const otherLabels = surface.terminalIds
122 .filter((terminalId) => terminalId !== surface.activeTerminalId)
123 .map(labelFor);
124 void confirmTerminalClose([labelFor(surface.activeTerminalId), ...otherLabels]).then(
125 (confirmed) => {
126 if (confirmed) finishClose([surface]);
127 },
128 );
129 });
130 const closeOtherSurfaces = useStableCallback((surface: RightPanelSurface) =>
131 closeSurfaces(surfaces.filter((entry) => entry.id !== surface.id)),
132 );
133 const closeSurfacesToRight = useStableCallback((surface: RightPanelSurface) => {
134 const surfaceIndex = surfaces.findIndex((entry) => entry.id === surface.id);
135 if (surfaceIndex < 0) return;
136 closeSurfaces(surfaces.slice(surfaceIndex + 1));
137 });
138 const closeAllSurfaces = useStableCallback(() => closeSurfaces(surfaces));
139​
140 return { closeSurface, closeOtherSurfaces, closeSurfacesToRight, closeAllSurfaces };