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([]); const [next,setNext]=useState(null); const [loading,setLoading]=useState(false); const [error,setError]=useState(null); const [download,setDownload]=useState(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 setReload(value=>value+1)}>Обновить историю}> {error&&

{error}

} {!loading&&!items.length&&!error&&

Сохранённых версий пока нет.

} {items.map(item=>
  • void save(item.id)}>Скачать}/>
  • )}
    {next&&}
    ; }