@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.
- package/README.md +61 -0
- package/components.json +21 -0
- package/package.json +66 -0
- package/src/components/audit/audit-log-table.test.tsx +91 -0
- package/src/components/audit/audit-log-table.tsx +226 -0
- package/src/components/auth/accept-invitation-card.tsx +100 -0
- package/src/components/auth/forgot-password-form.tsx +92 -0
- package/src/components/auth/reset-password-form.tsx +143 -0
- package/src/components/auth/sign-in-form.test.tsx +140 -0
- package/src/components/auth/sign-in-form.tsx +509 -0
- package/src/components/auth/sign-up-form.tsx +123 -0
- package/src/components/layout/app-shell.tsx +189 -0
- package/src/components/layout/auth-shell.tsx +28 -0
- package/src/components/layout/theme-toggle.tsx +55 -0
- package/src/components/layout/user-menu.tsx +84 -0
- package/src/components/primitives/empty-state.tsx +40 -0
- package/src/components/primitives/page-header.tsx +27 -0
- package/src/components/primitives/spinner.tsx +28 -0
- package/src/components/profile/api-tokens-card.tsx +235 -0
- package/src/components/profile/change-password-form.tsx +158 -0
- package/src/components/profile/passkeys-card.tsx +180 -0
- package/src/components/profile/profile-card.tsx +33 -0
- package/src/components/profile/two-factor-card.tsx +226 -0
- package/src/components/projects/project-consents.tsx +572 -0
- package/src/components/projects/project-create-dialog.tsx +209 -0
- package/src/components/projects/project-detail-tabs.tsx +63 -0
- package/src/components/projects/project-email-templates.tsx +282 -0
- package/src/components/projects/project-notices.tsx +458 -0
- package/src/components/projects/project-settings.tsx +1133 -0
- package/src/components/projects/project-tenants.tsx +107 -0
- package/src/components/projects/projects-table.test.tsx +95 -0
- package/src/components/projects/projects-table.tsx +240 -0
- package/src/components/roles/role-badge.tsx +91 -0
- package/src/components/roles/role-select.tsx +76 -0
- package/src/components/tenants/tenant-create-dialog.tsx +163 -0
- package/src/components/tenants/tenant-detail-tabs.tsx +51 -0
- package/src/components/tenants/tenant-members.tsx +578 -0
- package/src/components/tenants/tenant-roles.test.tsx +91 -0
- package/src/components/tenants/tenant-roles.tsx +414 -0
- package/src/components/tenants/tenant-settings.tsx +385 -0
- package/src/components/tenants/tenants-table.test.tsx +71 -0
- package/src/components/tenants/tenants-table.tsx +245 -0
- package/src/components/ui/alert.tsx +52 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +32 -0
- package/src/components/ui/button.tsx +64 -0
- package/src/components/ui/card.tsx +65 -0
- package/src/components/ui/dialog.tsx +99 -0
- package/src/components/ui/dropdown-menu.tsx +97 -0
- package/src/components/ui/field.tsx +44 -0
- package/src/components/ui/input.tsx +23 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/select.tsx +87 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +87 -0
- package/src/components/ui/tabs.tsx +51 -0
- package/src/components/ui/textarea.tsx +22 -0
- package/src/components/ui/tooltip.tsx +27 -0
- package/src/components/users/user-create-dialog.tsx +267 -0
- package/src/components/users/user-delete-dialog.tsx +88 -0
- package/src/components/users/user-detail.tsx +843 -0
- package/src/components/users/users-table.test.tsx +97 -0
- package/src/components/users/users-table.tsx +382 -0
- package/src/context/auth-context.tsx +124 -0
- package/src/context/theme-context.test.tsx +78 -0
- package/src/context/theme-context.tsx +110 -0
- package/src/hooks/use-call.ts +64 -0
- package/src/index.ts +159 -0
- package/src/lib/error-message.ts +16 -0
- package/src/lib/utils.test.ts +61 -0
- package/src/lib/utils.ts +79 -0
- package/src/lib/webauthn.ts +36 -0
- package/src/styles.css +107 -0
- package/src/test-setup.ts +5 -0
- package/tailwind.preset.cjs +15 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { AlertCircle, Check, Copy, Save, Trash2 } from "lucide-react";
|
|
2
|
+
import { useEffect, useState, type FormEvent } from "react";
|
|
3
|
+
|
|
4
|
+
import { useUmClient } from "../../context/auth-context.js";
|
|
5
|
+
import { useCall } from "../../hooks/use-call.js";
|
|
6
|
+
import { errorMessage } from "../../lib/error-message.js";
|
|
7
|
+
import { Alert, AlertDescription } from "../ui/alert.js";
|
|
8
|
+
import { Button } from "../ui/button.js";
|
|
9
|
+
import {
|
|
10
|
+
Card,
|
|
11
|
+
CardContent,
|
|
12
|
+
CardDescription,
|
|
13
|
+
CardHeader,
|
|
14
|
+
CardTitle,
|
|
15
|
+
} from "../ui/card.js";
|
|
16
|
+
import {
|
|
17
|
+
Dialog,
|
|
18
|
+
DialogClose,
|
|
19
|
+
DialogContent,
|
|
20
|
+
DialogDescription,
|
|
21
|
+
DialogFooter,
|
|
22
|
+
DialogHeader,
|
|
23
|
+
DialogTitle,
|
|
24
|
+
DialogTrigger,
|
|
25
|
+
} from "../ui/dialog.js";
|
|
26
|
+
import { Field } from "../ui/field.js";
|
|
27
|
+
import { Input } from "../ui/input.js";
|
|
28
|
+
import { Label } from "../ui/label.js";
|
|
29
|
+
import { Skeleton } from "../ui/skeleton.js";
|
|
30
|
+
|
|
31
|
+
export interface TenantSettingsProps {
|
|
32
|
+
organizationId: string;
|
|
33
|
+
/** Allow editing name/slug (org owner/admin or superadmin). */
|
|
34
|
+
canManage?: boolean;
|
|
35
|
+
canDelete?: boolean;
|
|
36
|
+
onDeleted?: () => void;
|
|
37
|
+
/** Called with the new uuid after a successful id change. */
|
|
38
|
+
onIdChanged?: (newId: string) => void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const TenantSettings = ({
|
|
42
|
+
organizationId,
|
|
43
|
+
canManage = false,
|
|
44
|
+
canDelete = false,
|
|
45
|
+
onDeleted,
|
|
46
|
+
onIdChanged,
|
|
47
|
+
}: TenantSettingsProps) => {
|
|
48
|
+
const client = useUmClient();
|
|
49
|
+
const { data, loading, error, refresh } = useCall("getFullTenant", {
|
|
50
|
+
organizationId,
|
|
51
|
+
});
|
|
52
|
+
// Resolve projectId → product name (superadmin sees all projects;
|
|
53
|
+
// for others this may miss, in which case the raw id is shown).
|
|
54
|
+
const projects = useCall("listProjects");
|
|
55
|
+
const productName = data?.projectId
|
|
56
|
+
? (projects.data?.projects ?? []).find((p) => p.id === data.projectId)?.name
|
|
57
|
+
: undefined;
|
|
58
|
+
const [copied, setCopied] = useState(false);
|
|
59
|
+
const [deleting, setDeleting] = useState(false);
|
|
60
|
+
const [deleteError, setDeleteError] = useState<string | null>(null);
|
|
61
|
+
|
|
62
|
+
const copyId = async () => {
|
|
63
|
+
await navigator.clipboard.writeText(organizationId);
|
|
64
|
+
setCopied(true);
|
|
65
|
+
setTimeout(() => setCopied(false), 2000);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const handleDelete = async () => {
|
|
69
|
+
setDeleting(true);
|
|
70
|
+
setDeleteError(null);
|
|
71
|
+
const res = await client.adminDeleteTenant({ id: organizationId });
|
|
72
|
+
setDeleting(false);
|
|
73
|
+
if (res.ok) {
|
|
74
|
+
onDeleted?.();
|
|
75
|
+
} else {
|
|
76
|
+
setDeleteError(errorMessage(res.error, "Failed to delete tenant."));
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
if (error) {
|
|
81
|
+
return (
|
|
82
|
+
<Alert variant="destructive">
|
|
83
|
+
<AlertCircle />
|
|
84
|
+
<AlertDescription>{errorMessage(error)}</AlertDescription>
|
|
85
|
+
</Alert>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div className="grid gap-6">
|
|
91
|
+
<Card>
|
|
92
|
+
<CardHeader>
|
|
93
|
+
<CardTitle>General</CardTitle>
|
|
94
|
+
<CardDescription>
|
|
95
|
+
{canManage ? "Update tenant information" : "Tenant information"}
|
|
96
|
+
</CardDescription>
|
|
97
|
+
</CardHeader>
|
|
98
|
+
<CardContent className="grid gap-4">
|
|
99
|
+
{loading ? (
|
|
100
|
+
<>
|
|
101
|
+
<Skeleton className="h-5 w-48" />
|
|
102
|
+
<Skeleton className="h-5 w-32" />
|
|
103
|
+
<Skeleton className="h-5 w-64" />
|
|
104
|
+
</>
|
|
105
|
+
) : data ? (
|
|
106
|
+
<>
|
|
107
|
+
{canManage ? (
|
|
108
|
+
<GeneralForm
|
|
109
|
+
organizationId={organizationId}
|
|
110
|
+
name={data.name}
|
|
111
|
+
slug={data.slug}
|
|
112
|
+
onSaved={refresh}
|
|
113
|
+
/>
|
|
114
|
+
) : (
|
|
115
|
+
<>
|
|
116
|
+
<div className="grid gap-1">
|
|
117
|
+
<Label className="text-muted-foreground text-xs">Name</Label>
|
|
118
|
+
<p className="text-sm font-medium">{data.name}</p>
|
|
119
|
+
</div>
|
|
120
|
+
<div className="grid gap-1">
|
|
121
|
+
<Label className="text-muted-foreground text-xs">Slug</Label>
|
|
122
|
+
<p className="font-mono text-sm">{data.slug}</p>
|
|
123
|
+
</div>
|
|
124
|
+
</>
|
|
125
|
+
)}
|
|
126
|
+
{data.projectId ? (
|
|
127
|
+
<div className="grid gap-1">
|
|
128
|
+
<Label className="text-muted-foreground text-xs">Product</Label>
|
|
129
|
+
<p className="text-sm font-medium">
|
|
130
|
+
{productName ?? <code className="font-mono text-xs">{data.projectId}</code>}
|
|
131
|
+
</p>
|
|
132
|
+
</div>
|
|
133
|
+
) : null}
|
|
134
|
+
<div className="grid gap-1">
|
|
135
|
+
<Label className="text-muted-foreground text-xs">UUID</Label>
|
|
136
|
+
<div className="flex items-center gap-2">
|
|
137
|
+
<code className="rounded bg-muted px-2 py-1 font-mono text-xs">
|
|
138
|
+
{organizationId}
|
|
139
|
+
</code>
|
|
140
|
+
<Button
|
|
141
|
+
variant="ghost"
|
|
142
|
+
size="icon"
|
|
143
|
+
className="size-7"
|
|
144
|
+
onClick={copyId}
|
|
145
|
+
aria-label="Copy UUID"
|
|
146
|
+
>
|
|
147
|
+
{copied ? (
|
|
148
|
+
<Check className="size-3.5 text-green-600" />
|
|
149
|
+
) : (
|
|
150
|
+
<Copy className="size-3.5" />
|
|
151
|
+
)}
|
|
152
|
+
</Button>
|
|
153
|
+
</div>
|
|
154
|
+
</div>
|
|
155
|
+
</>
|
|
156
|
+
) : null}
|
|
157
|
+
</CardContent>
|
|
158
|
+
</Card>
|
|
159
|
+
|
|
160
|
+
{canDelete && (
|
|
161
|
+
<Card className="border-destructive/50">
|
|
162
|
+
<CardHeader>
|
|
163
|
+
<CardTitle className="text-destructive">Danger zone</CardTitle>
|
|
164
|
+
<CardDescription>
|
|
165
|
+
Deleting a tenant removes all its members, roles and data. This
|
|
166
|
+
action cannot be undone.
|
|
167
|
+
</CardDescription>
|
|
168
|
+
</CardHeader>
|
|
169
|
+
<CardContent className="grid gap-6">
|
|
170
|
+
<ChangeIdForm
|
|
171
|
+
organizationId={organizationId}
|
|
172
|
+
onChanged={onIdChanged}
|
|
173
|
+
/>
|
|
174
|
+
|
|
175
|
+
{deleteError && (
|
|
176
|
+
<Alert variant="destructive" className="mb-4">
|
|
177
|
+
<AlertCircle />
|
|
178
|
+
<AlertDescription>{deleteError}</AlertDescription>
|
|
179
|
+
</Alert>
|
|
180
|
+
)}
|
|
181
|
+
<Dialog>
|
|
182
|
+
<DialogTrigger asChild>
|
|
183
|
+
<Button variant="destructive">
|
|
184
|
+
<Trash2 /> Delete tenant
|
|
185
|
+
</Button>
|
|
186
|
+
</DialogTrigger>
|
|
187
|
+
<DialogContent>
|
|
188
|
+
<DialogHeader>
|
|
189
|
+
<DialogTitle>Delete tenant?</DialogTitle>
|
|
190
|
+
<DialogDescription>
|
|
191
|
+
This will permanently delete{" "}
|
|
192
|
+
<strong>{data?.name ?? "this tenant"}</strong> and all
|
|
193
|
+
associated data. This action cannot be undone.
|
|
194
|
+
</DialogDescription>
|
|
195
|
+
</DialogHeader>
|
|
196
|
+
<DialogFooter>
|
|
197
|
+
<DialogClose asChild>
|
|
198
|
+
<Button variant="outline">Cancel</Button>
|
|
199
|
+
</DialogClose>
|
|
200
|
+
<Button
|
|
201
|
+
variant="destructive"
|
|
202
|
+
onClick={handleDelete}
|
|
203
|
+
loading={deleting}
|
|
204
|
+
>
|
|
205
|
+
Delete permanently
|
|
206
|
+
</Button>
|
|
207
|
+
</DialogFooter>
|
|
208
|
+
</DialogContent>
|
|
209
|
+
</Dialog>
|
|
210
|
+
</CardContent>
|
|
211
|
+
</Card>
|
|
212
|
+
)}
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const GeneralForm = ({
|
|
218
|
+
organizationId,
|
|
219
|
+
name: initialName,
|
|
220
|
+
slug: initialSlug,
|
|
221
|
+
onSaved,
|
|
222
|
+
}: {
|
|
223
|
+
organizationId: string;
|
|
224
|
+
name: string;
|
|
225
|
+
slug: string;
|
|
226
|
+
onSaved: () => void;
|
|
227
|
+
}) => {
|
|
228
|
+
const client = useUmClient();
|
|
229
|
+
const [name, setName] = useState(initialName);
|
|
230
|
+
const [slug, setSlug] = useState(initialSlug);
|
|
231
|
+
const [saving, setSaving] = useState(false);
|
|
232
|
+
const [saved, setSaved] = useState(false);
|
|
233
|
+
const [error, setError] = useState<string | null>(null);
|
|
234
|
+
|
|
235
|
+
// Server data refreshed underneath us (e.g. another tab saved) —
|
|
236
|
+
// resync untouched fields.
|
|
237
|
+
useEffect(() => setName(initialName), [initialName]);
|
|
238
|
+
useEffect(() => setSlug(initialSlug), [initialSlug]);
|
|
239
|
+
|
|
240
|
+
const dirty = name.trim() !== initialName || slug.trim() !== initialSlug;
|
|
241
|
+
|
|
242
|
+
const submit = async (e: FormEvent) => {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
setSaving(true);
|
|
245
|
+
setError(null);
|
|
246
|
+
setSaved(false);
|
|
247
|
+
const res = await client.updateTenant({
|
|
248
|
+
organizationId,
|
|
249
|
+
data: { name: name.trim(), slug: slug.trim() },
|
|
250
|
+
});
|
|
251
|
+
setSaving(false);
|
|
252
|
+
if (res.ok) {
|
|
253
|
+
setSaved(true);
|
|
254
|
+
setTimeout(() => setSaved(false), 2000);
|
|
255
|
+
onSaved();
|
|
256
|
+
} else {
|
|
257
|
+
setError(errorMessage(res.error, "Couldn't save tenant settings."));
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
return (
|
|
262
|
+
<form onSubmit={submit} className="grid gap-4">
|
|
263
|
+
{error ? (
|
|
264
|
+
<Alert variant="destructive">
|
|
265
|
+
<AlertCircle />
|
|
266
|
+
<AlertDescription>{error}</AlertDescription>
|
|
267
|
+
</Alert>
|
|
268
|
+
) : null}
|
|
269
|
+
<div className="grid gap-4 sm:grid-cols-2">
|
|
270
|
+
<Field label="Name" htmlFor="tenant-name">
|
|
271
|
+
<Input
|
|
272
|
+
id="tenant-name"
|
|
273
|
+
required
|
|
274
|
+
value={name}
|
|
275
|
+
onChange={(e) => setName(e.target.value)}
|
|
276
|
+
/>
|
|
277
|
+
</Field>
|
|
278
|
+
<Field label="Slug" htmlFor="tenant-slug">
|
|
279
|
+
<Input
|
|
280
|
+
id="tenant-slug"
|
|
281
|
+
required
|
|
282
|
+
value={slug}
|
|
283
|
+
onChange={(e) => setSlug(e.target.value)}
|
|
284
|
+
className="font-mono"
|
|
285
|
+
/>
|
|
286
|
+
</Field>
|
|
287
|
+
</div>
|
|
288
|
+
<div className="flex items-center gap-3">
|
|
289
|
+
<Button type="submit" loading={saving} disabled={!dirty || !name.trim() || !slug.trim()}>
|
|
290
|
+
{saved ? <Check /> : <Save />}
|
|
291
|
+
{saved ? "Saved" : "Save changes"}
|
|
292
|
+
</Button>
|
|
293
|
+
</div>
|
|
294
|
+
</form>
|
|
295
|
+
);
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// Superadmin-only uuid rewrite — members, invitations, roles and
|
|
299
|
+
// live sessions are repointed server-side in one transaction. Meant
|
|
300
|
+
// for aligning the tenant uuid with an external system's key.
|
|
301
|
+
const ChangeIdForm = ({
|
|
302
|
+
organizationId,
|
|
303
|
+
onChanged,
|
|
304
|
+
}: {
|
|
305
|
+
organizationId: string;
|
|
306
|
+
onChanged?: (newId: string) => void;
|
|
307
|
+
}) => {
|
|
308
|
+
const client = useUmClient();
|
|
309
|
+
const [newId, setNewId] = useState("");
|
|
310
|
+
const [busy, setBusy] = useState(false);
|
|
311
|
+
const [error, setError] = useState<string | null>(null);
|
|
312
|
+
|
|
313
|
+
const valid =
|
|
314
|
+
/^[A-Za-z0-9_-]{1,64}$/.test(newId.trim()) && newId.trim() !== organizationId;
|
|
315
|
+
|
|
316
|
+
const submit = async () => {
|
|
317
|
+
setBusy(true);
|
|
318
|
+
setError(null);
|
|
319
|
+
const res = await client.adminChangeTenantId({
|
|
320
|
+
id: organizationId,
|
|
321
|
+
newId: newId.trim(),
|
|
322
|
+
});
|
|
323
|
+
setBusy(false);
|
|
324
|
+
if (res.ok) {
|
|
325
|
+
onChanged?.(res.data.id);
|
|
326
|
+
} else {
|
|
327
|
+
setError(errorMessage(res.error, "Couldn't change the tenant UUID."));
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
return (
|
|
332
|
+
<div className="grid gap-2">
|
|
333
|
+
<Label htmlFor="tenant-new-id">Change UUID</Label>
|
|
334
|
+
<p className="text-muted-foreground text-xs">
|
|
335
|
+
Rewrites the tenant's uuid and repoints members, roles and live
|
|
336
|
+
sessions. Use it to match an external system's key. Signed-in
|
|
337
|
+
users of this tenant may see errors until their next token
|
|
338
|
+
refresh.
|
|
339
|
+
</p>
|
|
340
|
+
{error ? (
|
|
341
|
+
<Alert variant="destructive">
|
|
342
|
+
<AlertCircle />
|
|
343
|
+
<AlertDescription>{error}</AlertDescription>
|
|
344
|
+
</Alert>
|
|
345
|
+
) : null}
|
|
346
|
+
<div className="flex items-center gap-2">
|
|
347
|
+
<Input
|
|
348
|
+
id="tenant-new-id"
|
|
349
|
+
value={newId}
|
|
350
|
+
onChange={(e) => setNewId(e.target.value)}
|
|
351
|
+
placeholder="new-uuid"
|
|
352
|
+
className="max-w-sm font-mono"
|
|
353
|
+
/>
|
|
354
|
+
<Dialog>
|
|
355
|
+
<DialogTrigger asChild>
|
|
356
|
+
<Button variant="destructive" disabled={!valid || busy}>
|
|
357
|
+
Change UUID
|
|
358
|
+
</Button>
|
|
359
|
+
</DialogTrigger>
|
|
360
|
+
<DialogContent>
|
|
361
|
+
<DialogHeader>
|
|
362
|
+
<DialogTitle>Change tenant UUID?</DialogTitle>
|
|
363
|
+
<DialogDescription>
|
|
364
|
+
The tenant id becomes{" "}
|
|
365
|
+
<code className="font-mono text-xs">{newId.trim()}</code>. Every
|
|
366
|
+
reference is repointed; external systems keyed on the old uuid
|
|
367
|
+
must be updated in the same maintenance window.
|
|
368
|
+
</DialogDescription>
|
|
369
|
+
</DialogHeader>
|
|
370
|
+
<DialogFooter>
|
|
371
|
+
<DialogClose asChild>
|
|
372
|
+
<Button variant="outline">Cancel</Button>
|
|
373
|
+
</DialogClose>
|
|
374
|
+
<DialogClose asChild>
|
|
375
|
+
<Button variant="destructive" onClick={() => void submit()} loading={busy}>
|
|
376
|
+
Change UUID
|
|
377
|
+
</Button>
|
|
378
|
+
</DialogClose>
|
|
379
|
+
</DialogFooter>
|
|
380
|
+
</DialogContent>
|
|
381
|
+
</Dialog>
|
|
382
|
+
</div>
|
|
383
|
+
</div>
|
|
384
|
+
);
|
|
385
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { afterEach, describe, expect, test } from "bun:test";
|
|
3
|
+
|
|
4
|
+
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
5
|
+
|
|
6
|
+
import { mockRpc, reply } from "@nexys/user-management-client/testing";
|
|
7
|
+
|
|
8
|
+
import { AuthProvider } from "../../context/auth-context.js";
|
|
9
|
+
import { TenantsTable } from "./tenants-table.js";
|
|
10
|
+
|
|
11
|
+
// Deliberately out of order and mixed case: the table must render them
|
|
12
|
+
// alphabetically A–Z regardless of what the server sends.
|
|
13
|
+
const TENANTS = [
|
|
14
|
+
{ id: "t1", name: "zeta", slug: "zeta", projectId: null, createdAt: "2026-03-01T00:00:00Z", memberCount: 4 },
|
|
15
|
+
{ id: "t2", name: "Alpha", slug: "alpha", projectId: null, createdAt: "2026-01-01T00:00:00Z", memberCount: 1 },
|
|
16
|
+
{ id: "t3", name: "beta", slug: "beta", projectId: null, createdAt: "2026-02-01T00:00:00Z", memberCount: 9 },
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
document.body.innerHTML = "";
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const clientFor = () => {
|
|
24
|
+
const rpc = mockRpc((p) => {
|
|
25
|
+
if (p === "me") {
|
|
26
|
+
return reply({ code: "unauth" }, 401);
|
|
27
|
+
}
|
|
28
|
+
if (p === "adminListTenants") {
|
|
29
|
+
return reply({ tenants: TENANTS });
|
|
30
|
+
}
|
|
31
|
+
if (p === "listProjects") {
|
|
32
|
+
return reply({ projects: [] });
|
|
33
|
+
}
|
|
34
|
+
return reply({ error: "not_found" }, 404);
|
|
35
|
+
});
|
|
36
|
+
return rpc.client;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
describe("<TenantsTable />", () => {
|
|
40
|
+
test("renders tenants alphabetically A–Z by default", async () => {
|
|
41
|
+
render(
|
|
42
|
+
<AuthProvider client={clientFor()} refreshIntervalMs={0}>
|
|
43
|
+
<TenantsTable allowCreate={false} />
|
|
44
|
+
</AuthProvider>
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
await waitFor(() => {
|
|
48
|
+
expect(screen.getByText("Alpha")).toBeTruthy();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const rows = screen.getAllByRole("row").slice(1); // drop the header
|
|
52
|
+
const rowText = rows.map((r) => r.textContent ?? "");
|
|
53
|
+
// Case-insensitive: "beta" sits between "Alpha" and "zeta".
|
|
54
|
+
expect(rowText[0]).toContain("Alpha");
|
|
55
|
+
expect(rowText[1]).toContain("beta");
|
|
56
|
+
expect(rowText[2]).toContain("zeta");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("shows the product filter and sort controls in the toolbar", async () => {
|
|
60
|
+
render(
|
|
61
|
+
<AuthProvider client={clientFor()} refreshIntervalMs={0}>
|
|
62
|
+
<TenantsTable allowCreate={false} />
|
|
63
|
+
</AuthProvider>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
await waitFor(() => {
|
|
67
|
+
expect(screen.getByLabelText("Sort by")).toBeTruthy();
|
|
68
|
+
});
|
|
69
|
+
expect(screen.getByLabelText("Filter by product")).toBeTruthy();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AdminTenantRow as TenantRow,
|
|
3
|
+
} from "@nexys/user-management-client";
|
|
4
|
+
import {
|
|
5
|
+
AlertCircle,
|
|
6
|
+
ArrowUpDown,
|
|
7
|
+
Building2,
|
|
8
|
+
ListFilter,
|
|
9
|
+
RefreshCcw,
|
|
10
|
+
Search,
|
|
11
|
+
} from "lucide-react";
|
|
12
|
+
import { useMemo, useState } from "react";
|
|
13
|
+
|
|
14
|
+
import { useCall } from "../../hooks/use-call.js";
|
|
15
|
+
import { errorMessage } from "../../lib/error-message.js";
|
|
16
|
+
import { formatRelative } from "../../lib/utils.js";
|
|
17
|
+
import { EmptyState } from "../primitives/empty-state.js";
|
|
18
|
+
import { Alert, AlertDescription } from "../ui/alert.js";
|
|
19
|
+
import { Badge } from "../ui/badge.js";
|
|
20
|
+
import { Button } from "../ui/button.js";
|
|
21
|
+
import { Input } from "../ui/input.js";
|
|
22
|
+
import {
|
|
23
|
+
Select,
|
|
24
|
+
SelectContent,
|
|
25
|
+
SelectItem,
|
|
26
|
+
SelectTrigger,
|
|
27
|
+
SelectValue,
|
|
28
|
+
} from "../ui/select.js";
|
|
29
|
+
import { Skeleton } from "../ui/skeleton.js";
|
|
30
|
+
import {
|
|
31
|
+
Table,
|
|
32
|
+
TableBody,
|
|
33
|
+
TableCell,
|
|
34
|
+
TableHead,
|
|
35
|
+
TableHeader,
|
|
36
|
+
TableRow,
|
|
37
|
+
} from "../ui/table.js";
|
|
38
|
+
import { TenantCreateDialog } from "./tenant-create-dialog.js";
|
|
39
|
+
|
|
40
|
+
const SORT_OPTIONS: {
|
|
41
|
+
value: string;
|
|
42
|
+
label: string;
|
|
43
|
+
compare: (a: TenantRow, b: TenantRow) => number;
|
|
44
|
+
}[] = [
|
|
45
|
+
{
|
|
46
|
+
value: "name-asc",
|
|
47
|
+
label: "Name A–Z",
|
|
48
|
+
compare: (a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: "base" }),
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
value: "name-desc",
|
|
52
|
+
label: "Name Z–A",
|
|
53
|
+
compare: (a, b) => b.name.localeCompare(a.name, undefined, { sensitivity: "base" }),
|
|
54
|
+
},
|
|
55
|
+
{ value: "created-desc", label: "Newest first", compare: (a, b) => b.createdAt.localeCompare(a.createdAt) },
|
|
56
|
+
{ value: "created-asc", label: "Oldest first", compare: (a, b) => a.createdAt.localeCompare(b.createdAt) },
|
|
57
|
+
{ value: "members-desc", label: "Most members", compare: (a, b) => b.memberCount - a.memberCount },
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
export const TenantsTable = ({
|
|
61
|
+
allowCreate = true,
|
|
62
|
+
onSelect,
|
|
63
|
+
}: {
|
|
64
|
+
allowCreate?: boolean;
|
|
65
|
+
onSelect?: (id: string) => void;
|
|
66
|
+
}) => {
|
|
67
|
+
const [query, setQuery] = useState("");
|
|
68
|
+
const [sort, setSort] = useState("name-asc");
|
|
69
|
+
const [productFilter, setProductFilter] = useState("all");
|
|
70
|
+
const { data, error, loading, refresh } = useCall("adminListTenants");
|
|
71
|
+
// Resolve tenant.projectId → product name. Superadmins (the only
|
|
72
|
+
// callers of this table) see every project here.
|
|
73
|
+
const projects = useCall("listProjects");
|
|
74
|
+
const productNames = useMemo(() => {
|
|
75
|
+
const map = new Map<string, string>();
|
|
76
|
+
for (const p of projects.data?.projects ?? []) map.set(p.id, p.name);
|
|
77
|
+
return map;
|
|
78
|
+
}, [projects.data]);
|
|
79
|
+
|
|
80
|
+
const filtered = useMemo(() => {
|
|
81
|
+
let list = data?.tenants ?? [];
|
|
82
|
+
if (productFilter !== "all") {
|
|
83
|
+
list = list.filter((t) =>
|
|
84
|
+
productFilter === "none" ? !t.projectId : t.projectId === productFilter
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const q = query.trim().toLowerCase();
|
|
88
|
+
if (q) {
|
|
89
|
+
list = list.filter(
|
|
90
|
+
(t) =>
|
|
91
|
+
t.name.toLowerCase().includes(q) ||
|
|
92
|
+
t.slug.toLowerCase().includes(q) ||
|
|
93
|
+
t.id.toLowerCase().includes(q)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
const compare = (SORT_OPTIONS.find((o) => o.value === sort) ?? SORT_OPTIONS[0]!)
|
|
97
|
+
.compare;
|
|
98
|
+
return [...list].sort(compare);
|
|
99
|
+
}, [data, query, productFilter, sort]);
|
|
100
|
+
|
|
101
|
+
const filterActive = productFilter !== "all";
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<div className="grid gap-3">
|
|
105
|
+
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
106
|
+
<div className="relative min-w-[14rem] max-w-sm flex-1">
|
|
107
|
+
<Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
|
|
108
|
+
<Input
|
|
109
|
+
value={query}
|
|
110
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
111
|
+
placeholder="Search tenants…"
|
|
112
|
+
className="pl-9"
|
|
113
|
+
/>
|
|
114
|
+
</div>
|
|
115
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
116
|
+
<Select value={productFilter} onValueChange={setProductFilter}>
|
|
117
|
+
<SelectTrigger
|
|
118
|
+
className="h-8 w-[10.5rem]"
|
|
119
|
+
aria-label="Filter by product"
|
|
120
|
+
>
|
|
121
|
+
<ListFilter className="size-3.5 text-muted-foreground" />
|
|
122
|
+
<SelectValue />
|
|
123
|
+
</SelectTrigger>
|
|
124
|
+
<SelectContent>
|
|
125
|
+
<SelectItem value="all">All products</SelectItem>
|
|
126
|
+
<SelectItem value="none">No product</SelectItem>
|
|
127
|
+
{(projects.data?.projects ?? []).map((p) => (
|
|
128
|
+
<SelectItem key={p.id} value={p.id}>
|
|
129
|
+
{p.name}
|
|
130
|
+
</SelectItem>
|
|
131
|
+
))}
|
|
132
|
+
</SelectContent>
|
|
133
|
+
</Select>
|
|
134
|
+
<Select value={sort} onValueChange={setSort}>
|
|
135
|
+
<SelectTrigger className="h-8 w-[9.5rem]" aria-label="Sort by">
|
|
136
|
+
<ArrowUpDown className="size-3.5 text-muted-foreground" />
|
|
137
|
+
<SelectValue />
|
|
138
|
+
</SelectTrigger>
|
|
139
|
+
<SelectContent>
|
|
140
|
+
{SORT_OPTIONS.map((o) => (
|
|
141
|
+
<SelectItem key={o.value} value={o.value}>
|
|
142
|
+
{o.label}
|
|
143
|
+
</SelectItem>
|
|
144
|
+
))}
|
|
145
|
+
</SelectContent>
|
|
146
|
+
</Select>
|
|
147
|
+
<Button variant="outline" size="sm" onClick={refresh}>
|
|
148
|
+
<RefreshCcw />
|
|
149
|
+
Refresh
|
|
150
|
+
</Button>
|
|
151
|
+
{allowCreate ? <TenantCreateDialog onCreated={refresh} /> : null}
|
|
152
|
+
</div>
|
|
153
|
+
</div>
|
|
154
|
+
|
|
155
|
+
{error ? (
|
|
156
|
+
<Alert variant="destructive">
|
|
157
|
+
<AlertCircle />
|
|
158
|
+
<AlertDescription>{errorMessage(error)}</AlertDescription>
|
|
159
|
+
</Alert>
|
|
160
|
+
) : null}
|
|
161
|
+
|
|
162
|
+
<div className="overflow-hidden rounded-xl border bg-card">
|
|
163
|
+
<Table>
|
|
164
|
+
<TableHeader>
|
|
165
|
+
<TableRow>
|
|
166
|
+
<TableHead>Tenant</TableHead>
|
|
167
|
+
<TableHead>Slug</TableHead>
|
|
168
|
+
<TableHead>Product</TableHead>
|
|
169
|
+
<TableHead>Members</TableHead>
|
|
170
|
+
<TableHead>Created</TableHead>
|
|
171
|
+
</TableRow>
|
|
172
|
+
</TableHeader>
|
|
173
|
+
<TableBody>
|
|
174
|
+
{loading ? (
|
|
175
|
+
Array.from({ length: 4 }).map((_, i) => (
|
|
176
|
+
<TableRow key={`sk-${i}`}>
|
|
177
|
+
<TableCell><Skeleton className="h-4 w-40" /></TableCell>
|
|
178
|
+
<TableCell><Skeleton className="h-4 w-24" /></TableCell>
|
|
179
|
+
<TableCell><Skeleton className="h-4 w-24" /></TableCell>
|
|
180
|
+
<TableCell><Skeleton className="h-4 w-10" /></TableCell>
|
|
181
|
+
<TableCell><Skeleton className="h-4 w-20" /></TableCell>
|
|
182
|
+
</TableRow>
|
|
183
|
+
))
|
|
184
|
+
) : filtered.length === 0 ? (
|
|
185
|
+
<TableRow>
|
|
186
|
+
<TableCell colSpan={5} className="p-0">
|
|
187
|
+
<EmptyState
|
|
188
|
+
icon={Building2}
|
|
189
|
+
title={
|
|
190
|
+
query || filterActive ? "No tenants match" : "No tenants yet"
|
|
191
|
+
}
|
|
192
|
+
description={
|
|
193
|
+
query || filterActive
|
|
194
|
+
? "Try a different search or filter."
|
|
195
|
+
: "Create the first tenant to get started."
|
|
196
|
+
}
|
|
197
|
+
className="border-0 rounded-none bg-transparent"
|
|
198
|
+
action={
|
|
199
|
+
!query && !filterActive && allowCreate ? (
|
|
200
|
+
<TenantCreateDialog onCreated={refresh} />
|
|
201
|
+
) : undefined
|
|
202
|
+
}
|
|
203
|
+
/>
|
|
204
|
+
</TableCell>
|
|
205
|
+
</TableRow>
|
|
206
|
+
) : (
|
|
207
|
+
filtered.map((t) => (
|
|
208
|
+
<TableRow
|
|
209
|
+
key={t.id}
|
|
210
|
+
className={onSelect ? "cursor-pointer" : undefined}
|
|
211
|
+
onClick={onSelect ? () => onSelect(t.id) : undefined}
|
|
212
|
+
>
|
|
213
|
+
<TableCell>
|
|
214
|
+
<div className="flex items-center gap-2.5">
|
|
215
|
+
<div className="flex size-8 items-center justify-center rounded-md bg-muted text-xs font-semibold uppercase">
|
|
216
|
+
{t.name.slice(0, 2)}
|
|
217
|
+
</div>
|
|
218
|
+
<span className="font-medium">{t.name}</span>
|
|
219
|
+
</div>
|
|
220
|
+
</TableCell>
|
|
221
|
+
<TableCell className="font-mono text-xs">{t.slug}</TableCell>
|
|
222
|
+
<TableCell>
|
|
223
|
+
{t.projectId ? (
|
|
224
|
+
<Badge variant="muted">
|
|
225
|
+
{productNames.get(t.projectId) ?? t.projectId}
|
|
226
|
+
</Badge>
|
|
227
|
+
) : (
|
|
228
|
+
<span className="text-muted-foreground text-xs">—</span>
|
|
229
|
+
)}
|
|
230
|
+
</TableCell>
|
|
231
|
+
<TableCell>
|
|
232
|
+
<Badge variant="muted">{t.memberCount}</Badge>
|
|
233
|
+
</TableCell>
|
|
234
|
+
<TableCell title={t.createdAt} className="text-sm text-muted-foreground">
|
|
235
|
+
{formatRelative(t.createdAt)}
|
|
236
|
+
</TableCell>
|
|
237
|
+
</TableRow>
|
|
238
|
+
))
|
|
239
|
+
)}
|
|
240
|
+
</TableBody>
|
|
241
|
+
</Table>
|
|
242
|
+
</div>
|
|
243
|
+
</div>
|
|
244
|
+
);
|
|
245
|
+
};
|