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

terminalLaunchContext.ts

120 lines4.1 KB

1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { useEffect, useState } from "react";
3​
4interface TerminalLocationLike {
5 readonly cwd: string;
6 readonly worktreePath: string | null;
7}
8​
9interface PendingTerminalLaunchTarget {
10 readonly environmentId: EnvironmentId;
11 readonly threadId: ThreadId;
12 readonly terminalId: string;
14​
15export interface PendingTerminalLaunch {
16 readonly cwd: string;
17 readonly worktreePath: string | null;
18 readonly env?: Record<string, string>;
19 readonly initialInput?: string;
21​
22const pendingTerminalLaunches = new Map<string, PendingTerminalLaunch>();
23​
24function pendingTerminalLaunchKey(input: PendingTerminalLaunchTarget): string {
25 return `${input.environmentId}:${input.threadId}:${input.terminalId}`;
27​
28export function stagePendingTerminalLaunch(input: {
29 readonly target: PendingTerminalLaunchTarget;
30 readonly launch: PendingTerminalLaunch;
31}) {
32 pendingTerminalLaunches.set(pendingTerminalLaunchKey(input.target), {
33 cwd: input.launch.cwd,
34 worktreePath: input.launch.worktreePath,
35 env: input.launch.env ? { ...input.launch.env } : undefined,
36 initialInput: input.launch.initialInput,
37 });
39​
40export function takePendingTerminalLaunch(
41 target: PendingTerminalLaunchTarget,
42): PendingTerminalLaunch | null {
43 const key = pendingTerminalLaunchKey(target);
44 const launch = pendingTerminalLaunches.get(key) ?? null;
45 if (launch) {
46 pendingTerminalLaunches.delete(key);
47 }
48​
49 return launch;
51​
52export function resolvePreferredThreadWorktreePath(input: {
53 readonly threadShellWorktreePath: string | null;
54 readonly threadDetailWorktreePath: string | null;
55}): string | null {
56 return input.threadDetailWorktreePath ?? input.threadShellWorktreePath ?? null;
58​
59export function resolveTerminalOpenLocation(input: {
60 readonly terminalLocation: TerminalLocationLike | null;
61 readonly activeSessionLocation: TerminalLocationLike | null;
62 readonly workspaceRoot: string;
63 readonly threadShellWorktreePath: string | null;
64 readonly threadDetailWorktreePath: string | null;
65}): {
66 readonly cwd: string;
67 readonly worktreePath: string | null;
68} {
69 const sessionLocation = input.terminalLocation ?? input.activeSessionLocation;
70​
71 // Keep cwd paired with its identity; null identifies the project checkout.
72 if (sessionLocation !== null) {
73 return { cwd: sessionLocation.cwd, worktreePath: sessionLocation.worktreePath };
74 }
75 const preferredThreadWorktreePath = resolvePreferredThreadWorktreePath({
76 threadShellWorktreePath: input.threadShellWorktreePath,
77 threadDetailWorktreePath: input.threadDetailWorktreePath,
78 });
79 return {
80 cwd: preferredThreadWorktreePath ?? input.workspaceRoot,
81 worktreePath: preferredThreadWorktreePath,
82 };
84​
85export function useInitialTerminalLaunchLocation(input: {
86 readonly targetKey: string | null;
87 readonly pendingLaunch: PendingTerminalLaunch | null;
88 readonly hasResolvedPendingLaunch: boolean;
89 readonly isAuthenticated: boolean;
90 readonly canRead: boolean;
91 readonly canOperate: boolean;
92 readonly hasLoadedSessions: boolean;
93 readonly location: Parameters<typeof resolveTerminalOpenLocation>[0] | null;
94}) {
95 let candidate: TerminalLocationLike | null = null;
96 if (
97 input.location !== null &&
98 input.hasResolvedPendingLaunch &&
99 input.isAuthenticated &&
100 (input.canRead || input.canOperate)
101 ) {
102 if (input.pendingLaunch !== null) {
103 candidate = {
104 cwd: input.pendingLaunch.cwd,
105 worktreePath: input.pendingLaunch.worktreePath,
106 };
107 } else if (!input.canRead || input.hasLoadedSessions) {
108 // Metadata must establish whether this target has a location before we retain a fallback.
109 candidate = resolveTerminalOpenLocation(input.location);
110 }
111 }
112 const [entry, setEntry] = useState(() => ({ key: input.targetKey, location: candidate }));
113 useEffect(() => {
114 if (entry.key === input.targetKey && (entry.location !== null || candidate === null)) {
115 return;
116 }
117 setEntry({ key: input.targetKey, location: candidate });
118 }, [candidate, entry.key, entry.location, input.targetKey]);
119 return entry.key === input.targetKey ? entry.location : null;