77 type KeyboardEvent as ReactKeyboardEvent ,
88 type MouseEvent as ReactMouseEvent ,
99} from "react" ;
10+ import { scopeThreadRef } from "@t3tools/client-runtime/environment" ;
1011import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell" ;
1112import { cn } from "~/lib/utils" ;
1213import { resolveSidebarRowAccessibility } from "../Sidebar.logic" ;
@@ -19,11 +20,12 @@ import {
1920 type ComposerThreadDraftState ,
2021 type DraftSessionState ,
2122} from "../../composerDraftStore" ;
23+ import { selectThreadTerminalUiState , useTerminalUiStateStore } from "../../terminalUiStateStore" ;
2224import { draftPenClassName , draftSurfaceClassName } from "./SidebarThreadRow" ;
2325
2426const SidebarDraftRow = memo ( function SidebarDraftRow ( props : {
2527 draftId : DraftId ;
26- composer : ComposerThreadDraftState ;
28+ composer : ComposerThreadDraftState | null ;
2729 project : ProjectFaviconProject | null ;
2830 projectDisplayName : string | null ;
2931 isActive : boolean ;
@@ -32,22 +34,26 @@ const SidebarDraftRow = memo(function SidebarDraftRow(props: {
3234 onContextMenu : ( draftId : DraftId , position : { x : number ; y : number } ) => void ;
3335} ) {
3436 const { composer, draftId, onContextMenu, onDiscard, onNavigate } = props ;
35- const promptPreview =
36- replaceComposerContextReferences ( composer . prompt , ( occurrence ) => occurrence . label )
37- . trim ( )
38- . split ( "\n" , 1 ) [ 0 ] ?? "" ;
37+ const promptPreview = composer
38+ ? ( replaceComposerContextReferences ( composer . prompt , ( occurrence ) => occurrence . label )
39+ . trim ( )
40+ . split ( "\n" , 1 ) [ 0 ] ?? "" )
41+ : "" ;
3942 // images mirrors persistedAttachments once rehydration finishes; before
4043 // that only the persisted list is populated, hence max not sum.
41- const attachmentCount =
42- Math . max ( composer . images . length , composer . persistedAttachments . length ) +
43- composer . files . length +
44- composer . terminalContexts . length +
45- composer . previewAnnotations . length +
46- composer . reviewComments . length ;
44+ const attachmentCount = composer
45+ ? Math . max ( composer . images . length , composer . persistedAttachments . length ) +
46+ composer . files . length +
47+ composer . terminalContexts . length +
48+ composer . previewAnnotations . length +
49+ composer . reviewComments . length
50+ : 0 ;
4751 const preview =
4852 promptPreview . length > 0
4953 ? promptPreview
50- : `${ attachmentCount } attachment${ attachmentCount === 1 ? "" : "s" } ` ;
54+ : attachmentCount > 0
55+ ? `${ attachmentCount } attachment${ attachmentCount === 1 ? "" : "s" } `
56+ : "Terminal open" ;
5157 const accessibility = resolveSidebarRowAccessibility ( {
5258 title : preview ,
5359 statusLabel : "Unsent draft" ,
@@ -134,7 +140,7 @@ const SidebarDraftRow = memo(function SidebarDraftRow(props: {
134140interface SidebarDraftRowData {
135141 draftId : DraftId ;
136142 session : DraftSessionState ;
137- composer : ComposerThreadDraftState ;
143+ composer : ComposerThreadDraftState | null ;
138144}
139145
140146export const SidebarDraftBlock = memo ( function SidebarDraftBlock ( props : {
@@ -147,6 +153,17 @@ export const SidebarDraftBlock = memo(function SidebarDraftBlock(props: {
147153} ) {
148154 const draftThreadsByThreadKey = useComposerDraftStore ( ( store ) => store . draftThreadsByThreadKey ) ;
149155 const draftsByThreadKey = useComposerDraftStore ( ( store ) => store . draftsByThreadKey ) ;
156+ const terminalUiStateByThreadKey = useTerminalUiStateStore (
157+ ( store ) => store . terminalUiStateByThreadKey ,
158+ ) ;
159+ // A script run from a draft opens a terminal without touching the composer;
160+ // hiding that draft would leave its process running with no way back to it.
161+ const isListed = ( session : DraftSessionState , composer : ComposerThreadDraftState | undefined ) =>
162+ composerDraftHasUserContent ( composer ) ||
163+ selectThreadTerminalUiState (
164+ terminalUiStateByThreadKey ,
165+ scopeThreadRef ( session . environmentId , session . threadId ) ,
166+ ) . terminalIds . length > 0 ;
150167 const [ frozenActive , setFrozenActive ] = useState < {
151168 routeDraftId : string | null ;
152169 row : SidebarDraftRowData | null ;
@@ -158,8 +175,8 @@ export const SidebarDraftBlock = memo(function SidebarDraftBlock(props: {
158175 const session = draftThreadsByThreadKey [ draftId ] ;
159176 const composer = draftsByThreadKey [ draftId ] ;
160177 row =
161- session && session . promotedTo == null && composer && composerDraftHasUserContent ( composer )
162- ? { draftId, session, composer }
178+ session && session . promotedTo == null && isListed ( session , composer )
179+ ? { draftId, session, composer : composer ?? null }
163180 : null ;
164181 }
165182 setFrozenActive ( { routeDraftId : props . routeDraftId , row } ) ;
@@ -183,10 +200,14 @@ export const SidebarDraftBlock = memo(function SidebarDraftBlock(props: {
183200 continue ;
184201 }
185202 const composer = draftsByThreadKey [ draftKey ] ;
186- if ( ! composer || ! composerDraftHasUserContent ( composer ) ) {
203+ if ( ! isListed ( session , composer ) ) {
187204 continue ;
188205 }
189- rows . push ( { draftId : DraftId . make ( draftKey ) , session, composer } ) ;
206+ rows . push ( {
207+ draftId : DraftId . make ( draftKey ) ,
208+ session,
209+ composer : composer ?? null ,
210+ } ) ;
190211 }
191212 rows . sort ( ( left , right ) => right . session . createdAt . localeCompare ( left . session . createdAt ) ) ;
192213 return rows ;
0 commit comments