import { useEffect, useRef, useState } from 'react'; import { Button, FeatureSettingsWindow, Icon, IconButton, Select, SettingsCard, StatusBadge, TextField, TextAreaField, WindowFooterActions, ConfirmationModal, ResourceList, ResourceRow } from '@nodedc/ui-react'; import { loadProjects } from '../api'; import type { InfrastructureHostView, ProjectSummary, ProjectWorkspace } from '../types'; import { b2Request, type B2Status } from './api'; import './b2.css'; type Section = 'connection' | 'contours'; type Form = 'contour' | 'application' | 'project' | null; export function B2HostSettings({ host, workspace, onClose, onChanged, onError }: { host: InfrastructureHostView; workspace: ProjectWorkspace; onClose: () => void; onChanged: () => Promise; onError: (error: unknown) => void; }) { const project = workspace.project; const [section, setSection] = useState
('contours'); const [status, setStatus] = useState(null); const [pending, setPending] = useState('load'); const [connection, setConnection] = useState(''); const connectionFile = useRef(null); const [key, setKey] = useState(''), [name, setName] = useState(''); const [contourId, setContourId] = useState(''), [applicationId, setApplicationId] = useState(''); const [form, setForm] = useState
(null); const [showHistory, setShowHistory] = useState(false), [showInactive, setShowInactive] = useState(false); const [targetProject, setTargetProject] = useState(project.projectRef); const [projects, setProjects] = useState([]); const [issued, setIssued] = useState(''); const [confirm, setConfirm] = useState<{ action: string; fields: Record; text: string } | null>(null); const request = (action: string, data: Record = {}) => b2Request(project.projectRef, host.hostRef, action, data); const refresh = () => request('status').then(setStatus); useEffect(() => { let active = true; Promise.all([request('status'), loadProjects()]).then(([value, available]) => { if (!active) return; setStatus(value); if (!value.connected) setSection('connection'); setProjects(available.filter((item) => item.ownerScope.ownerScopeRef === project.ownerScope.ownerScopeRef)); }).catch((error) => { if (active) onError(error); }).finally(() => { if (active) setPending(''); }); return () => { active = false; }; }, [host.hostRef, project.projectRef]); async function run(action: string, data: Record = {}) { setPending(action); try { const result = await request<{ connection?: unknown; id?: string }>(action, data); if (result.connection) setIssued(JSON.stringify(result.connection, null, 2)); if (action === 'connect') { setConnection(''); setSection('contours'); } if (action === 'contour-create' && result.id) { setContourId(result.id); setApplicationId(''); } if (action === 'application-create' && result.id) setApplicationId(result.id); setForm(null); setName(''); setKey(''); // A successful mutation must not be presented as failed if a subsequent read fails. try { await refresh(); await onChanged(); } catch (error) { onError(error); } return true; } catch (error) { onError(error); return false; } finally { setPending(''); } } const contours = status?.contours || [], selected = contours.find((item) => item.id === contourId); const linkedProject = status?.links?.find((item) => item.contourId === contourId)?.projectRef; const applications = (status?.applications || []).filter((app) => app.authority !== 'host' && app.key !== 'legacy-mission-core' && app.grants[contourId]); const application = applications.find((app) => app.id === applicationId); const inactive = (app: typeof applications[number]) => app.credentials.length > 0 && !app.credentials.some((item) => item.enabled); const busy = Boolean(pending), canManage = status?.authority === 'host'; const begin = (value: Form) => { setForm(value); setName(''); setKey(''); setTargetProject(project.projectRef); }; const back = () => { setIssued(''); setShowHistory(false); if (form) setForm(null); else if (applicationId) setApplicationId(''); else { setContourId(''); setShowInactive(false); } }; function downloadIssuedKey() { const url = URL.createObjectURL(new Blob([issued + '\n'], { type: 'application/json' })); const link = document.createElement('a'); link.href = url; link.download = 'arnavi-application-key.json'; document.body.append(link); link.click(); link.remove(); window.setTimeout(() => URL.revokeObjectURL(url), 1000); } return <> { setSection(value); setForm(null); setIssued(''); }} onClose={onClose} footer={ {section === 'contours' && (selected || form) ? : null} { setPending('refresh'); try { await refresh(); } catch (error) { onError(error); } finally { setPending(''); } }}> }>
{section === 'connection' ? <> {status?.connected ? 'Подключён' : pending === 'load' ? 'Подключение…' : 'Не подключён'}} />
{ const file = event.currentTarget.files?.[0]; event.currentTarget.value = ''; if (!file) return; setPending('file'); try { if (file.size > 16384) throw new Error(); const text = await file.text(); JSON.parse(text); setConnection(text); } catch { onError(new Error('Не удалось прочитать файл ключа VPS.')); } finally { setPending(''); } }} /> setConnection(event.target.value)} rows={4} />
: null} {section === 'contours' ? <>
{[host.displayName, selected?.name, application?.name].filter(Boolean).join(' / ')}
{form === 'contour' || form === 'application' ?
{form === 'application' ? : null} setName(e.target.value)} /> setKey(e.target.value.toLowerCase())} />
: form === 'project' ?