88import { scopeProjectRef } from "@t3tools/client-runtime/environment" ;
99import { projectScriptCwd , projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts" ;
1010import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels" ;
11- import { memo , useCallback , useEffect , useMemo , useState } from "react" ;
11+ import { memo , useEffect , useState } from "react" ;
1212import { useComposerDraftStore } from "../composerDraftStore" ;
1313import { useStableCallback } from "../hooks/useStableCallback" ;
1414import type { TerminalContextSelection } from "../lib/terminalContext" ;
@@ -70,6 +70,12 @@ function serverTerminalIdsStrictSubsetOfClient(
7070 return true ;
7171}
7272
73+ interface TerminalLaunchLocationWithEnv {
74+ readonly cwd : string ;
75+ readonly worktreePath : string | null ;
76+ readonly runtimeEnv : Record < string , string > ;
77+ }
78+
7379interface PersistentThreadTerminalDrawerProps {
7480 threadRef : { environmentId : EnvironmentId ; threadId : ThreadId } ;
7581 threadId : ThreadId ;
@@ -118,67 +124,36 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
118124 const panelSurfaces = useRightPanelStore (
119125 ( state ) => selectThreadRightPanelState ( state . byThreadKey , threadRef ) . surfaces ,
120126 ) ;
121- const panelTerminalIds = useMemo (
122- ( ) =>
123- new Set (
124- panelSurfaces . flatMap ( ( surface ) =>
125- surface . kind === "terminal" ? surface . terminalIds : [ ] ,
126- ) ,
127- ) ,
128- [ panelSurfaces ] ,
129- ) ;
130- const drawerTerminalSessions = useMemo (
131- ( ) =>
132- knownTerminalSessions ?. filter (
133- ( session ) => ! panelTerminalIds . has ( session . target . terminalId ) ,
134- ) ?? [ ] ,
135- [ knownTerminalSessions , panelTerminalIds ] ,
127+ const panelTerminalIds = new Set (
128+ panelSurfaces . flatMap ( ( surface ) => ( surface . kind === "terminal" ? surface . terminalIds : [ ] ) ) ,
136129 ) ;
137- const terminalLabelsById = useMemo ( ( ) => {
138- const next = new Map < string , string > ( ) ;
139- for ( const session of drawerTerminalSessions ) {
140- next . set (
141- session . target . terminalId ,
142- resolveTerminalSessionLabel ( session . target . terminalId , session . state . summary ) ,
143- ) ;
144- }
145- return next ;
146- } , [ drawerTerminalSessions ] ) ;
147- const terminalLaunchLocationsById = useMemo ( ( ) => {
148- const next = new Map <
149- string ,
150- {
151- readonly cwd : string ;
152- readonly worktreePath : string | null ;
153- readonly runtimeEnv : Record < string , string > ;
154- }
155- > ( ) ;
156- if ( ! project ) {
157- return next ;
158- }
159-
160- for ( const session of drawerTerminalSessions ) {
161- const summary = session . state . summary ;
162- if ( ! summary ) {
163- continue ;
164- }
165- const worktreePathForLaunch =
166- launchContext !== null ? launchContext . worktreePath : summary . worktreePath ;
167- next . set ( session . target . terminalId , {
168- cwd : launchContext ?. cwd ?? summary . cwd ,
130+ const drawerTerminalSessions =
131+ knownTerminalSessions ?. filter ( ( session ) => ! panelTerminalIds . has ( session . target . terminalId ) ) ??
132+ [ ] ;
133+ const terminalLabelsById = new Map < string , string > ( ) ;
134+ for ( const session of drawerTerminalSessions ) {
135+ terminalLabelsById . set (
136+ session . target . terminalId ,
137+ resolveTerminalSessionLabel ( session . target . terminalId , session . state . summary ) ,
138+ ) ;
139+ }
140+ const terminalLaunchLocationsById = new Map < string , TerminalLaunchLocationWithEnv > ( ) ;
141+ for ( const session of drawerTerminalSessions ) {
142+ const summary = session . state . summary ;
143+ if ( ! project || ! summary ) continue ;
144+ const worktreePathForLaunch =
145+ launchContext !== null ? launchContext . worktreePath : summary . worktreePath ;
146+ terminalLaunchLocationsById . set ( session . target . terminalId , {
147+ cwd : launchContext ?. cwd ?? summary . cwd ,
148+ worktreePath : worktreePathForLaunch ,
149+ runtimeEnv : projectScriptRuntimeEnv ( {
150+ project : { cwd : project . workspaceRoot } ,
169151 worktreePath : worktreePathForLaunch ,
170- runtimeEnv : projectScriptRuntimeEnv ( {
171- project : { cwd : project . workspaceRoot } ,
172- worktreePath : worktreePathForLaunch ,
173- } ) ,
174- } ) ;
175- }
176-
177- return next ;
178- } , [ drawerTerminalSessions , launchContext , project ] ) ;
179- const serverOrderedTerminalIds = useMemo (
180- ( ) => drawerTerminalSessions . map ( ( session ) => session . target . terminalId ) ,
181- [ drawerTerminalSessions ] ,
152+ } ) ,
153+ } ) ;
154+ }
155+ const serverOrderedTerminalIds = drawerTerminalSessions . map (
156+ ( session ) => session . target . terminalId ,
182157 ) ;
183158 const storeSetTerminalHeight = useTerminalUiStateStore ( ( state ) => state . setTerminalHeight ) ;
184159 const storeSplitTerminal = useTerminalUiStateStore ( ( state ) => state . splitTerminal ) ;
@@ -203,47 +178,32 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
203178 } , [ reconcileTerminalIds , serverOrderedTerminalIds , terminalUiState . terminalIds , threadRef ] ) ;
204179 const [ localFocusRequestId , setLocalFocusRequestId ] = useState ( 0 ) ;
205180 const worktreePath = serverThread ?. worktreePath ?? draftThread ?. worktreePath ?? null ;
206- const effectiveWorktreePath = useMemo ( ( ) => {
207- if ( launchContext !== null ) {
208- return launchContext . worktreePath ;
209- }
210- return worktreePath ;
211- } , [ launchContext , worktreePath ] ) ;
212- const cwd = useMemo (
213- ( ) =>
214- launchContext ?. cwd ??
215- ( project
216- ? projectScriptCwd ( {
217- project : { cwd : project . workspaceRoot } ,
218- worktreePath : effectiveWorktreePath ,
219- } )
220- : null ) ,
221- [ effectiveWorktreePath , launchContext ?. cwd , project ] ,
222- ) ;
223- const runtimeEnv = useMemo (
224- ( ) =>
225- project
226- ? projectScriptRuntimeEnv ( {
227- project : { cwd : project . workspaceRoot } ,
228- worktreePath : effectiveWorktreePath ,
229- } )
230- : { } ,
231- [ effectiveWorktreePath , project ] ,
232- ) ;
181+ const effectiveWorktreePath = launchContext !== null ? launchContext . worktreePath : worktreePath ;
182+ const cwd =
183+ launchContext ?. cwd ??
184+ ( project
185+ ? projectScriptCwd ( {
186+ project : { cwd : project . workspaceRoot } ,
187+ worktreePath : effectiveWorktreePath ,
188+ } )
189+ : null ) ;
190+ const runtimeEnv = project
191+ ? projectScriptRuntimeEnv ( {
192+ project : { cwd : project . workspaceRoot } ,
193+ worktreePath : effectiveWorktreePath ,
194+ } )
195+ : { } ;
233196
234- const bumpFocusRequestId = useCallback ( ( ) => {
197+ const bumpFocusRequestId = ( ) => {
235198 if ( ! visible ) {
236199 return ;
237200 }
238201 setLocalFocusRequestId ( ( value ) => value + 1 ) ;
239- } , [ visible ] ) ;
202+ } ;
240203
241- const setTerminalHeight = useCallback (
242- ( height : number ) => {
243- storeSetTerminalHeight ( threadRef , height ) ;
244- } ,
245- [ storeSetTerminalHeight , threadRef ] ,
246- ) ;
204+ const setTerminalHeight = ( height : number ) => {
205+ storeSetTerminalHeight ( threadRef , height ) ;
206+ } ;
247207
248208 const launchDrawerTerminal = ( place : ( terminalId : string ) => void ) => {
249209 if ( ! cwd ) return ;
@@ -265,13 +225,10 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
265225 launchDrawerTerminal ( ( terminalId ) => storeNewTerminal ( threadRef , terminalId ) ) ,
266226 ) ;
267227
268- const activateTerminal = useCallback (
269- ( terminalId : string ) => {
270- storeSetActiveTerminal ( threadRef , terminalId ) ;
271- bumpFocusRequestId ( ) ;
272- } ,
273- [ bumpFocusRequestId , storeSetActiveTerminal , threadRef ] ,
274- ) ;
228+ const activateTerminal = ( terminalId : string ) => {
229+ storeSetActiveTerminal ( threadRef , terminalId ) ;
230+ bumpFocusRequestId ( ) ;
231+ } ;
275232
276233 const closeTerminal = useStableCallback ( ( terminalId : string ) => {
277234 if ( ! terminalActions . canWrite ( ) ) return ;
@@ -280,15 +237,12 @@ export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerm
280237 bumpFocusRequestId ( ) ;
281238 } ) ;
282239
283- const handleAddTerminalContext = useCallback (
284- ( selection : TerminalContextSelection ) => {
285- if ( ! visible ) {
286- return ;
287- }
288- onAddTerminalContext ( selection ) ;
289- } ,
290- [ onAddTerminalContext , visible ] ,
291- ) ;
240+ const handleAddTerminalContext = ( selection : TerminalContextSelection ) => {
241+ if ( ! visible ) {
242+ return ;
243+ }
244+ onAddTerminalContext ( selection ) ;
245+ } ;
292246
293247 if ( ! project || ( ! terminalUiState . terminalOpen && ! active ) || ! cwd ) {
294248 return null ;
@@ -394,81 +348,38 @@ export const PersistentThreadTerminalPanel = memo(function PersistentThreadTermi
394348 ?. state . summary ?? null ;
395349 const worktreePath =
396350 launchContext ?. worktreePath ?? activeSummary ?. worktreePath ?? threadWorktreePath ;
397- const cwd = useMemo (
398- ( ) =>
399- launchContext ?. cwd ??
400- activeSummary ?. cwd ??
401- ( project
402- ? projectScriptCwd ( {
403- project : { cwd : project . workspaceRoot } ,
404- worktreePath,
405- } )
406- : null ) ,
407- [ activeSummary ?. cwd , launchContext ?. cwd , project , worktreePath ] ,
408- ) ;
409- const runtimeEnv = useMemo (
410- ( ) =>
411- project
412- ? projectScriptRuntimeEnv ( {
413- project : { cwd : project . workspaceRoot } ,
414- worktreePath,
415- } )
416- : { } ,
417- [ project , worktreePath ] ,
418- ) ;
419- const terminalLabelsById = useMemo ( ( ) => {
420- const labels = new Map < string , string > ( ) ;
421- for ( const terminalId of surface . terminalIds ) {
422- const summary =
423- knownTerminalSessions ?. find ( ( session ) => session . target . terminalId === terminalId ) ?. state
424- . summary ?? null ;
425- labels . set ( terminalId , resolveTerminalSessionLabel ( terminalId , summary ) ) ;
426- }
427- return labels ;
428- } , [ knownTerminalSessions , surface . terminalIds ] ) ;
429- const terminalLaunchLocationsById = useMemo ( ( ) => {
430- const locations = new Map <
431- string ,
432- {
433- readonly cwd : string ;
434- readonly worktreePath : string | null ;
435- readonly runtimeEnv : Record < string , string > ;
436- }
437- > ( ) ;
438- for ( const terminalId of surface . terminalIds ) {
439- const summary =
440- knownTerminalSessions ?. find ( ( session ) => session . target . terminalId === terminalId ) ?. state
441- . summary ?? null ;
442- const terminalWorktreePath =
443- launchContext ?. worktreePath ?? summary ?. worktreePath ?? threadWorktreePath ;
444- const terminalCwd =
351+ const cwd =
352+ launchContext ?. cwd ??
353+ activeSummary ?. cwd ??
354+ ( project ? projectScriptCwd ( { project : { cwd : project . workspaceRoot } , worktreePath } ) : null ) ;
355+ const runtimeEnv = project
356+ ? projectScriptRuntimeEnv ( { project : { cwd : project . workspaceRoot } , worktreePath } )
357+ : { } ;
358+ const terminalLabelsById = new Map < string , string > ( ) ;
359+ const terminalLaunchLocationsById = new Map < string , TerminalLaunchLocationWithEnv > ( ) ;
360+ for ( const terminalId of surface . terminalIds ) {
361+ const summary =
362+ knownTerminalSessions ?. find ( ( session ) => session . target . terminalId === terminalId ) ?. state
363+ . summary ?? null ;
364+ terminalLabelsById . set ( terminalId , resolveTerminalSessionLabel ( terminalId , summary ) ) ;
365+ if ( ! project ) continue ;
366+ const terminalWorktreePath =
367+ launchContext ?. worktreePath ?? summary ?. worktreePath ?? threadWorktreePath ;
368+ terminalLaunchLocationsById . set ( terminalId , {
369+ cwd :
445370 launchContext ?. cwd ??
446371 summary ?. cwd ??
447- ( project
448- ? projectScriptCwd ( {
449- project : { cwd : project . workspaceRoot } ,
450- worktreePath : terminalWorktreePath ,
451- } )
452- : null ) ;
453- if ( ! terminalCwd || ! project ) continue ;
454- locations . set ( terminalId , {
455- cwd : terminalCwd ,
456- worktreePath : terminalWorktreePath ,
457- runtimeEnv : projectScriptRuntimeEnv ( {
372+ projectScriptCwd ( {
458373 project : { cwd : project . workspaceRoot } ,
459374 worktreePath : terminalWorktreePath ,
460375 } ) ,
461- } ) ;
462- }
463- return locations ;
464- } , [
465- knownTerminalSessions ,
466- launchContext ?. cwd ,
467- launchContext ?. worktreePath ,
468- project ,
469- surface . terminalIds ,
470- threadWorktreePath ,
471- ] ) ;
376+ worktreePath : terminalWorktreePath ,
377+ runtimeEnv : projectScriptRuntimeEnv ( {
378+ project : { cwd : project . workspaceRoot } ,
379+ worktreePath : terminalWorktreePath ,
380+ } ) ,
381+ } ) ;
382+ }
472383
473384 if ( ! project || ! cwd ) return null ;
474385
0 commit comments