|
| 1 | +import { Link } from "@tanstack/react-router"; |
| 2 | +import { motion } from "framer-motion"; |
| 3 | +import { Network, Server, Settings as SettingsIcon } from "lucide-react"; |
| 4 | +import { useCallback, useMemo, useState } from "react"; |
| 5 | +import { EmptyState } from "@/components/shared/EmptyState"; |
| 6 | +import { Body, PageTitle, SectionHeading } from "@/components/ui/typography"; |
| 7 | +import { useInstances } from "@/hooks/useInstances"; |
| 8 | +import { COLOR } from "@/lib/constants"; |
| 9 | +import { formatCount } from "@/lib/utils"; |
| 10 | +import { FleetRow } from "./FleetRow"; |
| 11 | +import { |
| 12 | + computeFleetAggregates, |
| 13 | + DEFAULT_ROW_METRICS, |
| 14 | + type FleetRowMetrics, |
| 15 | +} from "./fleetAggregates"; |
| 16 | + |
| 17 | +export function FleetDashboard() { |
| 18 | + const { instances } = useInstances(); |
| 19 | + const [metricsById, setMetricsById] = useState<Record<string, FleetRowMetrics>>({}); |
| 20 | + |
| 21 | + const setMetrics = useCallback((id: string, m: FleetRowMetrics) => { |
| 22 | + setMetricsById((prev) => ({ ...prev, [id]: m })); |
| 23 | + }, []); |
| 24 | + |
| 25 | + const rows = useMemo( |
| 26 | + () => instances.map((i) => metricsById[i.id] ?? DEFAULT_ROW_METRICS), |
| 27 | + [instances, metricsById], |
| 28 | + ); |
| 29 | + const agg = useMemo(() => computeFleetAggregates(rows), [rows]); |
| 30 | + |
| 31 | + if (instances.length === 0) { |
| 32 | + return ( |
| 33 | + <div className="page-container page-container--xl"> |
| 34 | + <EmptyState |
| 35 | + icon={Network} |
| 36 | + title="No instances configured" |
| 37 | + description="Add at least one Honcho instance in Settings to use the Fleet view." |
| 38 | + action={ |
| 39 | + <Link |
| 40 | + to="/settings" |
| 41 | + className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-md" |
| 42 | + style={{ |
| 43 | + background: "var(--accent-dim)", |
| 44 | + border: "1px solid var(--accent-border)", |
| 45 | + color: "var(--accent-text)", |
| 46 | + }} |
| 47 | + > |
| 48 | + <SettingsIcon className="w-4 h-4" strokeWidth={1.5} /> |
| 49 | + Go to Settings |
| 50 | + </Link> |
| 51 | + } |
| 52 | + /> |
| 53 | + </div> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="page-container page-container--xl"> |
| 59 | + <motion.div initial={{ opacity: 0, y: -8 }} animate={{ opacity: 1, y: 0 }} className="mb-8"> |
| 60 | + <div className="flex items-center gap-2 mb-1"> |
| 61 | + <Network className="w-5 h-5" style={{ color: "var(--accent)" }} strokeWidth={1.5} /> |
| 62 | + <PageTitle>Fleet</PageTitle> |
| 63 | + <span |
| 64 | + className="ml-1 text-xs font-mono px-2 py-0.5 rounded-full" |
| 65 | + style={{ |
| 66 | + background: COLOR.accentSubtle, |
| 67 | + color: COLOR.accentText, |
| 68 | + border: `1px solid ${COLOR.accentBorder}`, |
| 69 | + }} |
| 70 | + > |
| 71 | + {agg.totalInstances} agent{agg.totalInstances !== 1 ? "s" : ""} |
| 72 | + </span> |
| 73 | + </div> |
| 74 | + <Body className="leading-none">Cross-instance overview of all configured agents</Body> |
| 75 | + </motion.div> |
| 76 | + |
| 77 | + <motion.div |
| 78 | + initial={{ opacity: 0, y: 8 }} |
| 79 | + animate={{ opacity: 1, y: 0 }} |
| 80 | + transition={{ delay: 0.05 }} |
| 81 | + className="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-4" |
| 82 | + > |
| 83 | + <MetricCard label="Workspaces" value={agg.totalWorkspaces} /> |
| 84 | + <MetricCard label="Conclusions" value={agg.totalConclusions} accent /> |
| 85 | + <MetricCard |
| 86 | + label="Healthy" |
| 87 | + value={agg.healthyCount} |
| 88 | + total={agg.totalInstances} |
| 89 | + color={agg.healthyCount === agg.totalInstances ? COLOR.success : COLOR.warning} |
| 90 | + /> |
| 91 | + <MetricCard |
| 92 | + label="Unreachable" |
| 93 | + value={agg.unreachableCount} |
| 94 | + color={agg.unreachableCount > 0 ? COLOR.destructive : "var(--text-3)"} |
| 95 | + /> |
| 96 | + </motion.div> |
| 97 | + |
| 98 | + <motion.div |
| 99 | + initial={{ opacity: 0, y: 8 }} |
| 100 | + animate={{ opacity: 1, y: 0 }} |
| 101 | + transition={{ delay: 0.12 }} |
| 102 | + className="rounded-xl theme-card overflow-hidden" |
| 103 | + > |
| 104 | + <div |
| 105 | + className="flex items-center gap-2 px-4 py-3" |
| 106 | + style={{ borderBottom: "1px solid var(--border)" }} |
| 107 | + > |
| 108 | + <Server className="w-4 h-4" style={{ color: "var(--accent)" }} strokeWidth={1.5} /> |
| 109 | + <SectionHeading className="mb-0">Agents</SectionHeading> |
| 110 | + <span className="text-xs ml-1" style={{ color: "var(--text-4)" }}> |
| 111 | + all configured instances · queue updates every 10s |
| 112 | + </span> |
| 113 | + </div> |
| 114 | + |
| 115 | + <div className="overflow-x-auto"> |
| 116 | + <table className="w-full text-xs"> |
| 117 | + <thead> |
| 118 | + <tr style={{ background: "var(--bg-3)" }}> |
| 119 | + <th className="py-2 px-4 font-medium text-left" style={{ color: "var(--text-3)" }}> |
| 120 | + Agent |
| 121 | + </th> |
| 122 | + <th className="py-2 px-4 font-medium text-right" style={{ color: "var(--text-3)" }}> |
| 123 | + Workspaces |
| 124 | + </th> |
| 125 | + <th className="py-2 px-4 font-medium text-right" style={{ color: "var(--text-3)" }}> |
| 126 | + Conclusions |
| 127 | + </th> |
| 128 | + <th |
| 129 | + className="py-2 px-4 font-medium text-right" |
| 130 | + style={{ color: "var(--text-3)" }} |
| 131 | + title="Active / Pending queue work units" |
| 132 | + > |
| 133 | + Queue (a/p) |
| 134 | + </th> |
| 135 | + <th className="py-2 px-4 font-medium text-right" style={{ color: "var(--text-3)" }}> |
| 136 | + Last seen |
| 137 | + </th> |
| 138 | + </tr> |
| 139 | + </thead> |
| 140 | + <tbody> |
| 141 | + {instances.map((inst) => ( |
| 142 | + <FleetRow key={inst.id} instance={inst} onMetrics={setMetrics} /> |
| 143 | + ))} |
| 144 | + </tbody> |
| 145 | + </table> |
| 146 | + </div> |
| 147 | + </motion.div> |
| 148 | + </div> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +interface MetricCardProps { |
| 153 | + label: string; |
| 154 | + value: number; |
| 155 | + total?: number; |
| 156 | + color?: string; |
| 157 | + accent?: boolean; |
| 158 | +} |
| 159 | + |
| 160 | +function MetricCard({ label, value, total, color, accent }: MetricCardProps) { |
| 161 | + const valueColor = color ?? (accent ? COLOR.accentText : "var(--text-1)"); |
| 162 | + return ( |
| 163 | + <div className="rounded-xl p-4 theme-card"> |
| 164 | + <div className="text-2xl font-semibold font-mono" style={{ color: valueColor }}> |
| 165 | + {formatCount(value)} |
| 166 | + {total !== undefined && ( |
| 167 | + <span className="text-base ml-1" style={{ color: "var(--text-4)" }}> |
| 168 | + / {formatCount(total)} |
| 169 | + </span> |
| 170 | + )} |
| 171 | + </div> |
| 172 | + <div className="text-xs mt-0.5" style={{ color: "var(--text-3)" }}> |
| 173 | + {label} |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments