@nexys/user-management-web 0.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/README.md +61 -0
  2. package/components.json +21 -0
  3. package/package.json +66 -0
  4. package/src/components/audit/audit-log-table.test.tsx +91 -0
  5. package/src/components/audit/audit-log-table.tsx +226 -0
  6. package/src/components/auth/accept-invitation-card.tsx +100 -0
  7. package/src/components/auth/forgot-password-form.tsx +92 -0
  8. package/src/components/auth/reset-password-form.tsx +143 -0
  9. package/src/components/auth/sign-in-form.test.tsx +140 -0
  10. package/src/components/auth/sign-in-form.tsx +509 -0
  11. package/src/components/auth/sign-up-form.tsx +123 -0
  12. package/src/components/layout/app-shell.tsx +189 -0
  13. package/src/components/layout/auth-shell.tsx +28 -0
  14. package/src/components/layout/theme-toggle.tsx +55 -0
  15. package/src/components/layout/user-menu.tsx +84 -0
  16. package/src/components/primitives/empty-state.tsx +40 -0
  17. package/src/components/primitives/page-header.tsx +27 -0
  18. package/src/components/primitives/spinner.tsx +28 -0
  19. package/src/components/profile/api-tokens-card.tsx +235 -0
  20. package/src/components/profile/change-password-form.tsx +158 -0
  21. package/src/components/profile/passkeys-card.tsx +180 -0
  22. package/src/components/profile/profile-card.tsx +33 -0
  23. package/src/components/profile/two-factor-card.tsx +226 -0
  24. package/src/components/projects/project-consents.tsx +572 -0
  25. package/src/components/projects/project-create-dialog.tsx +209 -0
  26. package/src/components/projects/project-detail-tabs.tsx +63 -0
  27. package/src/components/projects/project-email-templates.tsx +282 -0
  28. package/src/components/projects/project-notices.tsx +458 -0
  29. package/src/components/projects/project-settings.tsx +1133 -0
  30. package/src/components/projects/project-tenants.tsx +107 -0
  31. package/src/components/projects/projects-table.test.tsx +95 -0
  32. package/src/components/projects/projects-table.tsx +240 -0
  33. package/src/components/roles/role-badge.tsx +91 -0
  34. package/src/components/roles/role-select.tsx +76 -0
  35. package/src/components/tenants/tenant-create-dialog.tsx +163 -0
  36. package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
  37. package/src/components/tenants/tenant-members.tsx +578 -0
  38. package/src/components/tenants/tenant-roles.test.tsx +91 -0
  39. package/src/components/tenants/tenant-roles.tsx +414 -0
  40. package/src/components/tenants/tenant-settings.tsx +385 -0
  41. package/src/components/tenants/tenants-table.test.tsx +71 -0
  42. package/src/components/tenants/tenants-table.tsx +245 -0
  43. package/src/components/ui/alert.tsx +52 -0
  44. package/src/components/ui/avatar.tsx +46 -0
  45. package/src/components/ui/badge.tsx +32 -0
  46. package/src/components/ui/button.tsx +64 -0
  47. package/src/components/ui/card.tsx +65 -0
  48. package/src/components/ui/dialog.tsx +99 -0
  49. package/src/components/ui/dropdown-menu.tsx +97 -0
  50. package/src/components/ui/field.tsx +44 -0
  51. package/src/components/ui/input.tsx +23 -0
  52. package/src/components/ui/label.tsx +19 -0
  53. package/src/components/ui/select.tsx +87 -0
  54. package/src/components/ui/separator.tsx +22 -0
  55. package/src/components/ui/skeleton.tsx +13 -0
  56. package/src/components/ui/switch.tsx +29 -0
  57. package/src/components/ui/table.tsx +87 -0
  58. package/src/components/ui/tabs.tsx +51 -0
  59. package/src/components/ui/textarea.tsx +22 -0
  60. package/src/components/ui/tooltip.tsx +27 -0
  61. package/src/components/users/user-create-dialog.tsx +267 -0
  62. package/src/components/users/user-delete-dialog.tsx +88 -0
  63. package/src/components/users/user-detail.tsx +843 -0
  64. package/src/components/users/users-table.test.tsx +97 -0
  65. package/src/components/users/users-table.tsx +382 -0
  66. package/src/context/auth-context.tsx +124 -0
  67. package/src/context/theme-context.test.tsx +78 -0
  68. package/src/context/theme-context.tsx +110 -0
  69. package/src/hooks/use-call.ts +64 -0
  70. package/src/index.ts +159 -0
  71. package/src/lib/error-message.ts +16 -0
  72. package/src/lib/utils.test.ts +61 -0
  73. package/src/lib/utils.ts +79 -0
  74. package/src/lib/webauthn.ts +36 -0
  75. package/src/styles.css +107 -0
  76. package/src/test-setup.ts +5 -0
  77. package/tailwind.preset.cjs +15 -0
@@ -0,0 +1,189 @@
1
+ import { Menu, X } from "lucide-react";
2
+ import type { LucideIcon } from "lucide-react";
3
+ import { useState, type ReactNode } from "react";
4
+
5
+ import { cn } from "../../lib/utils.js";
6
+
7
+ export interface NavItem {
8
+ key: string;
9
+ label: ReactNode;
10
+ icon?: LucideIcon;
11
+ href?: string;
12
+ active?: boolean;
13
+ badge?: ReactNode;
14
+ onClick?: () => void;
15
+ }
16
+
17
+ export interface NavSection {
18
+ title?: ReactNode;
19
+ items: NavItem[];
20
+ }
21
+
22
+ export interface AppShellProps {
23
+ brand?: ReactNode;
24
+ sections: NavSection[];
25
+ /** Render at the bottom of the sidebar — typically <UserMenu />. */
26
+ sidebarFooter?: ReactNode;
27
+ /** Optional topbar on the right (search, notifications, etc). */
28
+ topbar?: ReactNode;
29
+ children: ReactNode;
30
+ className?: string;
31
+ }
32
+
33
+ const NavLink = ({
34
+ item,
35
+ onNavigate,
36
+ }: {
37
+ item: NavItem;
38
+ /** Fires after the item's own handler — closes the mobile drawer. */
39
+ onNavigate?: () => void;
40
+ }) => {
41
+ const Icon = item.icon;
42
+ const inner = (
43
+ <>
44
+ {Icon ? <Icon className="size-4 shrink-0" aria-hidden /> : null}
45
+ <span className="flex-1 truncate">{item.label}</span>
46
+ {item.badge ? <span className="ml-auto">{item.badge}</span> : null}
47
+ </>
48
+ );
49
+ const className = cn(
50
+ "group flex items-center gap-2.5 rounded-md px-2.5 py-2 text-sm font-medium transition-colors",
51
+ "text-muted-foreground hover:bg-accent hover:text-foreground",
52
+ item.active && "bg-accent text-foreground"
53
+ );
54
+ if (item.href) {
55
+ return (
56
+ <a
57
+ href={item.href}
58
+ className={className}
59
+ aria-current={item.active ? "page" : undefined}
60
+ onClick={onNavigate}
61
+ >
62
+ {inner}
63
+ </a>
64
+ );
65
+ }
66
+ return (
67
+ <button
68
+ type="button"
69
+ onClick={() => {
70
+ item.onClick?.();
71
+ onNavigate?.();
72
+ }}
73
+ className={cn(className, "w-full text-left")}
74
+ aria-current={item.active ? "page" : undefined}
75
+ >
76
+ {inner}
77
+ </button>
78
+ );
79
+ };
80
+
81
+ const SidebarNav = ({
82
+ sections,
83
+ onNavigate,
84
+ }: {
85
+ sections: NavSection[];
86
+ onNavigate?: () => void;
87
+ }) => (
88
+ <nav className="flex-1 overflow-y-auto px-2 py-4">
89
+ {sections.map((section, i) => (
90
+ <div key={i} className={cn(i > 0 && "mt-6")}>
91
+ {section.title ? (
92
+ <p className="px-2.5 pb-1.5 text-xs font-medium uppercase tracking-wide text-muted-foreground">
93
+ {section.title}
94
+ </p>
95
+ ) : null}
96
+ <ul className="grid gap-0.5">
97
+ {section.items.map((item) => (
98
+ <li key={item.key}>
99
+ <NavLink item={item} onNavigate={onNavigate} />
100
+ </li>
101
+ ))}
102
+ </ul>
103
+ </div>
104
+ ))}
105
+ </nav>
106
+ );
107
+
108
+ export const AppShell = ({
109
+ brand,
110
+ sections,
111
+ sidebarFooter,
112
+ topbar,
113
+ children,
114
+ className,
115
+ }: AppShellProps) => {
116
+ // Mobile slide-over drawer — the sidebar is hidden below `md`.
117
+ const [drawerOpen, setDrawerOpen] = useState(false);
118
+ const closeDrawer = () => setDrawerOpen(false);
119
+
120
+ return (
121
+ <div className={cn("min-h-screen bg-background", className)}>
122
+ <div className="mx-auto grid min-h-screen w-full max-w-[1440px] grid-cols-1 md:grid-cols-[260px_1fr]">
123
+ <aside className="sticky top-0 hidden h-screen flex-col border-r bg-card/50 backdrop-blur md:flex">
124
+ {brand ? (
125
+ <div className="flex h-14 items-center border-b px-4 text-sm font-semibold tracking-tight">
126
+ {brand}
127
+ </div>
128
+ ) : null}
129
+ <SidebarNav sections={sections} />
130
+ {sidebarFooter ? (
131
+ <div className="border-t p-2">{sidebarFooter}</div>
132
+ ) : null}
133
+ </aside>
134
+
135
+ {/* Mobile drawer + backdrop. Rendered only while open. */}
136
+ {drawerOpen ? (
137
+ <div className="fixed inset-0 z-50 md:hidden" role="dialog" aria-modal="true">
138
+ <div
139
+ className="absolute inset-0 bg-black/40 backdrop-blur-sm"
140
+ onClick={closeDrawer}
141
+ aria-hidden
142
+ />
143
+ <div className="absolute inset-y-0 left-0 flex w-72 max-w-[85vw] flex-col border-r bg-card shadow-xl">
144
+ <div className="flex h-14 items-center justify-between border-b px-4 text-sm font-semibold tracking-tight">
145
+ <span className="truncate">{brand}</span>
146
+ <button
147
+ type="button"
148
+ onClick={closeDrawer}
149
+ aria-label="Close navigation"
150
+ className="rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground"
151
+ >
152
+ <X className="size-5" />
153
+ </button>
154
+ </div>
155
+ <SidebarNav sections={sections} onNavigate={closeDrawer} />
156
+ {sidebarFooter ? (
157
+ <div className="border-t p-2">{sidebarFooter}</div>
158
+ ) : null}
159
+ </div>
160
+ </div>
161
+ ) : null}
162
+
163
+ <main className="flex min-h-screen min-w-0 flex-col">
164
+ {/* Mobile header: hamburger + brand. Hidden from `md` up. */}
165
+ <header className="sticky top-0 z-40 flex h-14 items-center gap-3 border-b bg-background/80 px-4 backdrop-blur md:hidden">
166
+ <button
167
+ type="button"
168
+ onClick={() => setDrawerOpen(true)}
169
+ aria-label="Open navigation"
170
+ className="rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground"
171
+ >
172
+ <Menu className="size-5" />
173
+ </button>
174
+ <div className="min-w-0 flex-1 truncate text-sm font-semibold tracking-tight">
175
+ {brand}
176
+ </div>
177
+ {topbar}
178
+ </header>
179
+ {topbar ? (
180
+ <header className="sticky top-0 z-30 hidden h-14 items-center justify-end gap-3 border-b bg-background/80 px-6 backdrop-blur md:flex">
181
+ {topbar}
182
+ </header>
183
+ ) : null}
184
+ <div className="min-w-0 flex-1 px-4 py-6 sm:px-6">{children}</div>
185
+ </main>
186
+ </div>
187
+ </div>
188
+ );
189
+ };
@@ -0,0 +1,28 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ import { cn } from "../../lib/utils.js";
4
+
5
+ export interface AuthShellProps {
6
+ brand?: ReactNode;
7
+ footer?: ReactNode;
8
+ children: ReactNode;
9
+ className?: string;
10
+ }
11
+
12
+ /**
13
+ * Centered, slick auth shell. Drops a subtle dotted background so a
14
+ * raw <SignInForm /> still looks like a polished page.
15
+ */
16
+ export const AuthShell = ({ brand, footer, children, className }: AuthShellProps) => (
17
+ <div
18
+ className={cn(
19
+ "relative flex min-h-screen flex-col items-center justify-center gap-6 bg-background px-4 py-12",
20
+ "before:absolute before:inset-0 before:-z-10 before:bg-[radial-gradient(circle_at_1px_1px,hsl(var(--border))_1px,transparent_0)] before:bg-[size:24px_24px] before:opacity-50",
21
+ className
22
+ )}
23
+ >
24
+ {brand ? <div className="text-sm font-semibold tracking-tight">{brand}</div> : null}
25
+ {children}
26
+ {footer ? <div className="text-xs text-muted-foreground">{footer}</div> : null}
27
+ </div>
28
+ );
@@ -0,0 +1,55 @@
1
+ import { Check, Monitor, Moon, Sun } from "lucide-react";
2
+
3
+ import { useTheme, type Theme } from "../../context/theme-context.js";
4
+ import { cn } from "../../lib/utils.js";
5
+ import { Button } from "../ui/button.js";
6
+ import {
7
+ DropdownMenu,
8
+ DropdownMenuContent,
9
+ DropdownMenuItem,
10
+ DropdownMenuTrigger,
11
+ } from "../ui/dropdown-menu.js";
12
+
13
+ const OPTIONS: { value: Theme; label: string; icon: typeof Sun }[] = [
14
+ { value: "light", label: "Light", icon: Sun },
15
+ { value: "dark", label: "Dark", icon: Moon },
16
+ { value: "system", label: "System", icon: Monitor },
17
+ ];
18
+
19
+ export interface ThemeToggleProps {
20
+ className?: string;
21
+ }
22
+
23
+ /** Icon button + menu to pick light / dark / system. Needs <ThemeProvider>. */
24
+ export const ThemeToggle = ({ className }: ThemeToggleProps) => {
25
+ const { theme, resolvedTheme, setTheme } = useTheme();
26
+
27
+ return (
28
+ <DropdownMenu>
29
+ <DropdownMenuTrigger asChild>
30
+ <Button
31
+ variant="ghost"
32
+ size="icon"
33
+ aria-label="Change theme"
34
+ className={cn("text-muted-foreground", className)}
35
+ >
36
+ {resolvedTheme === "dark" ? <Moon /> : <Sun />}
37
+ </Button>
38
+ </DropdownMenuTrigger>
39
+ <DropdownMenuContent align="end" className="w-36">
40
+ {OPTIONS.map(({ value, label, icon: Icon }) => (
41
+ <DropdownMenuItem
42
+ key={value}
43
+ onSelect={() => setTheme(value)}
44
+ aria-checked={theme === value}
45
+ role="menuitemradio"
46
+ >
47
+ <Icon />
48
+ {label}
49
+ {theme === value ? <Check className="ml-auto" /> : null}
50
+ </DropdownMenuItem>
51
+ ))}
52
+ </DropdownMenuContent>
53
+ </DropdownMenu>
54
+ );
55
+ };
@@ -0,0 +1,84 @@
1
+ import { ChevronsUpDown, LogOut, type LucideIcon } from "lucide-react";
2
+ import type { ReactNode } from "react";
3
+
4
+ import { useAuth } from "../../context/auth-context.js";
5
+ import { initialsOf } from "../../lib/utils.js";
6
+ import { UserRoleBadge } from "../roles/role-badge.js";
7
+ import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar.js";
8
+ import {
9
+ DropdownMenu,
10
+ DropdownMenuContent,
11
+ DropdownMenuItem,
12
+ DropdownMenuLabel,
13
+ DropdownMenuSeparator,
14
+ DropdownMenuTrigger,
15
+ } from "../ui/dropdown-menu.js";
16
+
17
+ export interface UserMenuItem {
18
+ label: ReactNode;
19
+ icon?: LucideIcon;
20
+ onSelect: () => void;
21
+ }
22
+
23
+ export interface UserMenuProps {
24
+ image?: string | null;
25
+ items?: UserMenuItem[];
26
+ }
27
+
28
+ export const UserMenu = ({ image, items = [] }: UserMenuProps) => {
29
+ const { data, signOut } = useAuth();
30
+ if (!data) return null;
31
+ const { user } = data;
32
+
33
+ return (
34
+ <DropdownMenu>
35
+ <DropdownMenuTrigger className="group flex w-full items-center gap-2.5 rounded-md p-2 text-left text-sm transition-colors hover:bg-accent focus:outline-none focus:ring-2 focus:ring-ring/40">
36
+ <Avatar className="size-8">
37
+ {image ? <AvatarImage src={image} alt={user.name ?? user.email} /> : null}
38
+ <AvatarFallback>{initialsOf(user.name, user.email)}</AvatarFallback>
39
+ </Avatar>
40
+ <div className="flex flex-1 flex-col leading-tight">
41
+ <span className="line-clamp-1 font-medium">{user.name ?? user.email}</span>
42
+ <span className="line-clamp-1 text-xs text-muted-foreground">
43
+ {user.email}
44
+ </span>
45
+ </div>
46
+ <ChevronsUpDown className="size-4 text-muted-foreground transition-transform group-data-[state=open]:rotate-180" />
47
+ </DropdownMenuTrigger>
48
+ <DropdownMenuContent align="end" className="w-60">
49
+ <DropdownMenuLabel className="flex flex-col gap-1 text-foreground">
50
+ <span className="text-sm font-medium">{user.name ?? user.email}</span>
51
+ <span className="text-xs font-normal text-muted-foreground">
52
+ {user.email}
53
+ </span>
54
+ <UserRoleBadge role={user.role} className="mt-1 w-fit" />
55
+ </DropdownMenuLabel>
56
+ {items.length > 0 && <DropdownMenuSeparator />}
57
+ {items.map((item, i) => {
58
+ const Icon = item.icon;
59
+ return (
60
+ <DropdownMenuItem
61
+ key={i}
62
+ onSelect={(e) => {
63
+ e.preventDefault();
64
+ item.onSelect();
65
+ }}
66
+ >
67
+ {Icon ? <Icon /> : null}
68
+ {item.label}
69
+ </DropdownMenuItem>
70
+ );
71
+ })}
72
+ <DropdownMenuSeparator />
73
+ <DropdownMenuItem
74
+ onSelect={(e) => {
75
+ e.preventDefault();
76
+ void signOut();
77
+ }}
78
+ >
79
+ <LogOut /> Sign out
80
+ </DropdownMenuItem>
81
+ </DropdownMenuContent>
82
+ </DropdownMenu>
83
+ );
84
+ };
@@ -0,0 +1,40 @@
1
+ import type { LucideIcon } from "lucide-react";
2
+ import type { ReactNode } from "react";
3
+
4
+ import { cn } from "../../lib/utils.js";
5
+
6
+ export interface EmptyStateProps {
7
+ icon?: LucideIcon;
8
+ title: ReactNode;
9
+ description?: ReactNode;
10
+ action?: ReactNode;
11
+ className?: string;
12
+ }
13
+
14
+ export const EmptyState = ({
15
+ icon: Icon,
16
+ title,
17
+ description,
18
+ action,
19
+ className,
20
+ }: EmptyStateProps) => (
21
+ <div
22
+ className={cn(
23
+ "flex flex-col items-center justify-center gap-3 rounded-xl border border-dashed bg-muted/30 px-6 py-12 text-center",
24
+ className
25
+ )}
26
+ >
27
+ {Icon ? (
28
+ <div className="flex size-11 items-center justify-center rounded-full bg-background shadow-sm">
29
+ <Icon className="size-5 text-muted-foreground" aria-hidden />
30
+ </div>
31
+ ) : null}
32
+ <div className="space-y-1">
33
+ <p className="text-base font-medium">{title}</p>
34
+ {description ? (
35
+ <p className="text-sm text-muted-foreground">{description}</p>
36
+ ) : null}
37
+ </div>
38
+ {action ? <div className="mt-2">{action}</div> : null}
39
+ </div>
40
+ );
@@ -0,0 +1,27 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ import { cn } from "../../lib/utils.js";
4
+
5
+ export interface PageHeaderProps {
6
+ title: ReactNode;
7
+ description?: ReactNode;
8
+ actions?: ReactNode;
9
+ className?: string;
10
+ }
11
+
12
+ export const PageHeader = ({ title, description, actions, className }: PageHeaderProps) => (
13
+ <div
14
+ className={cn(
15
+ "flex flex-col gap-2 border-b pb-5 sm:flex-row sm:items-end sm:justify-between",
16
+ className
17
+ )}
18
+ >
19
+ <div className="space-y-1">
20
+ <h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
21
+ {description ? (
22
+ <p className="text-sm text-muted-foreground">{description}</p>
23
+ ) : null}
24
+ </div>
25
+ {actions ? <div className="flex items-center gap-2">{actions}</div> : null}
26
+ </div>
27
+ );
@@ -0,0 +1,28 @@
1
+ import { Loader2 } from "lucide-react";
2
+
3
+ import { cn } from "../../lib/utils.js";
4
+
5
+ export const Spinner = ({
6
+ className,
7
+ size = "md",
8
+ }: {
9
+ className?: string;
10
+ size?: "sm" | "md" | "lg";
11
+ }) => (
12
+ <Loader2
13
+ aria-hidden
14
+ className={cn(
15
+ "animate-spin text-muted-foreground",
16
+ size === "sm" && "size-4",
17
+ size === "md" && "size-5",
18
+ size === "lg" && "size-8",
19
+ className
20
+ )}
21
+ />
22
+ );
23
+
24
+ export const FullPageSpinner = () => (
25
+ <div className="flex min-h-[40vh] items-center justify-center">
26
+ <Spinner size="lg" />
27
+ </div>
28
+ );
@@ -0,0 +1,235 @@
1
+ import {
2
+ type ApiTokenCreated,
3
+ type ApiTokenRow,
4
+ } from "@nexys/user-management-client";
5
+ import { AlertCircle, Check, Copy, KeySquare, Plus, ShieldOff } from "lucide-react";
6
+ import { useState, type FormEvent } from "react";
7
+
8
+ import { useUmClient } from "../../context/auth-context.js";
9
+ import { useCall } from "../../hooks/use-call.js";
10
+ import { errorMessage } from "../../lib/error-message.js";
11
+ import { formatRelative } from "../../lib/utils.js";
12
+ import { EmptyState } from "../primitives/empty-state.js";
13
+ import { Alert, AlertDescription } from "../ui/alert.js";
14
+ import { Badge } from "../ui/badge.js";
15
+ import { Button } from "../ui/button.js";
16
+ import {
17
+ Card,
18
+ CardContent,
19
+ CardDescription,
20
+ CardHeader,
21
+ CardTitle,
22
+ } from "../ui/card.js";
23
+ import { Input } from "../ui/input.js";
24
+ import {
25
+ Table,
26
+ TableBody,
27
+ TableCell,
28
+ TableHead,
29
+ TableHeader,
30
+ TableRow,
31
+ } from "../ui/table.js";
32
+
33
+ const StatusBadge = ({ status }: { status: ApiTokenRow["status"] }) => (
34
+ <Badge variant={status === "active" ? "secondary" : "outline"}>
35
+ {status}
36
+ </Badge>
37
+ );
38
+
39
+ /**
40
+ * Personal API tokens — long-lived `um_…` credentials for CLIs and
41
+ * scripts. The plaintext is shown exactly once after creation.
42
+ */
43
+ export const ApiTokensCard = () => {
44
+ const client = useUmClient();
45
+ const { data, loading, error, refresh } = useCall("listApiTokens");
46
+ const [name, setName] = useState("");
47
+ const [expiresInDays, setExpiresInDays] = useState("");
48
+ const [creating, setCreating] = useState(false);
49
+ const [created, setCreated] = useState<ApiTokenCreated | null>(null);
50
+ const [copied, setCopied] = useState(false);
51
+ const [busy, setBusy] = useState<string | null>(null);
52
+ const [actionError, setActionError] = useState<string | null>(null);
53
+
54
+ const tokens = data?.tokens ?? [];
55
+
56
+ const create = async (e: FormEvent) => {
57
+ e.preventDefault();
58
+ setCreating(true);
59
+ setActionError(null);
60
+ const days = expiresInDays.trim() ? Number(expiresInDays.trim()) : undefined;
61
+ const res = await client.createApiToken({
62
+ name: name.trim(),
63
+ expiresInDays: days,
64
+ });
65
+ setCreating(false);
66
+ if (res.ok) {
67
+ setCreated(res.data);
68
+ setName("");
69
+ setExpiresInDays("");
70
+ await refresh();
71
+ } else {
72
+ setActionError(errorMessage(res.error, "Couldn't create the token."));
73
+ }
74
+ };
75
+
76
+ const revoke = async (t: ApiTokenRow) => {
77
+ setBusy(t.id);
78
+ setActionError(null);
79
+ const res = await client.revokeApiToken({ id: t.id });
80
+ setBusy(null);
81
+ if (res.ok) {
82
+ await refresh();
83
+ } else {
84
+ setActionError(errorMessage(res.error, "Couldn't revoke the token."));
85
+ }
86
+ };
87
+
88
+ const copyToken = async () => {
89
+ if (!created) return;
90
+ await navigator.clipboard.writeText(created.token);
91
+ setCopied(true);
92
+ setTimeout(() => setCopied(false), 2000);
93
+ };
94
+
95
+ return (
96
+ <Card>
97
+ <CardHeader>
98
+ <CardTitle className="flex items-center gap-2">
99
+ <KeySquare className="size-4 text-muted-foreground" />
100
+ API tokens
101
+ </CardTitle>
102
+ <CardDescription>
103
+ Long-lived credentials for CLIs and scripts. Use them as{" "}
104
+ <code className="font-mono text-xs">Authorization: Bearer um_…</code>.
105
+ Revoked tokens stay listed until the retention policy removes them.
106
+ </CardDescription>
107
+ </CardHeader>
108
+ <CardContent className="grid gap-4">
109
+ {error ? (
110
+ <Alert variant="destructive">
111
+ <AlertCircle />
112
+ <AlertDescription>{errorMessage(error)}</AlertDescription>
113
+ </Alert>
114
+ ) : null}
115
+ {actionError ? (
116
+ <Alert variant="destructive">
117
+ <AlertCircle />
118
+ <AlertDescription>{actionError}</AlertDescription>
119
+ </Alert>
120
+ ) : null}
121
+
122
+ {created ? (
123
+ <div className="grid gap-2 rounded-lg border border-dashed bg-muted/30 p-4">
124
+ <p className="text-sm font-medium">
125
+ Copy your new token now — it won't be shown again.
126
+ </p>
127
+ <div className="flex items-center gap-2">
128
+ <code
129
+ data-testid="api-token-plaintext"
130
+ className="flex-1 break-all rounded bg-muted px-2 py-1 font-mono text-xs"
131
+ >
132
+ {created.token}
133
+ </code>
134
+ <Button variant="outline" size="sm" onClick={copyToken}>
135
+ {copied ? <Check className="text-green-600" /> : <Copy />}
136
+ {copied ? "Copied" : "Copy"}
137
+ </Button>
138
+ <Button size="sm" onClick={() => setCreated(null)}>
139
+ Done
140
+ </Button>
141
+ </div>
142
+ </div>
143
+ ) : (
144
+ <form
145
+ onSubmit={create}
146
+ className="grid items-end gap-3 sm:grid-cols-[1fr_auto_auto]"
147
+ >
148
+ <div className="grid gap-1.5">
149
+ <label htmlFor="token-name" className="text-sm font-medium leading-none">
150
+ Token name
151
+ </label>
152
+ <Input
153
+ id="token-name"
154
+ required
155
+ value={name}
156
+ onChange={(e) => setName(e.target.value)}
157
+ placeholder="e.g. CLI on work laptop"
158
+ />
159
+ </div>
160
+ <div className="grid gap-1.5">
161
+ <label htmlFor="token-expiry" className="text-sm font-medium leading-none">
162
+ Expires (days, optional)
163
+ </label>
164
+ <Input
165
+ id="token-expiry"
166
+ type="number"
167
+ min={1}
168
+ max={3650}
169
+ value={expiresInDays}
170
+ onChange={(e) => setExpiresInDays(e.target.value)}
171
+ placeholder="90"
172
+ className="w-36"
173
+ />
174
+ </div>
175
+ <Button type="submit" loading={creating} disabled={!name.trim()}>
176
+ <Plus />
177
+ Create token
178
+ </Button>
179
+ </form>
180
+ )}
181
+
182
+ {loading ? null : tokens.length === 0 ? (
183
+ <EmptyState
184
+ icon={KeySquare}
185
+ title="No API tokens"
186
+ description="Create one to authenticate scripts and CLI tools."
187
+ />
188
+ ) : (
189
+ <div className="overflow-hidden rounded-lg border">
190
+ <Table>
191
+ <TableHeader>
192
+ <TableRow>
193
+ <TableHead>Name</TableHead>
194
+ <TableHead>Token</TableHead>
195
+ <TableHead>Status</TableHead>
196
+ <TableHead>Last used</TableHead>
197
+ <TableHead className="text-right">Actions</TableHead>
198
+ </TableRow>
199
+ </TableHeader>
200
+ <TableBody>
201
+ {tokens.map((t) => (
202
+ <TableRow key={t.id}>
203
+ <TableCell className="font-medium">{t.name ?? "—"}</TableCell>
204
+ <TableCell className="font-mono text-xs text-muted-foreground">
205
+ {t.start ? `${t.start}…` : "—"}
206
+ </TableCell>
207
+ <TableCell>
208
+ <StatusBadge status={t.status} />
209
+ </TableCell>
210
+ <TableCell className="text-sm text-muted-foreground">
211
+ {t.lastUsedAt ? formatRelative(t.lastUsedAt) : "Never"}
212
+ </TableCell>
213
+ <TableCell className="text-right">
214
+ {t.status === "active" ? (
215
+ <Button
216
+ variant="ghost"
217
+ size="icon"
218
+ onClick={() => revoke(t)}
219
+ loading={busy === t.id}
220
+ aria-label={`Revoke token ${t.name ?? t.id}`}
221
+ >
222
+ <ShieldOff className="text-destructive" />
223
+ </Button>
224
+ ) : null}
225
+ </TableCell>
226
+ </TableRow>
227
+ ))}
228
+ </TableBody>
229
+ </Table>
230
+ </div>
231
+ )}
232
+ </CardContent>
233
+ </Card>
234
+ );
235
+ };