@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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