g1t/apps/web/app/components/agent-setup.tsx
| 1 | import { Check, Copy, ExternalLink } from "lucide-react"; |
| 2 | import { type KeyboardEvent, useId, useMemo, useState, useSyncExternalStore } from "react"; |
| 3 | |
| 4 | import { |
| 5 | AGENT_IDS, |
| 6 | type AgentId, |
| 7 | DEFAULT_AGENT, |
| 8 | agentsFor, |
| 9 | cursorInstallLink, |
| 10 | getAgentChoice, |
| 11 | setAgentChoice, |
| 12 | subscribeAgentChoice, |
| 13 | } from "../lib/agent-setup"; |
| 14 | import { useAddresses } from "../lib/addresses"; |
| 15 | import { cn } from "../lib/cn"; |
| 16 | |
| 17 | /** The agent this browser chose last; every toggle on the page shares it. */ |
| 18 | export function useAgentChoice(): [AgentId, (id: AgentId) => void] { |
| 19 | const id = useSyncExternalStore(subscribeAgentChoice, getAgentChoice, () => DEFAULT_AGENT); |
| 20 | return [id, setAgentChoice]; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * How to connect g1t to a coding agent: a tab per agent, the command or |
| 25 | * config for the one chosen, and a copy button. Choosing an agent switches |
| 26 | * every block on the page and is remembered. |
| 27 | */ |
| 28 | export function AgentSetup({ hint = true, className }: { hint?: boolean; className?: string }) { |
| 29 | const [chosen, choose] = useAgentChoice(); |
| 30 | const [copied, setCopied] = useState(false); |
| 31 | const base = useId(); |
| 32 | const { mcp } = useAddresses(); |
| 33 | const agents = useMemo(() => agentsFor(mcp), [mcp]); |
| 34 | const agent = agents[chosen]; |
| 35 | |
| 36 | const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => { |
| 37 | const step = event.key === "ArrowRight" ? 1 : event.key === "ArrowLeft" ? -1 : 0; |
| 38 | if (!step) return; |
| 39 | event.preventDefault(); |
| 40 | const next = AGENT_IDS[(AGENT_IDS.indexOf(chosen) + step + AGENT_IDS.length) % AGENT_IDS.length]; |
| 41 | choose(next); |
| 42 | document.getElementById(`${base}-${next}`)?.focus(); |
| 43 | }; |
| 44 | |
| 45 | const copy = () => { |
| 46 | void navigator.clipboard?.writeText(agent.code).then( |
| 47 | () => { |
| 48 | setCopied(true); |
| 49 | setTimeout(() => setCopied(false), 1500); |
| 50 | }, |
| 51 | () => undefined, |
| 52 | ); |
| 53 | }; |
| 54 | |
| 55 | return ( |
| 56 | <div className={className}> |
| 57 | <div className="@container overflow-hidden rounded-lg border border-line bg-surface"> |
| 58 | <div |
| 59 | role="tablist" |
| 60 | aria-label="Coding agent" |
| 61 | onKeyDown={onKeyDown} |
| 62 | className="flex flex-wrap items-center gap-0.5 border-b border-line bg-bg/40 px-1.5 py-1.5" |
| 63 | > |
| 64 | {AGENT_IDS.map((id) => { |
| 65 | const on = id === chosen; |
| 66 | return ( |
| 67 | <button |
| 68 | key={id} |
| 69 | id={`${base}-${id}`} |
| 70 | type="button" |
| 71 | role="tab" |
| 72 | aria-selected={on} |
| 73 | aria-controls={`${base}-panel`} |
| 74 | tabIndex={on ? 0 : -1} |
| 75 | onClick={() => choose(id)} |
| 76 | className={cn( |
| 77 | "shrink-0 rounded-md px-1.5 py-1 text-xs @sm:px-2.5 font-medium whitespace-nowrap transition-colors outline-none focus-visible:ring-2 focus-visible:ring-accent/50", |
| 78 | on ? "bg-raised text-fg" : "text-muted hover:text-fg", |
| 79 | )} |
| 80 | > |
| 81 | {agents[id].label} |
| 82 | </button> |
| 83 | ); |
| 84 | })} |
| 85 | </div> |
| 86 | <div id={`${base}-panel`} role="tabpanel" aria-labelledby={`${base}-${chosen}`} className="relative"> |
| 87 | {agent.file && ( |
| 88 | <p className="px-3.5 pt-2.5 font-mono text-[0.6875rem] text-faint">{agent.file}</p> |
| 89 | )} |
| 90 | <pre className="overflow-x-auto py-2.5 pr-11 pl-3.5 font-mono text-[0.8125rem] leading-relaxed"> |
| 91 | <code> |
| 92 | {agent.lang === "sh" |
| 93 | ? agent.code.split("\n").map((line, index) => ( |
| 94 | <span key={index} className="block whitespace-pre-wrap [overflow-wrap:anywhere]"> |
| 95 | <span className="mr-2 text-faint select-none">$</span> |
| 96 | {line} |
| 97 | </span> |
| 98 | )) |
| 99 | : agent.code} |
| 100 | </code> |
| 101 | </pre> |
| 102 | <button |
| 103 | type="button" |
| 104 | aria-label={copied ? "Copied" : `Copy the ${agent.label} ${agent.file ? "config" : "command"}`} |
| 105 | onClick={copy} |
| 106 | className="absolute top-1.5 right-1.5 rounded-md p-1.5 text-faint transition-colors hover:bg-raised hover:text-fg" |
| 107 | > |
| 108 | {copied ? <Check size={14} className="text-accent" /> : <Copy size={14} />} |
| 109 | </button> |
| 110 | </div> |
| 111 | </div> |
| 112 | {hint && ( |
| 113 | <p className="mt-2 text-xs leading-5 text-muted"> |
| 114 | {agent.then.split("`").map((part, index) => |
| 115 | index % 2 ? ( |
| 116 | <code key={index} className="font-mono text-fg-soft"> |
| 117 | {part} |
| 118 | </code> |
| 119 | ) : ( |
| 120 | part |
| 121 | ), |
| 122 | )} |
| 123 | {chosen === "cursor" && ( |
| 124 | <> |
| 125 | {" "} |
| 126 | <a href={cursorInstallLink(mcp)} className="inline-flex items-center gap-1 text-fg underline underline-offset-4"> |
| 127 | Add to Cursor <ExternalLink size={11} /> |
| 128 | </a> |
| 129 | </> |
| 130 | )} |
| 131 | </p> |
| 132 | )} |
| 133 | </div> |
| 134 | ); |
| 135 | } |