11import { getUUID } from "../utils/random/getUUID"
2+ import { deleteGlobal , withGlobal } from "./useGlobal"
23import { CommandType } from "reactotron-core-contract"
34import type { StateSubscription , TimelineItem } from "../types"
4- import { withGlobal } from "./useGlobal"
55import { isSafeKey , sanitizeValue } from "../utils/sanitize"
66
77type UnsubscribeFn = ( ) => void
@@ -33,9 +33,7 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
3333 const [ _e , setError ] = withGlobal < Error | null > ( "error" , null )
3434 const [ clientIds , setClientIds ] = withGlobal < string [ ] > ( "clientIds" , [ ] )
3535 const [ , setActiveClientId ] = withGlobal ( "activeClientId" , "" )
36- const [ _timelineItems , setTimelineItems ] = withGlobal < TimelineItem [ ] > ( "timelineItems" , [ ] , {
37- persist : true ,
38- } )
36+ const [ _timelineItems , setTimelineItems ] = withGlobal < TimelineItem [ ] > ( "timelineItems" , [ ] )
3937 const [ _stateSubscriptionsByClientId , setStateSubscriptionsByClientId ] = withGlobal < {
4038 [ clientId : string ] : StateSubscription [ ]
4139 } > ( "stateSubscriptionsByClientId" , { } )
@@ -77,13 +75,22 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
7775 }
7876
7977 if ( data . type === "connectedClients" ) {
78+ let newestClientId = ""
8079 data . clients . forEach ( ( client : any ) => {
8180 // Store the client data in global state
8281 const clientId = client . clientId
8382 const [ _ , setClientData ] = withGlobal ( `client-${ clientId } ` , { } )
8483 setClientData ( client )
84+ if ( ! clientIds . includes ( clientId ) ) {
85+ newestClientId = clientId
86+ }
8587 } )
8688 setClientIds ( data . clients . map ( ( client : any ) => client . clientId ) )
89+
90+ if ( newestClientId ) {
91+ // Set the active client to the newest client
92+ setActiveClientId ( newestClientId )
93+ }
8794 }
8895
8996 if ( data . type === "command" && data . cmd ) {
@@ -109,7 +116,6 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
109116 console . tron . log ( "unknown command" , data . cmd )
110117 }
111118 if ( data . cmd . type === CommandType . StateValuesChange ) {
112- console . log ( "state.values.change" , data . cmd )
113119 data . cmd . payload . changes . forEach ( ( change : StateSubscription ) => {
114120 if ( ! isSafeKey ( data . cmd . clientId ) || ! isSafeKey ( change . path ) ) {
115121 console . warn (
@@ -155,8 +161,17 @@ export function connectToServer(props: { port: number } = { port: 9292 }): Unsub
155161 // Clean up after disconnect
156162 ws . socket . onclose = ( ) => {
157163 console . tron . log ( "Reactotron server disconnected" )
158- setIsConnected ( false )
164+ // Clear individual client data
165+ clientIds . forEach ( ( clientId ) => {
166+ const [ _ , setClientData ] = withGlobal ( `client-${ clientId } ` , { } )
167+ setClientData ( { } )
168+ } )
169+ deleteGlobal ( "clientIds" )
159170 setClientIds ( [ ] )
171+ setIsConnected ( false )
172+ setActiveClientId ( "" )
173+ setTimelineItems ( [ ] )
174+ setStateSubscriptionsByClientId ( { } )
160175 }
161176
162177 // Send a message to the server (which will be forwarded to the client)
0 commit comments