fix(connections): ensure connections reflect actual status. (#930)
This commit is contained in:
@@ -57,6 +57,7 @@ export const Connections = () => {
|
|||||||
removeConnection,
|
removeConnection,
|
||||||
setDefaultConnection,
|
setDefaultConnection,
|
||||||
refreshStatuses,
|
refreshStatuses,
|
||||||
|
syncConnectionStatuses,
|
||||||
} = useConnections();
|
} = useConnections();
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const navigate = useNavigate({ from: "/" });
|
const navigate = useNavigate({ from: "/" });
|
||||||
@@ -64,9 +65,10 @@ export const Connections = () => {
|
|||||||
const isURLHTTPS = useMemo(() => location.protocol === "https:", []);
|
const isURLHTTPS = useMemo(() => location.protocol === "https:", []);
|
||||||
const { t } = useTranslation("connections");
|
const { t } = useTranslation("connections");
|
||||||
|
|
||||||
// On first mount, try to refresh statuses
|
// On first mount, sync statuses and refresh
|
||||||
// biome-ignore lint/correctness/useExhaustiveDependencies: This can cause the icon to refresh too often
|
// biome-ignore lint/correctness/useExhaustiveDependencies: This can cause the icon to refresh too often
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
syncConnectionStatuses();
|
||||||
refreshStatuses();
|
refreshStatuses();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ export function useConnections() {
|
|||||||
const { addNodeDB } = useNodeDBStore();
|
const { addNodeDB } = useNodeDBStore();
|
||||||
const { addMessageStore } = useMessageStore();
|
const { addMessageStore } = useMessageStore();
|
||||||
const { setSelectedDevice } = useAppStore();
|
const { setSelectedDevice } = useAppStore();
|
||||||
|
const selectedDeviceId = useAppStore((s) => s.selectedDeviceId);
|
||||||
|
|
||||||
const updateStatus = useCallback(
|
const updateStatus = useCallback(
|
||||||
(id: ConnectionId, status: ConnectionStatus, error?: string) => {
|
(id: ConnectionId, status: ConnectionStatus, error?: string) => {
|
||||||
@@ -483,6 +484,25 @@ export function useConnections() {
|
|||||||
await Promise.all([...httpChecks, ...btChecks, ...serialChecks]);
|
await Promise.all([...httpChecks, ...btChecks, ...serialChecks]);
|
||||||
}, [connections, updateSavedConnection]);
|
}, [connections, updateSavedConnection]);
|
||||||
|
|
||||||
|
const syncConnectionStatuses = useCallback(() => {
|
||||||
|
// Find which connection corresponds to the currently selected device
|
||||||
|
const activeConnection = connections.find(
|
||||||
|
(c) => c.meshDeviceId === selectedDeviceId,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update all connection statuses
|
||||||
|
connections.forEach((conn) => {
|
||||||
|
const shouldBeConnected = activeConnection?.id === conn.id;
|
||||||
|
|
||||||
|
// Update status if it doesn't match reality
|
||||||
|
if (shouldBeConnected && conn.status !== "connected") {
|
||||||
|
updateSavedConnection(conn.id, { status: "connected" });
|
||||||
|
} else if (!shouldBeConnected && conn.status === "connected") {
|
||||||
|
updateSavedConnection(conn.id, { status: "disconnected" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}, [connections, selectedDeviceId, updateSavedConnection]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
connections,
|
connections,
|
||||||
addConnection,
|
addConnection,
|
||||||
@@ -492,5 +512,6 @@ export function useConnections() {
|
|||||||
removeConnection,
|
removeConnection,
|
||||||
setDefaultConnection,
|
setDefaultConnection,
|
||||||
refreshStatuses,
|
refreshStatuses,
|
||||||
|
syncConnectionStatuses,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user