| 1 | import { Check, Copy, LoaderCircle } from "lucide-react"; |
| 2 | import { type ComponentProps, type ReactNode, useState } from "react"; |
| 3 | import { Link, type LinkProps, NavLink, useLocation, useNavigation } from "react-router"; |
| 4 | |
| 5 | import { isWaitingMessage, linkPaths } from "../../lib/compute"; |
| 6 | import { type Submission, isPending } from "../../lib/pending"; |
| 7 | import { Mark } from "../logo"; |
| 8 | |
| 9 | export function Field({ |
| 10 | label, |
| 11 | hint, |
| 12 | children, |
| 13 | }: { |
| 14 | label: string; |
| 15 | hint?: string; |
| 16 | children: ReactNode; |
| 17 | }) { |
| 18 | return ( |
| 19 | <label className="block"> |
| 20 | <span className="mb-1.5 block text-sm font-medium text-muted"> |
| 21 | {label} |
| 22 | </span> |
| 23 | {children} |
| 24 | {hint && <span className="mt-1.5 block text-xs text-faint">{hint}</span>} |
| 25 | </label> |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | const CONTROL = |
| 30 | "w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim"; |
| 31 | |
| 32 | /** |
| 33 | * Attributes for a field that is not part of signing in. Password managers |
| 34 | * guess from names such as "name", "title" and "username" and offer to |
| 35 | * fill a login into them; these tell 1Password, LastPass, Bitwarden and |
| 36 | * Dashlane to leave the field alone. A field that states what it takes with |
| 37 | * `autoComplete` is left to them. |
| 38 | */ |
| 39 | export function notACredential(autoComplete?: string) { |
| 40 | if (autoComplete) return {}; |
| 41 | return { |
| 42 | autoComplete: "off", |
| 43 | "data-1p-ignore": true, |
| 44 | "data-lpignore": "true", |
| 45 | "data-bwignore": true, |
| 46 | "data-form-type": "other", |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | export function Input(props: ComponentProps<"input">) { |
| 51 | return <input {...notACredential(props.autoComplete)} {...props} className={CONTROL} />; |
| 52 | } |
| 53 | |
| 54 | export function Textarea(props: ComponentProps<"textarea">) { |
| 55 | return ( |
| 56 | <textarea |
| 57 | {...notACredential(props.autoComplete)} |
| 58 | {...props} |
| 59 | className={`${CONTROL} font-mono`} |
| 60 | /> |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** One tab in the row under a repository's or a workspace's header. */ |
| 65 | export function TabLink({ |
| 66 | to, |
| 67 | also, |
| 68 | end, |
| 69 | icon, |
| 70 | count, |
| 71 | children, |
| 72 | }: { |
| 73 | to: string; |
| 74 | /** Another path prefix under which this tab is the current one. */ |
| 75 | also?: string; |
| 76 | end?: boolean; |
| 77 | icon: ReactNode; |
| 78 | count?: number; |
| 79 | children: ReactNode; |
| 80 | }) { |
| 81 | const { pathname } = useLocation(); |
| 82 | return ( |
| 83 | <NavLink |
| 84 | to={to} |
| 85 | end={end} |
| 86 | className={({ isActive }) => |
| 87 | `-mb-px flex items-center gap-2 border-b-2 px-3 pb-3 text-sm whitespace-nowrap transition-colors ${ |
| 88 | isActive || (also && pathname.startsWith(also + "/")) |
| 89 | ? "border-accent font-medium text-fg" |
| 90 | : "border-transparent text-muted hover:text-fg" |
| 91 | }` |
| 92 | } |
| 93 | > |
| 94 | {icon} |
| 95 | {children} |
| 96 | {count != null && count > 0 && ( |
| 97 | <span className="rounded-full bg-raised px-1.5 py-px text-xs text-muted"> |
| 98 | {count} |
| 99 | </span> |
| 100 | )} |
| 101 | </NavLink> |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | type Variant = "primary" | "accent" | "quiet" | "danger"; |
| 106 | |
| 107 | const BUTTON_BASE = |
| 108 | "inline-flex items-center justify-center gap-2 rounded-md px-3.5 py-2 text-sm font-medium transition-colors disabled:opacity-50"; |
| 109 | |
| 110 | const BUTTON_VARIANTS: Record<Variant, string> = { |
| 111 | primary: "bg-fg text-bg hover:bg-white", |
| 112 | accent: "bg-accent text-bg hover:bg-[#aaf5d6]", |
| 113 | quiet: |
| 114 | "border border-line text-fg/80 hover:border-line-strong hover:bg-surface hover:text-fg", |
| 115 | // For what cannot be undone: transferring, deleting. |
| 116 | danger: "border border-danger/40 text-danger hover:border-danger hover:bg-danger/10", |
| 117 | }; |
| 118 | |
| 119 | export function Button({ |
| 120 | variant = "primary", |
| 121 | ...props |
| 122 | }: ComponentProps<"button"> & { variant?: Variant }) { |
| 123 | return ( |
| 124 | <button {...props} className={`${BUTTON_BASE} ${BUTTON_VARIANTS[variant]}`} /> |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Whether the submission `fields` names is still working (lib/pending.ts): |
| 130 | * the page's own navigation, or `fetcher`'s when the form is a fetcher's. |
| 131 | */ |
| 132 | export function usePending(fields?: Record<string, string | null | undefined>, fetcher?: Submission): boolean { |
| 133 | const navigation = useNavigation(); |
| 134 | return isPending(fetcher ?? navigation, fields); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * A form's submit button that says it is working: turned off, with a |
| 139 | * spinner and `pending` ("Saving…") in place of its words, from the moment |
| 140 | * it is pressed until the page has loaded what it changed. Its own |
| 141 | * `name`/`value` say which submission is its; `match` names it otherwise, |
| 142 | * such as the form's hidden `intent`. `fetcher` when the form is a |
| 143 | * fetcher's. `className` replaces the button look, for icon buttons, and |
| 144 | * `icon` says the spinner takes the place of everything inside it. Words |
| 145 | * with a leading icon should pass `pending`, so the spinner replaces both. |
| 146 | */ |
| 147 | export function SubmitButton({ |
| 148 | variant = "primary", |
| 149 | pending, |
| 150 | match, |
| 151 | fetcher, |
| 152 | busy, |
| 153 | icon, |
| 154 | disabled, |
| 155 | className, |
| 156 | children, |
| 157 | ...props |
| 158 | }: Omit<ComponentProps<"button">, "type"> & { |
| 159 | variant?: Variant; |
| 160 | /** The words while it works, such as "Saving…"; its own words when absent. */ |
| 161 | pending?: ReactNode; |
| 162 | match?: Record<string, string | null | undefined>; |
| 163 | fetcher?: Submission; |
| 164 | /** Working for a reason this button cannot see by itself. */ |
| 165 | busy?: boolean; |
| 166 | /** An icon button: while it works, the spinner is all it shows. */ |
| 167 | icon?: boolean; |
| 168 | }) { |
| 169 | const own = |
| 170 | typeof props.name === "string" && props.value != null ? { [props.name]: String(props.value) } : undefined; |
| 171 | const working = usePending({ ...own, ...match }, fetcher) || Boolean(busy); |
| 172 | return ( |
| 173 | <button |
| 174 | {...props} |
| 175 | type="submit" |
| 176 | disabled={disabled || working} |
| 177 | aria-busy={working || undefined} |
| 178 | className={className ?? `${BUTTON_BASE} ${BUTTON_VARIANTS[variant]}`} |
| 179 | > |
| 180 | {working ? ( |
| 181 | <> |
| 182 | <LoaderCircle size={14} aria-hidden="true" className="shrink-0 animate-spin" /> |
| 183 | {icon ? null : (pending ?? children)} |
| 184 | </> |
| 185 | ) : ( |
| 186 | children |
| 187 | )} |
| 188 | </button> |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** A link that looks like a button. */ |
| 193 | export function ButtonLink({ |
| 194 | variant = "primary", |
| 195 | large, |
| 196 | ...props |
| 197 | }: LinkProps & { variant?: Variant; large?: boolean }) { |
| 198 | return ( |
| 199 | <Link |
| 200 | {...props} |
| 201 | className={`${BUTTON_BASE} ${BUTTON_VARIANTS[variant]} ${ |
| 202 | large ? "rounded-full px-5 py-2.5" : "" |
| 203 | }`} |
| 204 | /> |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * A message with the g1t pages it names as links, such as the billing page |
| 210 | * a refusal to start compute points to. |
| 211 | */ |
| 212 | export function Linked({ text }: { text: string }) { |
| 213 | return ( |
| 214 | <> |
| 215 | {linkPaths(text).map((piece, at) => |
| 216 | piece.href ? ( |
| 217 | <Link key={at} to={piece.href} className="underline underline-offset-2"> |
| 218 | {piece.text} |
| 219 | </Link> |
| 220 | ) : ( |
| 221 | piece.text |
| 222 | ), |
| 223 | )} |
| 224 | </> |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | export function ErrorText({ children }: { children: ReactNode }) { |
| 229 | if (!children) return null; |
| 230 | if (typeof children !== "string") return <p className="text-sm text-danger">{children}</p>; |
| 231 | // Waiting for a free agent slot is not an error: it starts by itself. |
| 232 | return ( |
| 233 | <p className={`text-sm ${isWaitingMessage(children) ? "text-muted" : "text-danger"}`}> |
| 234 | <Linked text={children} /> |
| 235 | </p> |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * What a workspace's plan says before someone tries to start compute it |
| 241 | * would refuse ("Agents need a paid workspace or the free trial"), with |
| 242 | * where to fix it. |
| 243 | */ |
| 244 | export function ComputeNote({ note }: { note: string | null | undefined }) { |
| 245 | return note ? ( |
| 246 | <p className="text-sm text-muted"> |
| 247 | <Linked text={note} /> |
| 248 | </p> |
| 249 | ) : null; |
| 250 | } |
| 251 | |
| 252 | /** A small outlined label. */ |
| 253 | export function Pill({ children }: { children: ReactNode }) { |
| 254 | return ( |
| 255 | <span className="inline-flex items-center rounded-full border border-line px-2 py-0.5 text-xs text-muted"> |
| 256 | {children} |
| 257 | </span> |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | const AVATAR_HUES = [82, 200, 262, 28, 330, 160]; |
| 262 | |
| 263 | /** |
| 264 | * g1t itself: its agent, as reviewer, assignee and commit author, and the |
| 265 | * system, as the author of security updates, the issues it opens and merges |
| 266 | * from the queue. One name, `g1t`. |
| 267 | */ |
| 268 | export function isSystemName(name: string | null | undefined): boolean { |
| 269 | return name === "g1t"; |
| 270 | } |
| 271 | |
| 272 | /** Where an uploaded avatar is served, from the hash it is stored by. */ |
| 273 | export function avatarUrl(avatar: string): string { |
| 274 | return `/avatars/${avatar}`; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * An uploaded avatar if there is one, else a letter avatar whose colour is |
| 279 | * stable for a given name. People are round; a workspace is `square`. An |
| 280 | * image that fails to load falls back to the letter. |
| 281 | */ |
| 282 | export function Avatar({ |
| 283 | name, |
| 284 | size = 20, |
| 285 | square, |
| 286 | image, |
| 287 | system, |
| 288 | }: { |
| 289 | name: string; |
| 290 | size?: number; |
| 291 | square?: boolean; |
| 292 | /** The uploaded avatar's hash, as identity returns it. */ |
| 293 | image?: string | null; |
| 294 | /** g1t itself (a user of kind `system`), whatever the name. */ |
| 295 | system?: boolean; |
| 296 | }) { |
| 297 | const [failed, setFailed] = useState<string | null>(null); |
| 298 | // g1t itself wears its own mark: the pixel 1 on a dark square. |
| 299 | if (system || isSystemName(name)) { |
| 300 | return ( |
| 301 | <span |
| 302 | aria-hidden="true" |
| 303 | className="inline-flex shrink-0 items-center justify-center bg-[#0b0b0d] text-fg ring-1 ring-line-strong ring-inset" |
| 304 | style={{ width: size, height: size, borderRadius: size * 0.24 }} |
| 305 | > |
| 306 | <Mark className="size-full" /> |
| 307 | </span> |
| 308 | ); |
| 309 | } |
| 310 | if (image && failed !== image) { |
| 311 | return ( |
| 312 | <img |
| 313 | src={avatarUrl(image)} |
| 314 | alt="" |
| 315 | aria-hidden="true" |
| 316 | width={size} |
| 317 | height={size} |
| 318 | loading="lazy" |
| 319 | decoding="async" |
| 320 | onError={() => setFailed(image)} |
| 321 | className="inline-block shrink-0 bg-raised object-cover" |
| 322 | style={{ width: size, height: size, borderRadius: square ? size * 0.24 : size }} |
| 323 | /> |
| 324 | ); |
| 325 | } |
| 326 | let hash = 0; |
| 327 | for (const char of name) hash = (hash * 31 + char.charCodeAt(0)) | 0; |
| 328 | const hue = AVATAR_HUES[Math.abs(hash) % AVATAR_HUES.length]; |
| 329 | return ( |
| 330 | <span |
| 331 | aria-hidden="true" |
| 332 | className="inline-flex shrink-0 items-center justify-center font-mono font-semibold uppercase" |
| 333 | style={{ |
| 334 | width: size, |
| 335 | height: size, |
| 336 | borderRadius: square ? size * 0.24 : size, |
| 337 | fontSize: size * 0.5, |
| 338 | background: `oklch(0.4 0.09 ${hue})`, |
| 339 | color: `oklch(0.93 0.08 ${hue})`, |
| 340 | }} |
| 341 | > |
| 342 | {name[0]} |
| 343 | </span> |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | /** A line of text (usually a command) with a copy button. */ |
| 348 | export function CopyLine({ |
| 349 | text, |
| 350 | prompt, |
| 351 | }: { |
| 352 | text: string; |
| 353 | prompt?: boolean; |
| 354 | }) { |
| 355 | const [copied, setCopied] = useState(false); |
| 356 | return ( |
| 357 | <div className="group flex items-center gap-3 rounded-lg border border-line bg-surface py-2 pr-2 pl-3.5 font-mono text-[0.8125rem]"> |
| 358 | {/* Wraps rather than hides: a command or an address is no use half seen. */} |
| 359 | <code className="min-w-0 grow whitespace-pre-wrap [overflow-wrap:anywhere]"> |
| 360 | {prompt && <span className="mr-2 text-faint select-none">$</span>} |
| 361 | {/* Short words with hyphens, such as `--transport`, stay whole when |
| 362 | the line wraps; long ones, such as addresses, may still break. */} |
| 363 | {text.split(/(\s+)/).map((part, index) => |
| 364 | part.includes("-") && part.length <= 24 ? ( |
| 365 | <span key={index} className="whitespace-nowrap"> |
| 366 | {part} |
| 367 | </span> |
| 368 | ) : ( |
| 369 | part |
| 370 | ), |
| 371 | )} |
| 372 | </code> |
| 373 | <button |
| 374 | type="button" |
| 375 | aria-label="Copy" |
| 376 | className="shrink-0 rounded-md p-1.5 text-faint transition-colors hover:bg-raised hover:text-fg" |
| 377 | onClick={() => { |
| 378 | void navigator.clipboard.writeText(text); |
| 379 | setCopied(true); |
| 380 | setTimeout(() => setCopied(false), 1500); |
| 381 | }} |
| 382 | > |
| 383 | {copied ? <Check size={14} className="text-accent" /> : <Copy size={14} />} |
| 384 | </button> |
| 385 | </div> |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | const UNITS: [string, number][] = [ |
| 390 | ["d", 86_400_000], |
| 391 | ["h", 3_600_000], |
| 392 | ["m", 60_000], |
| 393 | ]; |
| 394 | |
| 395 | /** Compact relative time such as "5m ago". */ |
| 396 | export function TimeAgo({ at }: { at: string | number }) { |
| 397 | // Timestamps are RFC 3339 strings; a few older ones are still numbers. |
| 398 | const elapsed = Date.now() - new Date(at).getTime(); |
| 399 | const unit = UNITS.find(([, size]) => elapsed >= size); |
| 400 | const label = unit ? `${Math.floor(elapsed / unit[1])}${unit[0]} ago` : "just now"; |
| 401 | return ( |
| 402 | // The server and the browser render at slightly different times. |
| 403 | <time dateTime={new Date(at).toISOString()} suppressHydrationWarning> |
| 404 | {label} |
| 405 | </time> |
| 406 | ); |
| 407 | } |
| 408 | |
| 409 | export function EmptyState({ |
| 410 | title, |
| 411 | children, |
| 412 | }: { |
| 413 | title: string; |
| 414 | children?: ReactNode; |
| 415 | }) { |
| 416 | return ( |
| 417 | <div className="rounded-lg border border-dashed border-line px-6 py-12 text-center"> |
| 418 | <p className="font-medium">{title}</p> |
| 419 | {children && <div className="mt-1.5 text-sm text-muted">{children}</div>} |
| 420 | </div> |
| 421 | ); |
| 422 | } |