@@ -11,7 +11,7 @@ import {
1111 isAtomCommandInterrupted ,
1212 squashAtomCommandFailure ,
1313} from "@t3tools/client-runtime/state/runtime" ;
14- import { useCallback , useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
14+ import { useEffect , useRef , useState , type ReactNode } from "react" ;
1515import type {
1616 ServerProcessDiagnosticsEntry ,
1717 ServerProcessResourceHistorySummary ,
@@ -299,6 +299,28 @@ function ProcessNameCell({
299299 ) ;
300300}
301301
302+ function visibleProcessTree (
303+ processes : ReadonlyArray < ServerProcessDiagnosticsEntry > ,
304+ collapsedPids : ReadonlySet < number > ,
305+ ) {
306+ const visible : ServerProcessDiagnosticsEntry [ ] = [ ] ;
307+ let hiddenChildDepth : number | null = null ;
308+
309+ for ( const process of processes ) {
310+ if ( hiddenChildDepth !== null ) {
311+ if ( process . depth > hiddenChildDepth ) continue ;
312+ hiddenChildDepth = null ;
313+ }
314+
315+ visible . push ( process ) ;
316+ if ( collapsedPids . has ( process . pid ) ) {
317+ hiddenChildDepth = process . depth ;
318+ }
319+ }
320+
321+ return visible ;
322+ }
323+
302324function ProcessDiagnosticsTable ( {
303325 processes,
304326 canMaintainEnvironment,
@@ -313,26 +335,9 @@ function ProcessDiagnosticsTable({
313335 emptyLabel ?: string ;
314336} ) {
315337 const [ collapsedPids , setCollapsedPids ] = useState < ReadonlySet < number > > ( ( ) => new Set ( ) ) ;
316- const visibleProcesses = useMemo ( ( ) => {
317- const visible : ServerProcessDiagnosticsEntry [ ] = [ ] ;
318- let hiddenChildDepth : number | null = null ;
319-
320- for ( const process of processes ) {
321- if ( hiddenChildDepth !== null ) {
322- if ( process . depth > hiddenChildDepth ) continue ;
323- hiddenChildDepth = null ;
324- }
325-
326- visible . push ( process ) ;
327- if ( collapsedPids . has ( process . pid ) ) {
328- hiddenChildDepth = process . depth ;
329- }
330- }
338+ const visibleProcesses = visibleProcessTree ( processes , collapsedPids ) ;
331339
332- return visible ;
333- } , [ collapsedPids , processes ] ) ;
334-
335- const toggleProcess = useCallback ( ( pid : number ) => {
340+ const toggleProcess = ( pid : number ) => {
336341 setCollapsedPids ( ( previous ) => {
337342 const next = new Set ( previous ) ;
338343 if ( next . has ( pid ) ) {
@@ -342,7 +347,7 @@ function ProcessDiagnosticsTable({
342347 }
343348 return next ;
344349 } ) ;
345- } , [ ] ) ;
350+ } ;
346351
347352 return (
348353 < div className = "border-t border-border/60" >
@@ -783,7 +788,7 @@ export function DiagnosticsSettingsPanel() {
783788 } ;
784789 } , [ environmentId ] ) ;
785790
786- const openLogsDirectory = useCallback ( ( ) => {
791+ const openLogsDirectory = ( ) => {
787792 const logsDirectoryPath = observability ?. logsDirectoryPath ?? null ;
788793 if ( ! logsDirectoryPath ) return ;
789794
@@ -806,7 +811,7 @@ export function DiagnosticsSettingsPanel() {
806811 ) ;
807812 }
808813 } ) ( ) ;
809- } , [ environmentId , observability ?. logsDirectoryPath , openInEditor ] ) ;
814+ } ;
810815
811816 const isInitialLoading = isPending && data === null ;
812817 const isProcessInitialLoading = isProcessPending && processData === null ;
@@ -824,104 +829,97 @@ export function DiagnosticsSettingsPanel() {
824829 ) {
825830 setExportFormEnvironmentId ( environmentId ) ;
826831 }
827- const signalProcess = useCallback (
828- async ( pid : number , signal : ServerProcessSignal ) => {
829- const targetEnvironmentId = environmentIdRef . current ;
830- const process = processDataRef . current ?. processes . find ( ( entry ) => entry . pid === pid ) ;
831- if (
832- targetEnvironmentId === null ||
833- process === undefined ||
834- ! readEnvironmentScope ( targetEnvironmentId , AuthEnvironmentMaintainScope )
835- )
836- return ;
837- if ( signalingPidRef . current !== null ) return ;
838- signalingPidRef . current = pid ;
839- setSignalingPid ( pid ) ;
840- const clearSignaling = ( ) => {
841- signalingPidRef . current = null ;
842- setSignalingPid ( null ) ;
843- } ;
844- if ( signal === "SIGKILL" ) {
845- let confirmed = false ;
846- try {
847- confirmed = await ensureLocalApi ( ) . dialogs . confirm (
848- `Send SIGKILL to process ${ pid } ? This cannot be handled by the process.` ,
849- { variant : "destructive" } ,
850- ) ;
851- } catch ( error ) {
852- clearSignaling ( ) ;
853- toastManager . add ( {
854- type : "error" ,
855- title : "Could not confirm signal" ,
856- description : error instanceof Error ? error . message : `Failed to send ${ signal } .` ,
857- } ) ;
858- return ;
859- }
860- if ( ! confirmed ) {
861- clearSignaling ( ) ;
862- return ;
863- }
864- }
865- const currentEnvironmentId = environmentIdRef . current ;
866- if (
867- currentEnvironmentId === null ||
868- currentEnvironmentId !== targetEnvironmentId ||
869- ! readEnvironmentScope ( currentEnvironmentId , AuthEnvironmentMaintainScope )
870- ) {
832+ const signalProcess = async ( pid : number , signal : ServerProcessSignal ) => {
833+ const targetEnvironmentId = environmentIdRef . current ;
834+ const process = processDataRef . current ?. processes . find ( ( entry ) => entry . pid === pid ) ;
835+ if (
836+ targetEnvironmentId === null ||
837+ process === undefined ||
838+ ! readEnvironmentScope ( targetEnvironmentId , AuthEnvironmentMaintainScope )
839+ )
840+ return ;
841+ if ( signalingPidRef . current !== null ) return ;
842+ signalingPidRef . current = pid ;
843+ setSignalingPid ( pid ) ;
844+ const clearSignaling = ( ) => {
845+ signalingPidRef . current = null ;
846+ setSignalingPid ( null ) ;
847+ } ;
848+ if ( signal === "SIGKILL" ) {
849+ let confirmed = false ;
850+ try {
851+ confirmed = await ensureLocalApi ( ) . dialogs . confirm (
852+ `Send SIGKILL to process ${ pid } ? This cannot be handled by the process.` ,
853+ { variant : "destructive" } ,
854+ ) ;
855+ } catch ( error ) {
871856 clearSignaling ( ) ;
857+ toastManager . add ( {
858+ type : "error" ,
859+ title : "Could not confirm signal" ,
860+ description : error instanceof Error ? error . message : `Failed to send ${ signal } .` ,
861+ } ) ;
872862 return ;
873863 }
874- if (
875- processDataRef . current ?. processes . find ( ( entry ) => entry . pid === pid ) ?. startTimeMs !==
876- process . startTimeMs
877- ) {
864+ if ( ! confirmed ) {
878865 clearSignaling ( ) ;
879866 return ;
880867 }
868+ }
869+ const currentEnvironmentId = environmentIdRef . current ;
870+ if (
871+ currentEnvironmentId === null ||
872+ currentEnvironmentId !== targetEnvironmentId ||
873+ ! readEnvironmentScope ( currentEnvironmentId , AuthEnvironmentMaintainScope )
874+ ) {
875+ clearSignaling ( ) ;
876+ return ;
877+ }
878+ if (
879+ processDataRef . current ?. processes . find ( ( entry ) => entry . pid === pid ) ?. startTimeMs !==
880+ process . startTimeMs
881+ ) {
882+ clearSignaling ( ) ;
883+ return ;
884+ }
881885
882- try {
883- const result = await signalServerProcess ( {
884- environmentId : targetEnvironmentId ,
885- input : { pid, startTimeMs : process . startTimeMs , signal } ,
886+ const result = await signalServerProcess ( {
887+ environmentId : targetEnvironmentId ,
888+ input : { pid, startTimeMs : process . startTimeMs , signal } ,
889+ } ) ;
890+ clearSignaling ( ) ;
891+ if ( result . _tag === "Failure" ) {
892+ if ( ! isAtomCommandInterrupted ( result ) ) {
893+ const error = squashAtomCommandFailure ( result ) ;
894+ toastManager . add ( {
895+ type : "error" ,
896+ title : `Could not send ${ signal } ` ,
897+ description : error instanceof Error ? error . message : `Failed to send ${ signal } .` ,
886898 } ) ;
887- if ( result . _tag === "Failure" ) {
888- if ( ! isAtomCommandInterrupted ( result ) ) {
889- const error = squashAtomCommandFailure ( result ) ;
890- toastManager . add ( {
891- type : "error" ,
892- title : `Could not send ${ signal } ` ,
893- description : error instanceof Error ? error . message : `Failed to send ${ signal } .` ,
894- } ) ;
895- }
896- return ;
897- }
898- if ( ! result . value . signaled ) {
899- const message = Option . getOrUndefined ( result . value . message ) ;
900- refreshProcesses ( ) ;
901- if ( isStaleProcessSignalMessage ( message ) ) {
902- toastManager . add ( {
903- type : "info" ,
904- title : "Process already exited" ,
905- description :
906- "The process is not a child of the T3 Server. It might already have exited." ,
907- } ) ;
908- return ;
909- }
910-
911- toastManager . add ( {
912- type : "error" ,
913- title : `Could not send ${ signal } ` ,
914- description : message ?? `Failed to send ${ signal } .` ,
915- } ) ;
916- return ;
917- }
918- refreshProcesses ( ) ;
919- } finally {
920- clearSignaling ( ) ;
921899 }
922- } ,
923- [ refreshProcesses , signalServerProcess ] ,
924- ) ;
900+ return ;
901+ }
902+ if ( ! result . value . signaled ) {
903+ const message = Option . getOrUndefined ( result . value . message ) ;
904+ refreshProcesses ( ) ;
905+ if ( isStaleProcessSignalMessage ( message ) ) {
906+ toastManager . add ( {
907+ type : "info" ,
908+ title : "Process already exited" ,
909+ description : "The process is not a child of the T3 Server. It might already have exited." ,
910+ } ) ;
911+ return ;
912+ }
913+
914+ toastManager . add ( {
915+ type : "error" ,
916+ title : `Could not send ${ signal } ` ,
917+ description : message ?? `Failed to send ${ signal } .` ,
918+ } ) ;
919+ return ;
920+ }
921+ refreshProcesses ( ) ;
922+ } ;
925923
926924 const processDiagnosticsError = processData ? Option . getOrNull ( processData . error ) : null ;
927925 const processResourceError = resourceData ? Option . getOrNull ( resourceData . error ) : null ;
0 commit comments