|
| 1 | +import { motion } from "framer-motion"; |
| 2 | +import { Loader, RefreshCw, Sparkles } from "lucide-react"; |
| 3 | +import { useCallback, useEffect, useMemo, useState } from "react"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { Muted } from "@/components/ui/typography"; |
| 6 | +import { useInstances } from "@/hooks/useInstances"; |
| 7 | +import { COLOR } from "@/lib/constants"; |
| 8 | +import { |
| 9 | + type DiscoveredInstance, |
| 10 | + discoverHonchoInstances, |
| 11 | + suggestNameForInstance, |
| 12 | +} from "@/lib/discovery"; |
| 13 | + |
| 14 | +interface Row { |
| 15 | + discovered: DiscoveredInstance; |
| 16 | + suggestedName: string; |
| 17 | + checked: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +interface Props { |
| 21 | + /** If true, scan as soon as the component mounts. */ |
| 22 | + autoRun?: boolean; |
| 23 | + /** Called after the user has added at least one instance. */ |
| 24 | + onAdded?: () => void; |
| 25 | +} |
| 26 | + |
| 27 | +export function DiscoveredInstances({ autoRun = false, onAdded }: Props) { |
| 28 | + const { instances, add, activate } = useInstances(); |
| 29 | + const [scanning, setScanning] = useState(false); |
| 30 | + const [hasScanned, setHasScanned] = useState(false); |
| 31 | + const [rows, setRows] = useState<Row[]>([]); |
| 32 | + |
| 33 | + const existingBaseUrls = useMemo( |
| 34 | + () => new Set(instances.map((i) => i.baseUrl.replace(/\/+$/, "").toLowerCase())), |
| 35 | + [instances], |
| 36 | + ); |
| 37 | + |
| 38 | + const runScan = useCallback(async () => { |
| 39 | + setScanning(true); |
| 40 | + try { |
| 41 | + const found = await discoverHonchoInstances(); |
| 42 | + const fresh = found.filter( |
| 43 | + (d) => !existingBaseUrls.has(d.base_url.replace(/\/+$/, "").toLowerCase()), |
| 44 | + ); |
| 45 | + const named = await Promise.all( |
| 46 | + fresh.map(async (d) => { |
| 47 | + const name = (await suggestNameForInstance(d.base_url)) ?? `Honcho :${d.port}`; |
| 48 | + return { discovered: d, suggestedName: name, checked: true } satisfies Row; |
| 49 | + }), |
| 50 | + ); |
| 51 | + setRows(named); |
| 52 | + } finally { |
| 53 | + setScanning(false); |
| 54 | + setHasScanned(true); |
| 55 | + } |
| 56 | + }, [existingBaseUrls]); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (autoRun) void runScan(); |
| 60 | + }, [autoRun, runScan]); |
| 61 | + |
| 62 | + function setRowChecked(port: number, checked: boolean) { |
| 63 | + setRows((r) => r.map((row) => (row.discovered.port === port ? { ...row, checked } : row))); |
| 64 | + } |
| 65 | + |
| 66 | + function setRowName(port: number, suggestedName: string) { |
| 67 | + setRows((r) => |
| 68 | + r.map((row) => (row.discovered.port === port ? { ...row, suggestedName } : row)), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + function addSelected() { |
| 73 | + const selected = rows.filter((r) => r.checked); |
| 74 | + if (selected.length === 0) return; |
| 75 | + let firstId: string | null = null; |
| 76 | + for (const row of selected) { |
| 77 | + const created = add({ |
| 78 | + name: row.suggestedName.trim() || `Honcho :${row.discovered.port}`, |
| 79 | + baseUrl: row.discovered.base_url, |
| 80 | + token: "", |
| 81 | + }); |
| 82 | + if (firstId === null) firstId = created.id; |
| 83 | + } |
| 84 | + if (firstId) activate(firstId); |
| 85 | + setRows([]); |
| 86 | + onAdded?.(); |
| 87 | + } |
| 88 | + |
| 89 | + const selectedCount = rows.filter((r) => r.checked).length; |
| 90 | + |
| 91 | + return ( |
| 92 | + <div |
| 93 | + className="rounded-2xl p-5 space-y-3" |
| 94 | + style={{ background: "var(--bg-2)", border: "1px solid var(--border)" }} |
| 95 | + > |
| 96 | + <div className="flex items-center justify-between gap-3"> |
| 97 | + <div className="flex items-center gap-2"> |
| 98 | + <Sparkles |
| 99 | + className="w-4 h-4 shrink-0" |
| 100 | + style={{ color: COLOR.accentText }} |
| 101 | + strokeWidth={1.5} |
| 102 | + /> |
| 103 | + <div> |
| 104 | + <p className="text-sm font-medium" style={{ color: "var(--text-1)" }}> |
| 105 | + Discover local Honcho instances |
| 106 | + </p> |
| 107 | + <Muted className="text-xs">Scans 127.0.0.1:8000–8100 for running instances</Muted> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + <Button |
| 111 | + type="button" |
| 112 | + variant="ghost" |
| 113 | + onClick={() => void runScan()} |
| 114 | + disabled={scanning} |
| 115 | + className="rounded-xl px-3 py-2" |
| 116 | + title="Rescan" |
| 117 | + > |
| 118 | + {scanning ? ( |
| 119 | + <motion.div |
| 120 | + animate={{ rotate: 360 }} |
| 121 | + transition={{ |
| 122 | + duration: 1, |
| 123 | + repeat: Number.POSITIVE_INFINITY, |
| 124 | + ease: "linear", |
| 125 | + }} |
| 126 | + > |
| 127 | + <Loader className="w-4 h-4" strokeWidth={1.5} /> |
| 128 | + </motion.div> |
| 129 | + ) : ( |
| 130 | + <RefreshCw className="w-4 h-4" strokeWidth={1.5} /> |
| 131 | + )} |
| 132 | + <span className="hidden sm:inline ml-1.5 text-xs"> |
| 133 | + {scanning ? "Scanning…" : "Rescan"} |
| 134 | + </span> |
| 135 | + </Button> |
| 136 | + </div> |
| 137 | + |
| 138 | + {hasScanned && !scanning && rows.length === 0 && ( |
| 139 | + <Muted className="text-xs"> |
| 140 | + No new instances found. Add one manually below if you know its URL. |
| 141 | + </Muted> |
| 142 | + )} |
| 143 | + |
| 144 | + {rows.length > 0 && ( |
| 145 | + <> |
| 146 | + <div className="space-y-1.5"> |
| 147 | + {rows.map((row) => ( |
| 148 | + <DiscoveredRow |
| 149 | + key={row.discovered.port} |
| 150 | + row={row} |
| 151 | + onCheck={(c) => setRowChecked(row.discovered.port, c)} |
| 152 | + onRename={(n) => setRowName(row.discovered.port, n)} |
| 153 | + /> |
| 154 | + ))} |
| 155 | + </div> |
| 156 | + <Button |
| 157 | + type="button" |
| 158 | + variant="accent" |
| 159 | + onClick={addSelected} |
| 160 | + disabled={selectedCount === 0} |
| 161 | + className="w-full rounded-xl py-2.5" |
| 162 | + > |
| 163 | + {selectedCount === 0 |
| 164 | + ? "Select at least one" |
| 165 | + : `Add ${selectedCount} instance${selectedCount === 1 ? "" : "s"}`} |
| 166 | + </Button> |
| 167 | + </> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +interface DiscoveredRowProps { |
| 174 | + row: Row; |
| 175 | + onCheck: (checked: boolean) => void; |
| 176 | + onRename: (name: string) => void; |
| 177 | +} |
| 178 | + |
| 179 | +function DiscoveredRow({ row, onCheck, onRename }: DiscoveredRowProps) { |
| 180 | + return ( |
| 181 | + <div |
| 182 | + className="flex items-center gap-2.5 rounded-xl px-3 py-2" |
| 183 | + style={{ |
| 184 | + background: row.checked ? "var(--surface)" : "transparent", |
| 185 | + border: `1px solid ${row.checked ? "var(--accent-border)" : "var(--border)"}`, |
| 186 | + }} |
| 187 | + > |
| 188 | + <input |
| 189 | + type="checkbox" |
| 190 | + checked={row.checked} |
| 191 | + onChange={(e) => onCheck(e.target.checked)} |
| 192 | + className="w-4 h-4 shrink-0 cursor-pointer" |
| 193 | + aria-label={`Select ${row.suggestedName}`} |
| 194 | + /> |
| 195 | + <input |
| 196 | + type="text" |
| 197 | + value={row.suggestedName} |
| 198 | + onChange={(e) => onRename(e.target.value)} |
| 199 | + className="flex-1 min-w-0 bg-transparent text-sm font-medium border-0 outline-none px-1 py-0.5 rounded" |
| 200 | + style={{ color: "var(--text-1)" }} |
| 201 | + aria-label={`Name for instance on port ${row.discovered.port}`} |
| 202 | + disabled={!row.checked} |
| 203 | + /> |
| 204 | + <span className="text-xs font-mono shrink-0" style={{ color: "var(--text-4)" }}> |
| 205 | + :{row.discovered.port} |
| 206 | + </span> |
| 207 | + </div> |
| 208 | + ); |
| 209 | +} |
0 commit comments