@@ -699,106 +699,114 @@ function ProjectSessionsGroup({
699699 } ;
700700
701701 const handleResumeSession = async ( session : Session ) => {
702- if ( ! workspace ) return ;
702+ // Use functional update to avoid race conditions with stale closure values
703+ let savedWorkspace : WorkspaceData | null = null ;
703704
704- // Select this project first
705- navigate ( { type : "workspace" , projectId : project . id , mode : "features" } ) ;
705+ setWorkspace ( ( currentWorkspace ) => {
706+ if ( ! currentWorkspace ) return currentWorkspace ;
706707
707- // Find or create a feature to add the session terminal
708- const targetFeature = project . features . find ( ( f ) => ! f . archived ) ;
708+ // Find current project from latest state
709+ const currentProject = currentWorkspace . projects . find ( ( p ) => p . id === project . id ) ;
710+ if ( ! currentProject ) return currentWorkspace ;
709711
710- // If no active feature, just navigate to the project
711- if ( ! targetFeature ) {
712- const newWorkspace : WorkspaceData = {
713- ...workspace ,
714- active_project_id : project . id ,
715- } ;
716- setWorkspace ( newWorkspace ) ;
717- await invoke ( "workspace_save" , { data : newWorkspace } ) ;
718- return ;
719- }
712+ // Find or create a feature to add the session terminal
713+ const targetFeature = currentProject . features . find ( ( f ) => ! f . archived ) ;
720714
721- // Create a new terminal session with cc --resume command
722- const panelId = targetFeature . panels [ 0 ] ?. id ;
723- const title = session . summary || "CC Session" ;
724- const command = `cc --resume "${ session . id } "` ;
725-
726- if ( ! panelId ) {
727- // No panel exists, create one with the resume command
728- const newPanelId = crypto . randomUUID ( ) ;
729- const ptySessionId = crypto . randomUUID ( ) ;
730- const ptyId = crypto . randomUUID ( ) ;
731-
732- const newPanel = {
733- id : newPanelId ,
734- sessions : [ { id : ptySessionId , pty_id : ptyId , title, command } ] ,
735- active_session_id : ptySessionId ,
736- is_shared : false ,
737- cwd : project . path ,
738- } ;
715+ // If no active feature, just set active project
716+ if ( ! targetFeature ) {
717+ savedWorkspace = {
718+ ...currentWorkspace ,
719+ active_project_id : project . id ,
720+ } ;
721+ return savedWorkspace ;
722+ }
739723
740- const newProjects = workspace . projects . map ( ( p ) => {
741- if ( p . id !== project . id ) return p ;
742- return {
743- ...p ,
744- features : p . features . map ( ( f ) => {
745- if ( f . id !== targetFeature ! . id ) return f ;
746- return {
747- ...f ,
748- panels : [ ...f . panels , newPanel ] ,
749- layout : { type : "panel" as const , panelId : newPanelId } ,
750- } ;
751- } ) ,
752- active_feature_id : targetFeature ! . id ,
753- view_mode : "features" as const ,
724+ // Create a new terminal session with claude --resume command
725+ const panelId = targetFeature . panels [ 0 ] ?. id ;
726+ const title = session . summary || "Untitled" ;
727+ const command = `claude --resume "${ session . id } "` ;
728+
729+ if ( ! panelId ) {
730+ // No panel exists, create one with the resume command
731+ const newPanelId = crypto . randomUUID ( ) ;
732+ const ptySessionId = crypto . randomUUID ( ) ;
733+ const ptyId = crypto . randomUUID ( ) ;
734+
735+ const newPanel = {
736+ id : newPanelId ,
737+ sessions : [ { id : ptySessionId , pty_id : ptyId , title, command } ] ,
738+ active_session_id : ptySessionId ,
739+ is_shared : false ,
740+ cwd : project . path ,
754741 } ;
755- } ) ;
756742
757- const newWorkspace : WorkspaceData = {
758- ...workspace ,
759- projects : newProjects ,
760- active_project_id : project . id ,
761- } ;
762- setWorkspace ( newWorkspace ) ;
763- await invoke ( "workspace_save" , { data : newWorkspace } ) ;
764- } else {
765- // Add a new session tab to the first panel
766- const ptySessionId = crypto . randomUUID ( ) ;
767- const ptyId = crypto . randomUUID ( ) ;
768-
769- const newProjects = workspace . projects . map ( ( p ) => {
770- if ( p . id !== project . id ) return p ;
771- return {
772- ...p ,
773- features : p . features . map ( ( f ) => {
774- if ( f . id !== targetFeature ! . id ) return f ;
775- return {
776- ...f ,
777- panels : f . panels . map ( ( panel ) => {
778- if ( panel . id !== panelId ) return panel ;
779- return {
780- ...panel ,
781- sessions : [
782- ...( panel . sessions || [ ] ) ,
783- { id : ptySessionId , pty_id : ptyId , title, command } ,
784- ] ,
785- active_session_id : ptySessionId ,
786- } ;
787- } ) ,
788- } ;
789- } ) ,
790- active_feature_id : targetFeature ! . id ,
791- view_mode : "features" as const ,
743+ const newProjects = currentWorkspace . projects . map ( ( p ) => {
744+ if ( p . id !== project . id ) return p ;
745+ return {
746+ ...p ,
747+ features : p . features . map ( ( f ) => {
748+ if ( f . id !== targetFeature . id ) return f ;
749+ return {
750+ ...f ,
751+ panels : [ ...f . panels , newPanel ] ,
752+ layout : { type : "panel" as const , panelId : newPanelId } ,
753+ } ;
754+ } ) ,
755+ active_feature_id : targetFeature . id ,
756+ view_mode : "features" as const ,
757+ } ;
758+ } ) ;
759+
760+ savedWorkspace = {
761+ ...currentWorkspace ,
762+ projects : newProjects ,
763+ active_project_id : project . id ,
792764 } ;
793- } ) ;
765+ return savedWorkspace ;
766+ } else {
767+ // Add a new session tab to the first panel
768+ const ptySessionId = crypto . randomUUID ( ) ;
769+ const ptyId = crypto . randomUUID ( ) ;
770+
771+ const newProjects = currentWorkspace . projects . map ( ( p ) => {
772+ if ( p . id !== project . id ) return p ;
773+ return {
774+ ...p ,
775+ features : p . features . map ( ( f ) => {
776+ if ( f . id !== targetFeature . id ) return f ;
777+ return {
778+ ...f ,
779+ panels : f . panels . map ( ( panel ) => {
780+ if ( panel . id !== panelId ) return panel ;
781+ return {
782+ ...panel ,
783+ sessions : [
784+ ...( panel . sessions || [ ] ) ,
785+ { id : ptySessionId , pty_id : ptyId , title, command } ,
786+ ] ,
787+ active_session_id : ptySessionId ,
788+ } ;
789+ } ) ,
790+ } ;
791+ } ) ,
792+ active_feature_id : targetFeature . id ,
793+ view_mode : "features" as const ,
794+ } ;
795+ } ) ;
794796
795- const newWorkspace : WorkspaceData = {
796- ...workspace ,
797- projects : newProjects ,
798- active_project_id : project . id ,
799- } ;
800- setWorkspace ( newWorkspace ) ;
801- await invoke ( "workspace_save" , { data : newWorkspace } ) ;
797+ savedWorkspace = {
798+ ...currentWorkspace ,
799+ projects : newProjects ,
800+ active_project_id : project . id ,
801+ } ;
802+ return savedWorkspace ;
803+ }
804+ } ) ;
805+
806+ // Save after state update - use the captured workspace from functional update
807+ if ( savedWorkspace ) {
808+ await invoke ( "workspace_save" , { data : savedWorkspace } ) ;
809+ navigate ( { type : "workspace" , projectId : project . id , mode : "features" } ) ;
802810 }
803811 } ;
804812
0 commit comments