feat(fleet): preserve operator VESC integration before final driver merge
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import {useEffect,useState} from 'react';
|
||||
import {Button,LoadingRegion,ResourceList,ResourceRow,SettingsCard} from '@nodedc/ui-react';
|
||||
import type {ConfigurationVersion,SensorTransport} from '../../../../packages/sensor-ui/src/contracts';
|
||||
|
||||
export function VescBackups({deviceId,archive,revision}: {deviceId:string;archive:SensorTransport['configurationArchive'];revision:string|null}) {
|
||||
const [reload,setReload]=useState(0);
|
||||
const [items,setItems]=useState<ConfigurationVersion[]>([]);
|
||||
const [next,setNext]=useState<string|null>(null);
|
||||
const [loading,setLoading]=useState(false);
|
||||
const [error,setError]=useState<string|null>(null);
|
||||
const [download,setDownload]=useState<string|null>(null);
|
||||
useEffect(()=>{
|
||||
let active=true;
|
||||
setItems([]);setNext(null);setError(null);
|
||||
if(!archive)return;
|
||||
setLoading(true);
|
||||
archive.list(deviceId).then(result=>{if(active){setItems(result.items);setNext(result.next);}})
|
||||
.catch(()=>{if(active)setError('Не удалось загрузить историю конфигураций.');})
|
||||
.finally(()=>{if(active)setLoading(false);});
|
||||
return()=>{active=false;};
|
||||
},[deviceId,archive,revision,reload]);
|
||||
async function more(){
|
||||
if(!archive||!next||loading)return;
|
||||
setLoading(true);setError(null);
|
||||
try{const result=await archive.list(deviceId,next);setItems(current=>[...current,...result.items.filter(item=>!current.some(old=>old.id===item.id))]);setNext(result.next);}
|
||||
catch{setError('Не удалось загрузить следующие версии.');}
|
||||
finally{setLoading(false);}
|
||||
}
|
||||
async function save(id:string){
|
||||
if(!archive||download)return;
|
||||
setDownload(id);setError(null);
|
||||
try{
|
||||
const value=await archive.read(deviceId,id);
|
||||
const url=URL.createObjectURL(new Blob([JSON.stringify(value,null,2)+'\n'],{type:'application/json'}));
|
||||
const link=document.createElement('a');link.href=url;link.download=`${deviceId}-${id}.json`;link.click();
|
||||
setTimeout(()=>URL.revokeObjectURL(url),1000);
|
||||
}catch{setError('Не удалось скачать выбранную версию.');}
|
||||
finally{setDownload(null);}
|
||||
}
|
||||
return <SettingsCard title="История конфигураций" description="Сохранённые версии остаются на борту и передаются в Core при подключении." actions={<Button disabled={loading||!archive} onClick={()=>setReload(value=>value+1)}>Обновить историю</Button>}>
|
||||
<LoadingRegion loading={loading&&!items.length} label="Загрузка версий конфигурации">
|
||||
{error&&<p role="alert">{error}</p>}
|
||||
{!loading&&!items.length&&!error&&<p>Сохранённых версий пока нет.</p>}
|
||||
<ResourceList aria-label="Версии конфигурации VESC">{items.map(item=><li key={item.id}>
|
||||
<ResourceRow title={new Date(item.observed_at).toLocaleString()} description={`Прошивка ${item.firmware} · мотор и входы`}
|
||||
actions={<Button disabled={download!==null} loading={download===item.id} onClick={()=>void save(item.id)}>Скачать</Button>}/>
|
||||
</li>)}</ResourceList>
|
||||
</LoadingRegion>
|
||||
{next&&<Button loading={loading} disabled={loading} onClick={()=>void more()}>Ещё версии</Button>}
|
||||
</SettingsCard>;
|
||||
}
|
||||
Reference in New Issue
Block a user