mirror of
https://github.com/jkingsman/Remote-Terminal-for-MeshCore.git
synced 2026-08-10 02:33:04 +02:00
Add local node discovery
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user