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