Discover Ethernet routers under the board, map physical SIM slots, expose vendor controls and real telemetry, and provide packaged VPS speed tests with a live monochrome gauge. Enforce compact typography across Core and Node UI.
31 lines
2.5 KiB
TypeScript
31 lines
2.5 KiB
TypeScript
import {useEffect,useRef,useState} from 'react';
|
|
import type {RouterAccess,RouterSnapshot,RouterTransport} from './contracts';
|
|
const errors:Record<string,string>={access_rejected:'Роутер отклонил доступ. Проверьте логин и пароль или ключ API.',router_certificate_rejected:'Сертификат не соответствует этому роутеру.',router_unavailable:'Роутер недоступен по указанному адресу.',expired:'Запрос устарел. Повторите после обновления состояния.',unsupported_response:'Ответ роутера не поддерживается.',storage_error:'Не удалось сохранить настройки на борту.'};
|
|
export function useRouterRequest(value:RouterSnapshot,transport:RouterTransport){
|
|
const [pending,setPending]=useState(''),[sending,setSending]=useState(false),[message,setMessage]=useState('');
|
|
const active=useRef(true);useEffect(()=>{active.current=true;return()=>{active.current=false;};},[]);
|
|
useEffect(()=>{
|
|
if(!pending)return;
|
|
let stopped=false,timer:ReturnType<typeof setTimeout>;const end=Date.now()+95000;
|
|
async function poll(){
|
|
try{const result=await transport.operation(pending);if(stopped)return;
|
|
if(result.state!=='queued'&&result.state!=='applying'){
|
|
setPending('');setMessage(result.state==='complete'?'Сохранено на борту.':errors[result.error??'']??'Результат не подтверждён. Обновите состояние перед повтором.');return;
|
|
}
|
|
}catch{/* Read receipt; never repeat an ambiguous write. */}
|
|
if(stopped)return;
|
|
if(Date.now()>=end){setPending('');setMessage('Результат не подтверждён. Обновите состояние перед повтором.');return;}
|
|
timer=setTimeout(()=>void poll(),2000);
|
|
}
|
|
void poll();return()=>{stopped=true;clearTimeout(timer);};
|
|
},[pending,transport]);
|
|
async function send(fields:Omit<RouterAccess,'operation_id'|'runtime_id'|'deadline'>){
|
|
if(!value.runtime_id)return;
|
|
const id='op_'+crypto.randomUUID().replaceAll('-','');setSending(true);setMessage('');
|
|
try{await transport.authorize({...fields,operation_id:id,runtime_id:value.runtime_id,deadline:Math.floor(Date.now()/1000)+85});}
|
|
catch{if(active.current)setMessage('Проверяем результат отправки.');}
|
|
finally{if(active.current){setSending(false);setPending(id);}}
|
|
}
|
|
return {send,busy:sending||!!pending,message};
|
|
}
|