Skip to content
146 linesCodeBlameRaw
1import { ChevronRight, KeyRound } from "lucide-react";
2import type { ReactNode } from "react";
3import { Link } from "react-router";
4
5import type { AccessToken } from "@g1t/contracts";
6
7import { cn } from "../lib/cn";
8import { permissionChips, reachSummary, statusBadge } from "../lib/access-tokens";
9import { describeExpiry } from "../lib/token-scopes";
10import { ButtonLink, CopyLine, TimeAgo } from "./ui";
11import { Badge } from "./ui/badge";
12
13// One access token in a line, and the list of them: the same on your
14// settings, a workspace's tokens and a workspace's view of its members'
15// tokens.
16
17/** What a token reaches and may do, under its name. */
18export function TokenFacts({ token, className }: { token: AccessToken; className?: string }) {
19 const chips = permissionChips(token);
20 return (
21 <div className={cn("space-y-1.5", className)}>
22 <p className="text-xs text-muted">{reachSummary(token)}</p>
23 <div className="flex flex-wrap gap-1.5">
24 {chips.length === 0 && <span className="text-xs text-faint">No permissions</span>}
25 {chips.map((chip) => (
26 <span
27 key={chip.label}
28 className={cn(
29 "rounded border px-1.5 py-px text-[0.6875rem]",
30 chip.dangerous ? "border-danger/40 text-danger" : "border-line text-muted",
31 )}
32 >
33 {chip.label}
34 </span>
35 ))}
36 </div>
37 {token.repositorySelection === "selected" && (token.repositories?.length ?? 0) > 0 && (
38 <p className="truncate font-mono text-[0.6875rem] text-faint">{token.repositories!.join(", ")}</p>
39 )}
40 {token.reviewReason && <p className="text-xs text-faint">Owner's note: {token.reviewReason}</p>}
41 </div>
42 );
43}
44
45/** When it was made, by whom, last used, and when it expires. */
46export function TokenMeta({ token }: { token: AccessToken }) {
47 const expiry = describeExpiry(token.expiresAt);
48 return (
49 <p className="text-xs text-faint">
50 Created <TimeAgo at={token.createdAt} />
51 {token.workspaceOwned &&
52 (token.createdBy ? (
53 <>
54 {" "}
55 by <span className="font-mono">{token.createdBy}</span>
56 </>
57 ) : (
58 " by someone who has since left g1t"
59 ))}{" "}
60 ·{" "}
61 {token.lastUsedAt ? (
62 <>
63 last used <TimeAgo at={token.lastUsedAt} />
64 </>
65 ) : (
66 "never used"
67 )}{" "}
68 · <span className={expiry === "Expired" ? "text-danger" : expiry === "No expiry" ? "text-warn" : undefined}>{expiry}</span>
69 </p>
70 );
71}
72
73/** Its badges: waiting, denied or revoked; Admin on a workspace's token. */
74export function TokenBadges({ token }: { token: AccessToken }) {
75 const badge = statusBadge(token.status);
76 return (
77 <>
78 {badge && <Badge tone={badge.tone}>{badge.label}</Badge>}
79 {token.workspaceOwned && <Badge tone={token.admin ? "danger" : "neutral"}>{token.admin ? "Admin" : "Write"}</Badge>}
80 {token.website && <Badge tone="warn">Uses the website</Badge>}
81 </>
82 );
83}
84
85/** A list of tokens, each a link to its page. */
86export function TokenList({
87 tokens,
88 href,
89 lead,
90 aside,
91}: {
92 tokens: AccessToken[];
93 href: (token: AccessToken) => string;
94 /** Before a token's name, such as whose it is. */
95 lead?: (token: AccessToken) => ReactNode;
96 /** After it, such as an action. */
97 aside?: (token: AccessToken) => ReactNode;
98}) {
99 return (
100 <ul className="divide-y divide-line rounded-xl border border-line">
101 {tokens.map((token) => (
102 <li key={token.id} className="flex flex-col gap-3 px-4 py-3 sm:flex-row sm:items-start">
103 <div className="min-w-0 grow space-y-1">
104 <p className="flex min-w-0 flex-wrap items-center gap-2 text-sm">
105 {lead?.(token)}
106 <Link to={href(token)} className="flex min-w-0 items-center gap-1.5 font-medium text-fg hover:underline">
107 <KeyRound size={13} className="shrink-0 text-faint" />
108 <span className="truncate">{token.name}</span>
109 </Link>
110 <TokenBadges token={token} />
111 </p>
112 {token.description && <p className="truncate text-xs text-muted">{token.description}</p>}
113 <TokenMeta token={token} />
114 <TokenFacts token={token} />
115 </div>
116 <div className="flex shrink-0 items-center gap-2">
117 {aside?.(token)}
118 <Link to={href(token)} aria-label={`Open ${token.name}`} className="hidden text-faint hover:text-fg sm:block">
119 <ChevronRight size={16} />
120 </Link>
121 </div>
122 </li>
123 ))}
124 </ul>
125 );
126}
127
128/** A token just made: the secret, once, and what it is. */
129export function TokenCreated({ secret, token, back }: { secret: string; token: AccessToken; back: string }) {
130 return (
131 <div className="space-y-4 rounded-xl border border-accent/40 bg-surface p-4 sm:p-5">
132 <p className="text-sm">
133 <span className="font-medium">{token.name}</span> is ready. Copy it now: it will not be shown again.
134 </p>
135 <CopyLine text={secret} />
136 <div className="flex flex-wrap items-center gap-2">
137 <TokenBadges token={token} />
138 </div>
139 <TokenMeta token={token} />
140 <TokenFacts token={token} />
141 <ButtonLink variant="quiet" to={back}>
142 Back to tokens
143 </ButtonLink>
144 </div>
145 );
146}