|
| 1 | +'use client' |
| 2 | + |
| 3 | +import * as React from 'react' |
| 4 | +import { AlertCircleIcon, CheckCircle2Icon, CheckIcon } from 'lucide-react' |
| 5 | +import { match } from 'ts-pattern' |
| 6 | + |
| 7 | +import { isCompleted, useProgress } from '@/lib/progress/utils' |
| 8 | +import { AppCardSection } from '@/components/app/app-card' |
| 9 | +import { Callout } from '@/components/base/callout' |
| 10 | +import { Button } from '@/components/ui/button' |
| 11 | +import { Skeleton } from '@/components/ui/skeleton' |
| 12 | +import { Spinner } from '@/components/ui/spinner' |
| 13 | +import { TextCopyButton } from '@/app/(model)/text-copy-button' |
| 14 | +import NexusGateLogo from '@/public/icons/nexus-gate.svg' |
| 15 | +import { useModelStore } from '@/stores/model-store-provider' |
| 16 | + |
| 17 | +import { ProgressIndicator } from '../progress-indicator' |
| 18 | +import { useServiceDeployContext } from '../service-deploy-provider' |
| 19 | +import { useHostInfo } from '../use-host-info' |
| 20 | + |
| 21 | +export function NexusGateDeployPage() { |
| 22 | + const { deployMutation } = useServiceDeployContext() |
| 23 | + const isSuccess = useModelStore( |
| 24 | + (s) => |
| 25 | + s.serviceDeploy.progress.nexusGate.size > 0 && s.serviceDeploy.progress.nexusGate.values().every(isCompleted), |
| 26 | + ) |
| 27 | + |
| 28 | + if (deployMutation.isError) { |
| 29 | + return ( |
| 30 | + <AppCardSection> |
| 31 | + <Callout |
| 32 | + size="card" |
| 33 | + action={ |
| 34 | + <Button variant="outline" size="xs" onClick={() => deployMutation.mutate()}> |
| 35 | + 重试 |
| 36 | + </Button> |
| 37 | + } |
| 38 | + > |
| 39 | + {deployMutation.error.message} |
| 40 | + </Callout> |
| 41 | + </AppCardSection> |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + return ( |
| 46 | + <AppCardSection> |
| 47 | + <HostsList /> |
| 48 | + {isSuccess && <DeploySuccessCallout />} |
| 49 | + </AppCardSection> |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +function HostsList() { |
| 54 | + const configs = useModelStore((s) => s.serviceDeploy.config.nexusGate) |
| 55 | + const hosts = Array.from(configs.values()) |
| 56 | + return ( |
| 57 | + <div className="grid gap-4"> |
| 58 | + {hosts.map(({ host }) => ( |
| 59 | + <HostStatusCard key={host} hostId={host} /> |
| 60 | + ))} |
| 61 | + </div> |
| 62 | + ) |
| 63 | +} |
| 64 | + |
| 65 | +function HostStatusCard({ hostId }: { hostId: string }) { |
| 66 | + const { data: host } = useHostInfo({ hostId }) |
| 67 | + const config = useModelStore((s) => s.serviceDeploy.config.nexusGate.get(hostId)) |
| 68 | + const deployProgress = useModelStore((s) => s.serviceDeploy.progress.nexusGate.get(hostId)) |
| 69 | + const progress = useProgress(deployProgress) |
| 70 | + |
| 71 | + const { deployOneMutation } = useServiceDeployContext() |
| 72 | + |
| 73 | + if (!progress || !config) return null |
| 74 | + |
| 75 | + const ipAddr = host?.ip[0] |
| 76 | + const url = ipAddr ? `http://${ipAddr}:${config.port}` : undefined |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="grid grid-cols-[1fr_auto] gap-y-1 rounded-xl border px-4 py-3"> |
| 80 | + <div className="flex items-baseline gap-3"> |
| 81 | + <h4 className="text-base font-medium"> |
| 82 | + {host?.info.system_info.hostname ?? <Skeleton className="h-6 w-32" />} |
| 83 | + </h4> |
| 84 | + <div className="text-muted-foreground text-sm">{host?.ip[0]}</div> |
| 85 | + </div> |
| 86 | + <div className="col-start-2 row-span-2 pt-1"> |
| 87 | + <NexusGateLogo className="-m-0.5 size-8" /> |
| 88 | + </div> |
| 89 | + <div>NexusGate</div> |
| 90 | + <ProgressIndicator progress={progress} /> |
| 91 | + <div className="text-muted-foreground flex gap-2 [&_svg]:size-4 [&_svg]:shrink-0 [&>svg]:translate-y-0.5"> |
| 92 | + {match(progress.status) |
| 93 | + .with('done', () => ( |
| 94 | + <> |
| 95 | + <CheckIcon className="text-success" /> |
| 96 | + <div className="text-success">{progress.message}</div> |
| 97 | + </> |
| 98 | + )) |
| 99 | + .with('running', () => ( |
| 100 | + <> |
| 101 | + <Spinner /> |
| 102 | + <div>{progress.message}</div> |
| 103 | + </> |
| 104 | + )) |
| 105 | + .with('error', () => ( |
| 106 | + <> |
| 107 | + <AlertCircleIcon className="text-destructive" /> |
| 108 | + <div className="text-destructive">{progress.message}</div> |
| 109 | + <button |
| 110 | + className="text-primary hover:text-primary/90 shrink-0 font-medium whitespace-nowrap" |
| 111 | + onClick={() => deployOneMutation.mutate({ host: hostId, service: 'nexusGate' })} |
| 112 | + > |
| 113 | + 重试 |
| 114 | + </button> |
| 115 | + </> |
| 116 | + )) |
| 117 | + .with('idle', () => <div>{progress.message}</div>) |
| 118 | + .exhaustive()} |
| 119 | + </div> |
| 120 | + {url && progress.status === 'done' && ( |
| 121 | + <div className="col-span-full flex"> |
| 122 | + <div className="text-foreground border-border/50 grid grid-cols-[auto_auto] gap-x-2.5 gap-y-1 rounded-md border px-3 py-2.5"> |
| 123 | + <dl className="contents"> |
| 124 | + <dt className="text-muted-foreground">服务访问地址</dt> |
| 125 | + <dd> |
| 126 | + <a href={url} target="_blank" className="text-primary font-medium hover:underline"> |
| 127 | + {url} |
| 128 | + </a> |
| 129 | + </dd> |
| 130 | + </dl> |
| 131 | + <dl className="contents"> |
| 132 | + <dt className="text-muted-foreground">管理员密钥</dt> |
| 133 | + <dd> |
| 134 | + <TextCopyButton value={config.adminKey} message="已复制管理员密钥"> |
| 135 | + {config.adminKey} |
| 136 | + </TextCopyButton> |
| 137 | + </dd> |
| 138 | + </dl> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +function DeploySuccessCallout() { |
| 147 | + return ( |
| 148 | + <Callout size="card" variant="success" icon={<CheckCircle2Icon />}> |
| 149 | + NexusGate 部署完成 |
| 150 | + </Callout> |
| 151 | + ) |
| 152 | +} |
0 commit comments