Open History to see the last commit
1import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import {
3 parseScopedThreadKey,
4 scopedThreadKey,
5 scopeThreadRef,
6} from "@t3tools/client-runtime/environment";
7import { useState } from "react";
8import { useShallow } from "zustand/react/shallow";
9import { useComposerDraftStore } from "../../composerDraftStore";
10import type { TerminalContextSelection } from "../../lib/terminalContext";
11import { usePanelAnimationSettings, usePanelPresence } from "../../panelAnimations";
12import { useThreadRefs } from "../../state/entities";
13import { useTerminalUiStateStore } from "../../terminalUiStateStore";
14import {
15 MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
16 reconcileMountedTerminalThreadIds,
17} from "../ChatView.logic";
18import {
19 PersistentThreadTerminalDrawer,
20 type PersistentTerminalLaunchContext,
21} from "../PersistentThreadTerminals";
22
23interface ChatTerminalDrawersProps {
24 readonly activeThreadKey: string | null;
25 readonly activeTerminalOpen: boolean;
26 readonly composerHeight: number;
27 readonly launchContext: PersistentTerminalLaunchContext | null;
28 readonly focusRequestId: number;
29 readonly splitShortcutLabel: string | undefined;
30 readonly splitVerticalShortcutLabel: string | undefined;
31 readonly newShortcutLabel: string | undefined;
32 readonly closeShortcutLabel: string | undefined;
33 readonly keybindings: ResolvedKeybindingsConfig;
34 readonly onAddTerminalContext: (selection: TerminalContextSelection) => void;
35}
36
37/**
38 * Keeps terminal drawers mounted for recently open threads, so switching back
39 * to one finds its sessions as they were. Every thread's terminal and draft
40 * state feeds the reconcile, so it subscribes here instead of in ChatView.
41 */
42export function ChatTerminalDrawers(props: ChatTerminalDrawersProps) {
43 const { activeThreadKey } = props;
44 const openTerminalThreadKeys = useTerminalUiStateStore(
45 useShallow((state) =>
46 Object.entries(state.terminalUiStateByThreadKey).flatMap(
47 ([nextThreadKey, nextTerminalUiState]) =>
48 nextTerminalUiState.terminalOpen ? [nextThreadKey] : [],
49 ),
50 ),
51 );
52 const serverThreadRefs = useThreadRefs();
53 const serverThreadKeys = serverThreadRefs.map(scopedThreadKey);
54 const draftThreadsByThreadKey = useComposerDraftStore((store) => store.draftThreadsByThreadKey);
55 const draftThreadKeys = Object.values(draftThreadsByThreadKey).map((draftThread) =>
56 scopedThreadKey(scopeThreadRef(draftThread.environmentId, draftThread.threadId)),
57 );
58 const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
59 usePanelAnimationSettings();
60 const activeTerminalDrawerPresence = usePanelPresence(
61 Boolean(activeThreadKey && props.activeTerminalOpen),
62 true,
63 panelAnimationsActive,
64 activeThreadKey,
65 panelAnimationDurationMs,
66 );
67 const [mountedTerminalThreadKeys, setMountedTerminalThreadKeys] = useState<string[]>([]);
68 const existingOpenTerminalThreadKeys = (() => {
69 const existingThreadKeys = new Set<string>([...serverThreadKeys, ...draftThreadKeys]);
70 return openTerminalThreadKeys.filter((nextThreadKey) => existingThreadKeys.has(nextThreadKey));
71 })();
72 const reconciledMountedTerminalThreadKeys = reconcileMountedTerminalThreadIds({
73 currentThreadIds: mountedTerminalThreadKeys,
74 openThreadIds: existingOpenTerminalThreadKeys,
75 activeThreadId: activeThreadKey,
76 activeThreadTerminalOpen: activeTerminalDrawerPresence.present,
77 maxHiddenThreadCount: MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
78 });
79 if (
80 reconciledMountedTerminalThreadKeys.length !== mountedTerminalThreadKeys.length ||
81 reconciledMountedTerminalThreadKeys.some(
82 (threadKey, index) => threadKey !== mountedTerminalThreadKeys[index],
83 )
84 ) {
85 setMountedTerminalThreadKeys(reconciledMountedTerminalThreadKeys);
86 }
87 const mountedTerminalThreadRefs = mountedTerminalThreadKeys.flatMap((mountedThreadKey) => {
88 const mountedThreadRef = parseScopedThreadKey(mountedThreadKey);
89 return mountedThreadRef ? [{ key: mountedThreadKey, threadRef: mountedThreadRef }] : [];
90 });
91
92 return mountedTerminalThreadRefs.map(({ key: mountedThreadKey, threadRef: mountedThreadRef }) => (
93 <PersistentThreadTerminalDrawer
94 key={mountedThreadKey}
95 threadRef={mountedThreadRef}
96 threadId={mountedThreadRef.threadId}
97 active={mountedThreadKey === activeThreadKey}
98 composerHeight={mountedThreadKey === activeThreadKey ? props.composerHeight : 0}
99 launchContext={mountedThreadKey === activeThreadKey ? props.launchContext : null}
100 focusRequestId={mountedThreadKey === activeThreadKey ? props.focusRequestId : 0}
101 splitShortcutLabel={props.splitShortcutLabel}
102 splitVerticalShortcutLabel={props.splitVerticalShortcutLabel}
103 newShortcutLabel={props.newShortcutLabel}
104 closeShortcutLabel={props.closeShortcutLabel}
105 keybindings={props.keybindings}
106 onAddTerminalContext={props.onAddTerminalContext}
107 />
108 ));
109}