import React, { useEffect, useState } from 'react' import { Typography, Button, Space, Collapse, Spin, Message, Tag } from '@arco-design/web-react' import { IconCopy, IconRefresh } from '@arco-design/web-react/icon' import { fetchScriptPreview } from '../../../services/nodes' import type { InstallTokenResult, InstallMode } from '../../../types/nodes' import { buildAgentDownloadCommand, buildAgentInstallCommand } from '../installCommands' const { Text } = Typography interface Props { nodeId: number nodeName: string token: InstallTokenResult mode: InstallMode previewParams: { mode: string; arch: string; agentVersion: string; downloadSrc: string } onRegenerate: () => void } export function Step3CommandPreview({ nodeId, nodeName, token, mode, previewParams, onRegenerate }: Props) { const [remaining, setRemaining] = useState(0) const [preview, setPreview] = useState('') const [loadingPreview, setLoadingPreview] = useState(false) useEffect(() => { const expires = new Date(token.expiresAt).getTime() const tick = () => setRemaining(Math.max(0, Math.floor((expires - Date.now()) / 1000))) tick() const id = setInterval(tick, 1000) return () => clearInterval(id) }, [token.expiresAt]) const expired = remaining === 0 const command = buildAgentInstallCommand(token.url, token.fallbackUrl, token.scriptBase64) const fallbackCommand = buildAgentDownloadCommand(token.url, token.fallbackUrl, token.scriptBase64) const dockerComposeCmd = mode === 'docker' && token.composeUrl ? `curl -fsSL ${token.composeUrl} -o docker-compose.yml && docker-compose up -d` : null const copy = async (s: string) => { await navigator.clipboard.writeText(s) Message.success('已复制') } const loadPreview = async () => { setLoadingPreview(true) try { const text = await fetchScriptPreview(nodeId, previewParams) setPreview(text) } catch { Message.error('预览加载失败') } finally { setLoadingPreview(false) } } return (
节点: {nodeName} {expired ? '已过期' : `有效期 ${Math.floor(remaining / 60)}:${String(remaining % 60).padStart(2, '0')}`}
{command}
{expired && }
或固定下载到 /tmp 后执行: {fallbackCommand}
{dockerComposeCmd && (
或使用 docker-compose: {dockerComposeCmd}
)} 安装命令包含节点 token,请仅在目标机执行并妥善保存;公开安装链接会在 TTL 到期或首次消费后作废。 { if (keys.includes('preview') && !preview) loadPreview() }}> {loadingPreview ? : (
{preview}
)}
) }