Skip to content

Commit ab03087

Browse files
authored
fix(connections): ensure connections reflect actual status. (#930)
1 parent e13d543 commit ab03087

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/web/src/pages/Connections/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ export const Connections = () => {
5757
removeConnection,
5858
setDefaultConnection,
5959
refreshStatuses,
60+
syncConnectionStatuses,
6061
} = useConnections();
6162
const { toast } = useToast();
6263
const navigate = useNavigate({ from: "/" });
6364
const [addOpen, setAddOpen] = useState(false);
6465
const isURLHTTPS = useMemo(() => location.protocol === "https:", []);
6566
const { t } = useTranslation("connections");
6667

67-
// On first mount, try to refresh statuses
68+
// On first mount, sync statuses and refresh
6869
// biome-ignore lint/correctness/useExhaustiveDependencies: This can cause the icon to refresh too often
6970
useEffect(() => {
71+
syncConnectionStatuses();
7072
refreshStatuses();
7173
}, []);
7274

packages/web/src/pages/Connections/useConnections.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)