|
| 1 | +import { type ServerProviderStatus } from "@okcode/contracts"; |
| 2 | +import { memo, useState } from "react"; |
| 3 | +import { useNavigate } from "@tanstack/react-router"; |
| 4 | +import { |
| 5 | + CheckCircle2Icon, |
| 6 | + ChevronDownIcon, |
| 7 | + ChevronRightIcon, |
| 8 | + CircleAlertIcon, |
| 9 | + SettingsIcon, |
| 10 | + TerminalIcon, |
| 11 | + XCircleIcon, |
| 12 | +} from "lucide-react"; |
| 13 | +import { Button } from "../ui/button"; |
| 14 | + |
| 15 | +const PROVIDER_CONFIG = { |
| 16 | + codex: { |
| 17 | + label: "OpenAI (Codex CLI)", |
| 18 | + installCmd: "npm install -g @openai/codex", |
| 19 | + authCmd: "codex login", |
| 20 | + verifyCmd: "codex login status", |
| 21 | + }, |
| 22 | + claudeAgent: { |
| 23 | + label: "Anthropic (Claude Code)", |
| 24 | + installCmd: "npm install -g @anthropic-ai/claude-code", |
| 25 | + authCmd: "claude auth login", |
| 26 | + verifyCmd: "claude auth status", |
| 27 | + }, |
| 28 | +} as const; |
| 29 | + |
| 30 | +function StatusIcon({ status }: { status: ServerProviderStatus["status"] }) { |
| 31 | + switch (status) { |
| 32 | + case "ready": |
| 33 | + return <CheckCircle2Icon className="size-4 text-emerald-500" />; |
| 34 | + case "warning": |
| 35 | + return <CircleAlertIcon className="size-4 text-amber-500" />; |
| 36 | + case "error": |
| 37 | + return <XCircleIcon className="size-4 text-red-400" />; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function ProviderRow({ status }: { status: ServerProviderStatus }) { |
| 42 | + const [expanded, setExpanded] = useState(status.status !== "ready"); |
| 43 | + const config = PROVIDER_CONFIG[status.provider as keyof typeof PROVIDER_CONFIG]; |
| 44 | + if (!config) return null; |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="rounded-lg border border-border bg-card/50 p-3"> |
| 48 | + <button |
| 49 | + type="button" |
| 50 | + className="flex w-full items-center gap-2.5 text-left text-sm" |
| 51 | + onClick={() => setExpanded((v) => !v)} |
| 52 | + > |
| 53 | + <StatusIcon status={status.status} /> |
| 54 | + <span className="flex-1 font-medium text-foreground">{config.label}</span> |
| 55 | + {expanded ? ( |
| 56 | + <ChevronDownIcon className="size-3.5 text-muted-foreground" /> |
| 57 | + ) : ( |
| 58 | + <ChevronRightIcon className="size-3.5 text-muted-foreground" /> |
| 59 | + )} |
| 60 | + </button> |
| 61 | + |
| 62 | + {status.status !== "ready" && status.message && ( |
| 63 | + <p className="mt-1.5 ml-6.5 text-xs text-muted-foreground">{status.message}</p> |
| 64 | + )} |
| 65 | + |
| 66 | + {expanded && status.status !== "ready" && ( |
| 67 | + <div className="mt-3 ml-6.5 space-y-2"> |
| 68 | + <div className="space-y-1.5"> |
| 69 | + <Step n={1} label="Install"> |
| 70 | + <Code>{config.installCmd}</Code> |
| 71 | + </Step> |
| 72 | + <Step n={2} label="Authenticate"> |
| 73 | + <Code>{config.authCmd}</Code> |
| 74 | + </Step> |
| 75 | + <Step n={3} label="Verify"> |
| 76 | + <Code>{config.verifyCmd}</Code> |
| 77 | + </Step> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + )} |
| 81 | + |
| 82 | + {status.status === "ready" && ( |
| 83 | + <p className="mt-1 ml-6.5 text-xs text-emerald-600 dark:text-emerald-400">Ready</p> |
| 84 | + )} |
| 85 | + </div> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +function Step({ n, label, children }: { n: number; label: string; children: React.ReactNode }) { |
| 90 | + return ( |
| 91 | + <div className="flex items-baseline gap-2 text-xs"> |
| 92 | + <span className="shrink-0 text-muted-foreground"> |
| 93 | + {n}. {label}: |
| 94 | + </span> |
| 95 | + {children} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function Code({ children }: { children: React.ReactNode }) { |
| 101 | + return ( |
| 102 | + <code className="rounded bg-muted px-1.5 py-0.5 font-mono text-[11px] text-foreground"> |
| 103 | + {children} |
| 104 | + </code> |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +export const ProviderSetupCard = memo(function ProviderSetupCard({ |
| 109 | + providers, |
| 110 | +}: { |
| 111 | + providers: ReadonlyArray<ServerProviderStatus>; |
| 112 | +}) { |
| 113 | + const navigate = useNavigate(); |
| 114 | + const readyCount = providers.filter((p) => p.status === "ready").length; |
| 115 | + |
| 116 | + // Don't show if all providers are ready |
| 117 | + if (readyCount === providers.length && providers.length > 0) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return ( |
| 122 | + <div className="w-full max-w-md space-y-4"> |
| 123 | + <div className="space-y-1.5 text-center"> |
| 124 | + <div className="mx-auto mb-3 flex size-10 items-center justify-center rounded-xl border border-border bg-card shadow-sm"> |
| 125 | + <TerminalIcon className="size-5 text-foreground" /> |
| 126 | + </div> |
| 127 | + <h2 className="text-lg font-semibold text-foreground">Set up a provider</h2> |
| 128 | + <p className="text-sm text-muted-foreground"> |
| 129 | + {readyCount === 0 |
| 130 | + ? "Connect at least one AI provider to start coding." |
| 131 | + : `${readyCount} of ${providers.length} providers ready. Set up another provider or start coding.`} |
| 132 | + </p> |
| 133 | + </div> |
| 134 | + |
| 135 | + <div className="space-y-2"> |
| 136 | + {providers.map((status) => ( |
| 137 | + <ProviderRow key={status.provider} status={status} /> |
| 138 | + ))} |
| 139 | + </div> |
| 140 | + |
| 141 | + <div className="flex items-center justify-center gap-2 pt-1"> |
| 142 | + <Button variant="outline" size="sm" onClick={() => void navigate({ to: "/settings" })}> |
| 143 | + <SettingsIcon /> |
| 144 | + Settings |
| 145 | + </Button> |
| 146 | + </div> |
| 147 | + |
| 148 | + <p className="text-center text-xs text-muted-foreground/60"> |
| 149 | + Run <Code>npx okcode doctor</Code> to diagnose setup issues from the terminal. |
| 150 | + </p> |
| 151 | + </div> |
| 152 | + ); |
| 153 | +}); |
0 commit comments