@@ -52,9 +52,17 @@ type DeviceData = {
5252 waypoints : WaypointWithMetadata [ ] ;
5353 neighborInfo : Map < number , Protobuf . Mesh . NeighborInfo > ;
5454} ;
55+ export type ConnectionPhase =
56+ | "disconnected"
57+ | "connecting"
58+ | "configuring"
59+ | "configured" ;
60+
5561export interface Device extends DeviceData {
5662 // Ephemeral state (not persisted)
5763 status : Types . DeviceStatusEnum ;
64+ connectionPhase : ConnectionPhase ;
65+ connectionId : ConnectionId | null ;
5866 channels : Map < Types . ChannelNumber , Protobuf . Channel . Channel > ;
5967 config : Protobuf . LocalOnly . LocalConfig ;
6068 moduleConfig : Protobuf . LocalOnly . LocalModuleConfig ;
@@ -70,6 +78,8 @@ export interface Device extends DeviceData {
7078 clientNotifications : Protobuf . Mesh . ClientNotification [ ] ;
7179
7280 setStatus : ( status : Types . DeviceStatusEnum ) => void ;
81+ setConnectionPhase : ( phase : ConnectionPhase ) => void ;
82+ setConnectionId : ( id : ConnectionId | null ) => void ;
7383 setConfig : ( config : Protobuf . Config . Config ) => void ;
7484 setModuleConfig : ( config : Protobuf . ModuleConfig . ModuleConfig ) => void ;
7585 getEffectiveConfig < K extends ValidConfigType > (
@@ -153,6 +163,16 @@ export interface deviceState {
153163 ) => void ;
154164 removeSavedConnection : ( id : ConnectionId ) => void ;
155165 getSavedConnections : ( ) => Connection [ ] ;
166+
167+ // Active connection tracking
168+ activeConnectionId : ConnectionId | null ;
169+ setActiveConnectionId : ( id : ConnectionId | null ) => void ;
170+ getActiveConnectionId : ( ) => ConnectionId | null ;
171+
172+ // Helper selectors for connection ↔ device relationships
173+ getActiveConnection : ( ) => Connection | undefined ;
174+ getDeviceForConnection : ( id : ConnectionId ) => Device | undefined ;
175+ getConnectionForDevice : ( deviceId : number ) => Connection | undefined ;
156176}
157177
158178interface PrivateDeviceState extends deviceState {
@@ -185,6 +205,8 @@ function deviceFactory(
185205 neighborInfo,
186206
187207 status : Types . DeviceStatusEnum . DeviceDisconnected ,
208+ connectionPhase : "disconnected" ,
209+ connectionId : null ,
188210 channels : new Map ( ) ,
189211 config : create ( Protobuf . LocalOnly . LocalConfigSchema ) ,
190212 moduleConfig : create ( Protobuf . LocalOnly . LocalModuleConfigSchema ) ,
@@ -227,6 +249,26 @@ function deviceFactory(
227249 } ) ,
228250 ) ;
229251 } ,
252+ setConnectionPhase : ( phase : ConnectionPhase ) => {
253+ set (
254+ produce < PrivateDeviceState > ( ( draft ) => {
255+ const device = draft . devices . get ( id ) ;
256+ if ( device ) {
257+ device . connectionPhase = phase ;
258+ }
259+ } ) ,
260+ ) ;
261+ } ,
262+ setConnectionId : ( connectionId : ConnectionId | null ) => {
263+ set (
264+ produce < PrivateDeviceState > ( ( draft ) => {
265+ const device = draft . devices . get ( id ) ;
266+ if ( device ) {
267+ device . connectionId = connectionId ;
268+ }
269+ } ) ,
270+ ) ;
271+ } ,
230272 setConfig : ( config : Protobuf . Config . Config ) => {
231273 set (
232274 produce < PrivateDeviceState > ( ( draft ) => {
@@ -907,6 +949,7 @@ export const deviceStoreInitializer: StateCreator<PrivateDeviceState> = (
907949) => ( {
908950 devices : new Map ( ) ,
909951 savedConnections : [ ] ,
952+ activeConnectionId : null ,
910953
911954 addDevice : ( id ) => {
912955 const existing = get ( ) . devices . get ( id ) ;
@@ -972,6 +1015,33 @@ export const deviceStoreInitializer: StateCreator<PrivateDeviceState> = (
9721015 ) ;
9731016 } ,
9741017 getSavedConnections : ( ) => get ( ) . savedConnections ,
1018+
1019+ setActiveConnectionId : ( id ) => {
1020+ set (
1021+ produce < PrivateDeviceState > ( ( draft ) => {
1022+ draft . activeConnectionId = id ;
1023+ } ) ,
1024+ ) ;
1025+ } ,
1026+ getActiveConnectionId : ( ) => get ( ) . activeConnectionId ,
1027+
1028+ getActiveConnection : ( ) => {
1029+ const activeId = get ( ) . activeConnectionId ;
1030+ if ( ! activeId ) {
1031+ return undefined ;
1032+ }
1033+ return get ( ) . savedConnections . find ( ( c ) => c . id === activeId ) ;
1034+ } ,
1035+ getDeviceForConnection : ( id ) => {
1036+ const connection = get ( ) . savedConnections . find ( ( c ) => c . id === id ) ;
1037+ if ( ! connection ?. meshDeviceId ) {
1038+ return undefined ;
1039+ }
1040+ return get ( ) . devices . get ( connection . meshDeviceId ) ;
1041+ } ,
1042+ getConnectionForDevice : ( deviceId ) => {
1043+ return get ( ) . savedConnections . find ( ( c ) => c . meshDeviceId === deviceId ) ;
1044+ } ,
9751045} ) ;
9761046
9771047const persistOptions : PersistOptions < PrivateDeviceState , DevicePersisted > = {
0 commit comments