@@ -46,6 +46,7 @@ export function useConnections() {
4646 const { addNodeDB } = useNodeDBStore ( ) ;
4747 const { addMessageStore } = useMessageStore ( ) ;
4848 const { setSelectedDevice } = useAppStore ( ) ;
49+ const selectedDeviceId = useAppStore ( ( s ) => s . selectedDeviceId ) ;
4950
5051 const updateStatus = useCallback (
5152 ( id : ConnectionId , status : ConnectionStatus , error ?: string ) => {
@@ -483,6 +484,25 @@ export function useConnections() {
483484 await Promise . all ( [ ...httpChecks , ...btChecks , ...serialChecks ] ) ;
484485 } , [ connections , updateSavedConnection ] ) ;
485486
487+ const syncConnectionStatuses = useCallback ( ( ) => {
488+ // Find which connection corresponds to the currently selected device
489+ const activeConnection = connections . find (
490+ ( c ) => c . meshDeviceId === selectedDeviceId ,
491+ ) ;
492+
493+ // Update all connection statuses
494+ connections . forEach ( ( conn ) => {
495+ const shouldBeConnected = activeConnection ?. id === conn . id ;
496+
497+ // Update status if it doesn't match reality
498+ if ( shouldBeConnected && conn . status !== "connected" ) {
499+ updateSavedConnection ( conn . id , { status : "connected" } ) ;
500+ } else if ( ! shouldBeConnected && conn . status === "connected" ) {
501+ updateSavedConnection ( conn . id , { status : "disconnected" } ) ;
502+ }
503+ } ) ;
504+ } , [ connections , selectedDeviceId , updateSavedConnection ] ) ;
505+
486506 return {
487507 connections,
488508 addConnection,
@@ -492,5 +512,6 @@ export function useConnections() {
492512 removeConnection,
493513 setDefaultConnection,
494514 refreshStatuses,
515+ syncConnectionStatuses,
495516 } ;
496517}
0 commit comments