Add local node discovery

This commit is contained in:
Jack Kingsman
2026-03-13 17:25:28 -07:00
parent bd19015693
commit 3a4ea8022b
14 changed files with 721 additions and 6 deletions
+33 -1
View File
@@ -2,11 +2,20 @@ import { useState, useCallback, useEffect, useRef } from 'react';
import { api } from '../api';
import { takePrefetchOrFetch } from '../prefetch';
import { toast } from '../components/ui/sonner';
import type { HealthStatus, RadioConfig, RadioConfigUpdate } from '../types';
import type {
HealthStatus,
RadioConfig,
RadioConfigUpdate,
RadioDiscoveryResponse,
RadioDiscoveryTarget,
} from '../types';
export function useRadioControl() {
const [health, setHealth] = useState<HealthStatus | null>(null);
const [config, setConfig] = useState<RadioConfig | null>(null);
const [meshDiscovery, setMeshDiscovery] = useState<RadioDiscoveryResponse | null>(null);
const [meshDiscoveryLoadingTarget, setMeshDiscoveryLoadingTarget] =
useState<RadioDiscoveryTarget | null>(null);
const prevHealthRef = useRef<HealthStatus | null>(null);
const rebootPollTokenRef = useRef(0);
@@ -96,6 +105,26 @@ export function useRadioControl() {
}
}, []);
const handleDiscoverMesh = useCallback(async (target: RadioDiscoveryTarget) => {
setMeshDiscoveryLoadingTarget(target);
try {
const data = await api.discoverMesh(target);
setMeshDiscovery(data);
toast.success(
data.results.length === 0
? 'No nearby nodes responded'
: `Found ${data.results.length} nearby node${data.results.length === 1 ? '' : 's'}`
);
} catch (err) {
console.error('Failed to discover nearby nodes:', err);
toast.error('Failed to run mesh discovery', {
description: err instanceof Error ? err.message : 'Check radio connection',
});
} finally {
setMeshDiscoveryLoadingTarget(null);
}
}, []);
const handleHealthRefresh = useCallback(async () => {
try {
const data = await api.getHealth();
@@ -118,6 +147,9 @@ export function useRadioControl() {
handleDisconnect,
handleReconnect,
handleAdvertise,
meshDiscovery,
meshDiscoveryLoadingTarget,
handleDiscoverMesh,
handleHealthRefresh,
};
}