import { useEffect, useRef, useState } from 'react'; import { Download, Link2, ShieldCheck, Smartphone, X } from 'lucide-react'; import { api, downloadCompanionCertificate } from './api'; import type { Project } from './types'; import type { CompanionStatus } from './companion-types'; import './companion-panel.css'; export function CompanionPanel({ projects, selectedProjectId }: { projects: Project[]; selectedProjectId: string }) { const [status, setStatus] = useState(null), [address, setAddress] = useState(''), [port, setPort] = useState(4319); const [selected, setSelected] = useState(selectedProjectId ? [selectedProjectId] : []); const [approve, setApprove] = useState(false), [stop, setStop] = useState(false), [hours, setHours] = useState(24); const [pair, setPair] = useState<{ url: string; qr: string; expiresAt: string } | null>(null), [busy, setBusy] = useState(''), [error, setError] = useState(''); const mounted = useRef(true); async function refresh() { const result = await api('/companion'); if (mounted.current) { setStatus(result); setAddress(previous => previous || result.addresses[0]?.address || ''); } } useEffect(() => { mounted.current = true; void refresh().catch(value => { if (mounted.current) setError(value.message); }); const timer = setInterval(() => void refresh().catch(() => {}), 5000); return () => { mounted.current = false; clearInterval(timer); }; }, []); useEffect(() => { if (!pair) return; const timer = setTimeout(() => setPair(null), Math.max(0, Date.parse(pair.expiresAt) - Date.now())); return () => clearTimeout(timer); }, [pair]); async function act(name: string, action: () => Promise) { if (busy) return; setBusy(name); setError(''); try { await action(); await refresh(); } catch (value) { if (mounted.current) setError(value instanceof Error ? value.message : '手机连接操作失败。'); } finally { if (mounted.current) setBusy(''); } } return
{status?.enabled ? '手机连接已开启' : '在 iPhone 上接续任务'}

同一 Wi-Fi,通过 Safari 查看原任务、确认审批和停止执行。

{status?.enabled ? 'HTTPS' : '默认关闭'}
{error &&

{error}

}

Windows 连接地址

{status?.enabled ?
{status.origin}
:
{ event.preventDefault(); void act('start', () => api('/companion/start', { address, port })); }}>
} {status && !status.addresses.length && !status.enabled &&

尚未找到局域网地址。请先让电脑连接 Wi-Fi 或有线局域网,再刷新此窗口。

}

关闭手机连接后,电脑上的任务继续运行。宿主重启后,需要在这里重新开启。

{status?.certificate &&
首次连接:在 iPhone 信任证书
  1. 下载本机根证书,用你自己的文件传输方式保存到 iPhone「文件」,点开安装。
  2. 在 iPhone「设置 → 通用 → VPN 与设备管理」安装此证书描述文件。
  3. 在「设置 → 通用 → 关于本机 → 证书信任设置」开启此根证书的完全信任。
  4. 核对下方证书指纹,然后扫描新的配对码。连接地址发生变化时,在电脑重新开启网关。
SHA-256 {status.certificate.sha256}

证书到期 {new Date(status.certificate.expiresAt).toLocaleDateString()}。若无法连接,请核对同一 Wi-Fi、访客网络隔离和 Windows 防火墙的当前端口;这里不会修改系统信任或防火墙。

}

连接一台手机

仅授予选中项目。配对链接两分钟内有效,用过即失效。

允许查看的项目
{projects.map(project => )}
观察任务 · 必选
{pair && status?.enabled &&
手机一次性配对二维码
使用 iPhone 相机扫描

请先完成证书信任。{new Date(pair.expiresAt).toLocaleTimeString()} 失效。

{pair.url}

把链接发给自己时,接收者可以取得所选项目权限。完成配对后此链接立即失效。

}

已登记设备 {status?.devices.length ?? 0}

{!status?.devices.length &&

配对完成后,设备和授权范围会显示在这里。

}{status?.devices.map(device =>
{device.name}

{device.projectIds.map(id => projects.find(project => project.id === id)?.name ?? '原项目').join('、')} · {device.permissions.approve ? '可审批' : '观察'}{device.permissions.stop ? ' · 可停止' : ''}

{({ active: '已授权', revoked: '已撤销', expired: '已到期', reconnect_required: '需重新配对' } as Record)[device.status ?? ''] ?? device.status} · {new Date(device.expiresAt).toLocaleString()} 到期
)}
{!!status?.audit.length &&
最近的设备操作{status.audit.slice(0, 12).map(item =>

{status.devices.find(device => device.id === item.deviceId)?.name ?? '原设备'} · {({ pair: '配对', rotate: '轮换会话', revoke: '撤销', approve: '审批', stop: '停止' } as Record)[item.action] ?? item.action}{item.status}

)}
}
; }