import { useEffect, useState } from 'react'; import { Button, FeatureSettingsWindow, Icon, IconButton, Select, SettingsCard, StatusBadge, TextField, TextAreaField, WindowFooterActions, ConfirmationModal } 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' | 'applications'; 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
('connection'); const [status, setStatus] = useState(null); const [pending, setPending] = useState(''); const [connection, setConnection] = useState(''); const [key, setKey] = useState(''); const [name, setName] = useState(''); const [contour, setContour] = useState(''); 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; setPending('load'); Promise.all([request('status'), loadProjects()]).then(([value, available]) => { if (!active) return; setStatus(value); 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 }>(action, data); if (result.connection) setIssued(JSON.stringify(result.connection, null, 2)); if (action === 'connect') setConnection(''); await refresh(); await onChanged(); } catch (error) { onError(error); } finally { setPending(''); } } const contours = status?.contours || []; const canManage = status?.authority === 'host'; const busy = Boolean(pending); return <> { setSection(value); setKey(''); setName(''); }} onClose={onClose} footer={ { setPending('refresh'); try { await refresh(); } catch (error) { onError(error); } finally { setPending(''); } }}>}>
{section === 'connection' ? <> {status?.connected ? 'Подключён' : pending === 'load' ? 'Проверяем подключение' : status ? 'Не подключён' : 'Состояние не получено'} {status?.endpoint ?

{status.endpoint}

: null}
setConnection(event.target.value)} rows={5} />
: null} {section === 'contours' ? <>
{contours.map((item) => { const link = status?.links?.find((value) => value.contourId === item.id); return
{item.name}{item.key}
{link ? projects.find((p) => p.projectRef === link.projectRef)?.name || 'Связан с проектом' : 'Не связан'}
; })} {!contours.length ?

На VPS пока нет контуров.

: null} ({ value: item.projectRef, label: item.name }))} />
{canManage ?
setName(e.target.value)} /> setKey(e.target.value)} />
: null} : null} {section === 'applications' ? <> {issued ?
: null} {(status?.applications || []).filter((app) => app.authority !== 'host' && app.key !== 'legacy-mission-core').map((app) => contours.find((item) => item.id === id)?.name || id).join(', ')}>
{app.credentials.map((credential) =>
{credential.client_id}{credential.enabled ? 'Действует' : 'Отозван'}
{credential.enabled ? : null}
)}
)} {canManage ?
setName(e.target.value)} /> setKey(e.target.value)} />