5959</template >
6060
6161<script setup lang="ts">
62- const { isSharing, sharedTerminals } = useShare ();
62+ const { isSharing, sharedTerminals, addSharedTerminal } = useShare ();
6363
6464const props = defineProps <{
6565 rootPath: string ;
@@ -95,16 +95,17 @@ function createSessions(count: number): TerminalSession[] {
9595 return arr ;
9696}
9797
98- const sessions = ref <TerminalSession []>(createSessions (1 ));
98+ // In share mode, start empty — sharedTerminals watcher will populate sessions
99+ const sessions = ref <TerminalSession []>(isSharing .value ? [] : createSessions (1 ));
99100
100101// Track server terminal IDs we've already registered (to avoid duplicates on terminal-added)
101102const knownShareIds = new Set <string >();
102103
103104// activeId = left terminal in split (or the single visible terminal)
104- const activeId = ref (sessions .value [0 ]! .id );
105+ const activeId = ref (sessions .value [0 ]? .id ?? ' ' );
105106const splitId = ref <string | null >(null );
106107// focusedId = which terminal is highlighted/selected in sidebar
107- const focusedId = ref (sessions .value [0 ]! .id );
108+ const focusedId = ref (sessions .value [0 ]? .id ?? ' ' );
108109const splitRatio = ref (50 );
109110
110111// Map: terminalId → { left, right } for ALL saved (non-active) split pairs
@@ -125,7 +126,7 @@ watch(isSharing, (sharing) => {
125126
126127watch (sharedTerminals , (list ) => {
127128 if (! isSharing .value ) return ;
128- // Add new terminals
129+ // Add new terminals we don't have yet
129130 for (const t of list ) {
130131 if (! knownShareIds .has (t .id )) {
131132 knownShareIds .add (t .id );
@@ -137,26 +138,25 @@ watch(sharedTerminals, (list) => {
137138 }
138139 }
139140 }
140- // Remove gone terminals
141- const activeIds = new Set (list .map ((t : any ) => t .id ));
141+ // Remove terminals no longer on server
142+ const serverIds = new Set (list .map ((t : any ) => t .id ));
142143 const before = sessions .value .length ;
143144 sessions .value = sessions .value .filter (s => {
144- if (s .shareTerminalId && ! activeIds .has (s .shareTerminalId )) {
145+ if (s .shareTerminalId && ! serverIds .has (s .shareTerminalId )) {
145146 knownShareIds .delete (s .shareTerminalId );
146147 return false ;
147148 }
148149 return true ;
149150 });
150151 if (sessions .value .length !== before ) {
151- // Re-select if active was removed
152152 if (! sessions .value .find (s => s .id === activeId .value ) && sessions .value .length > 0 ) {
153153 activeId .value = sessions .value [0 ]! .id ;
154154 focusedId .value = activeId .value ;
155155 } else if (sessions .value .length === 0 ) {
156156 emit (" close" );
157157 }
158158 }
159- }, { deep: true });
159+ }, { deep: true , immediate: true });
160160
161161watch (() => props .initialTerminalHeight , (val ) => {
162162 if (val !== undefined ) panelHeight .value = val ;
@@ -179,6 +179,8 @@ function onShareCreated(localId: string, serverTerminalId: string) {
179179 if (s ) {
180180 s .shareTerminalId = serverTerminalId ;
181181 knownShareIds .add (serverTerminalId );
182+ // Add to sharedTerminals so terminal-removed can clean it up
183+ addSharedTerminal (serverTerminalId , s .name );
182184 }
183185}
184186
@@ -520,10 +522,13 @@ function resetSessions(count: number, splitIndex: number, savedPairsData?: [numb
520522
521523function ensureSession() {
522524 if (sessions .value .length === 0 ) {
525+ // In share mode, if the server already has terminals they'll appear via the watcher.
526+ // Only create a new local session if there are genuinely no shared terminals yet.
527+ if (isSharing .value && sharedTerminals .value .length > 0 ) return ;
523528 nextId = 1 ;
524529 epoch = Date .now ();
525530 savedPairsMap .clear ();
526- sessions .value = createSessions (1 );
531+ sessions .value = isSharing . value ? [{ id: ` t${ epoch }-${ nextId ++ } ` , name: ' Terminal 1 ' }] : createSessions (1 );
527532 activeId .value = sessions .value [0 ]! .id ;
528533 focusedId .value = activeId .value ;
529534 splitId .value = null ;
0 commit comments