@alepha/ui 0.25.1 → 0.25.2

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 (74) hide show
  1. package/package.json +3 -3
  2. package/src/components/account/__tests__/account-router.spec.ts +201 -0
  3. package/src/components/account/account-connections.tsx +103 -0
  4. package/src/components/account/account-delete-dialog.tsx +184 -0
  5. package/src/components/account/account-header.tsx +57 -0
  6. package/src/components/account/account-keys.tsx +221 -0
  7. package/src/components/account/account-layout.tsx +63 -0
  8. package/src/components/account/account-password-dialog.tsx +152 -0
  9. package/src/components/account/account-profile.tsx +229 -0
  10. package/src/components/account/account-router-options.tsx +74 -0
  11. package/src/components/account/account-router-page.tsx +69 -0
  12. package/src/components/account/account-router.tsx +214 -0
  13. package/src/components/account/account-security.tsx +150 -0
  14. package/src/components/account/account-sessions.tsx +140 -0
  15. package/src/components/admin/__tests__/admin-keys-token-dialog.browser.spec.tsx +62 -0
  16. package/src/components/admin/__tests__/admin-layout.browser.spec.tsx +82 -0
  17. package/src/components/admin/__tests__/admin-router-page.spec.ts +54 -0
  18. package/src/components/admin/__tests__/admin-router.spec.ts +208 -0
  19. package/src/components/admin/__tests__/admin-user-cell.browser.spec.tsx +99 -0
  20. package/src/components/admin/__tests__/admin-user-detail.spec.ts +29 -0
  21. package/src/components/admin/admin-analytics-query-panel.tsx +149 -0
  22. package/src/components/admin/admin-analytics-results.tsx +95 -0
  23. package/src/components/admin/admin-analytics.tsx +104 -0
  24. package/src/components/admin/admin-audits.tsx +12 -15
  25. package/src/components/admin/admin-files.tsx +10 -18
  26. package/src/components/admin/admin-jobs-executions-panel.tsx +2 -1
  27. package/src/components/admin/admin-jobs.tsx +3 -1
  28. package/src/components/admin/admin-keys-token-dialog.tsx +123 -0
  29. package/src/components/admin/admin-keys.tsx +54 -3
  30. package/src/components/admin/admin-layout.tsx +107 -0
  31. package/src/components/admin/admin-payments.tsx +4 -5
  32. package/src/components/admin/admin-router-options.tsx +130 -0
  33. package/src/components/admin/admin-router-page.tsx +74 -0
  34. package/src/components/admin/admin-router.tsx +379 -0
  35. package/src/components/admin/admin-sessions.tsx +2 -16
  36. package/src/components/admin/admin-user-cell.tsx +70 -0
  37. package/src/components/admin/admin-user-detail-overview-tab.tsx +28 -0
  38. package/src/components/admin/admin-user-detail-profile-policy.spec.ts +146 -0
  39. package/src/components/admin/admin-user-detail-profile-policy.ts +147 -0
  40. package/src/components/admin/admin-user-detail-security-tab.tsx +1 -1
  41. package/src/components/admin/admin-user-detail.tsx +76 -32
  42. package/src/components/admin/admin-users-status-filter.tsx +2 -0
  43. package/src/components/admin/admin-workflows-execution-sheet.tsx +277 -0
  44. package/src/components/admin/admin-workflows-executions-panel.tsx +170 -0
  45. package/src/components/admin/admin-workflows-status-badge.tsx +40 -0
  46. package/src/components/admin/admin-workflows.tsx +207 -0
  47. package/src/components/alepha-table/alepha-table.tsx +73 -17
  48. package/src/components/app-actions/__tests__/app-actions.browser.spec.tsx +79 -0
  49. package/src/components/app-actions/app-actions.tsx +113 -0
  50. package/src/components/app-shell/app-shell.tsx +122 -0
  51. package/src/components/auth/auth-reset-password.tsx +81 -8
  52. package/src/components/button-user/button-user.tsx +58 -1
  53. package/src/components/control/control.tsx +14 -71
  54. package/src/components/control-password/control-password.tsx +87 -0
  55. package/src/components/control-select/__tests__/control-select-clear-label.browser.spec.tsx +101 -0
  56. package/src/components/control-select/__tests__/control-select-searchable.browser.spec.tsx +142 -0
  57. package/src/components/control-select/control-select.tsx +78 -111
  58. package/src/components/control-upload/control-upload.tsx +27 -1
  59. package/src/components/markdown-view/markdown-view.tsx +6 -0
  60. package/src/components/nav-shell/nav-page.tsx +3 -3
  61. package/src/components/settings/__tests__/settings-nav.browser.spec.tsx +123 -0
  62. package/src/components/settings/__tests__/settings-section.browser.spec.tsx +130 -0
  63. package/src/components/settings/settings-danger-section.tsx +59 -0
  64. package/src/components/settings/settings-layout.tsx +56 -0
  65. package/src/components/settings/settings-nav.tsx +131 -0
  66. package/src/components/settings/settings-row.tsx +81 -0
  67. package/src/components/settings/settings-section.tsx +64 -0
  68. package/src/components/ui/dropdown-menu.tsx +9 -1
  69. package/src/lib/__tests__/rehype-safe-img.spec.ts +121 -0
  70. package/src/lib/i18n-fr.ts +68 -0
  71. package/src/lib/rehype-safe-img.ts +150 -0
  72. package/src/lib/resize-image.ts +135 -0
  73. package/src/styles.css +126 -1
  74. /package/src/components/{admin/admin-user-detail-provider-labels.ts → auth/provider-labels.ts} +0 -0
@@ -0,0 +1,221 @@
1
+ import { SettingsRow } from "@alepha/ui/components/settings/settings-row";
2
+ import { SettingsSection } from "@alepha/ui/components/settings/settings-section";
3
+ import { Button } from "@alepha/ui/components/ui/button";
4
+ import {
5
+ Dialog,
6
+ DialogContent,
7
+ DialogHeader,
8
+ DialogTitle,
9
+ } from "@alepha/ui/components/ui/dialog";
10
+ import { Input } from "@alepha/ui/components/ui/input";
11
+ import { Label } from "@alepha/ui/components/ui/label";
12
+ import { useDialog } from "@alepha/ui/components/use-dialog/use-dialog";
13
+ import { useToast } from "@alepha/ui/components/use-toast/use-toast";
14
+ import type { Infer } from "alepha";
15
+ import type { ApiKeyController, listApiKeyItemSchema } from "alepha/api/keys";
16
+ import { DateTimeProvider } from "alepha/datetime";
17
+ import { useClient, useInject } from "alepha/react";
18
+ import { Check, Clipboard, Plus, Trash2 } from "lucide-react";
19
+ import { type FormEvent, useState } from "react";
20
+
21
+ /**
22
+ * Derived from the framework's own response schema rather than restated, so a
23
+ * field added to the endpoint is available here the day it lands — and a
24
+ * field removed is a compile error rather than a blank cell.
25
+ */
26
+ type ApiKeyRow = Infer<typeof listApiKeyItemSchema>;
27
+
28
+ export interface AccountKeysProps {
29
+ apiKeys?: ApiKeyRow[];
30
+ }
31
+
32
+ /**
33
+ * Your own API keys: mint, see, revoke.
34
+ *
35
+ * The freshly created token is shown **once**, in a dialog that stays open
36
+ * until dismissed, because the server stores only a hash and cannot show it
37
+ * again. That is also why the create form and the reveal are separate steps
38
+ * rather than one inline row — a token that scrolls out of view behind a
39
+ * re-render is gone.
40
+ */
41
+ const AccountKeys = (props: AccountKeysProps) => {
42
+ const api = useClient<ApiKeyController>();
43
+ const dt = useInject(DateTimeProvider);
44
+ const dialog = useDialog();
45
+ const toaster = useToast();
46
+
47
+ const [keys, setKeys] = useState<ApiKeyRow[]>(props.apiKeys ?? []);
48
+ const [createOpen, setCreateOpen] = useState(false);
49
+ const [name, setName] = useState("");
50
+ const [creating, setCreating] = useState(false);
51
+ const [freshToken, setFreshToken] = useState<string | undefined>();
52
+ const [copied, setCopied] = useState(false);
53
+
54
+ const create = async (event: FormEvent) => {
55
+ event.preventDefault();
56
+ setCreating(true);
57
+ try {
58
+ const created: any = await api.createApiKey({ body: { name } });
59
+ setFreshToken(created.token);
60
+ setKeys(await (api.listApiKeys() as Promise<any>));
61
+ setName("");
62
+ setCreateOpen(false);
63
+ } catch (error: any) {
64
+ toaster.show(error?.message ?? "Could not create that key", "danger");
65
+ } finally {
66
+ setCreating(false);
67
+ }
68
+ };
69
+
70
+ const revoke = async (key: ApiKeyRow) => {
71
+ const ok = await dialog.confirm({
72
+ title: `Revoke ${key.name}?`,
73
+ description:
74
+ "Anything still using this key stops working immediately. This cannot be undone.",
75
+ confirmLabel: "Revoke",
76
+ destructive: true,
77
+ });
78
+ if (!ok) {
79
+ return;
80
+ }
81
+ try {
82
+ await api.revokeMyApiKey({ params: { id: key.id } });
83
+ setKeys((prev) => prev.filter((it) => it.id !== key.id));
84
+ } catch (error: any) {
85
+ toaster.show(error?.message ?? "Could not revoke that key", "danger");
86
+ }
87
+ };
88
+
89
+ const copy = async () => {
90
+ if (!freshToken) {
91
+ return;
92
+ }
93
+ await navigator.clipboard.writeText(freshToken);
94
+ setCopied(true);
95
+ };
96
+
97
+ return (
98
+ <>
99
+ <SettingsSection
100
+ title="API keys"
101
+ description="Keys act as you. Revoke any you no longer recognise."
102
+ >
103
+ {keys.map((key) => (
104
+ <SettingsRow
105
+ key={key.id}
106
+ label={key.name}
107
+ description={`…${key.tokenSuffix} · created ${dt
108
+ .of(key.createdAt)
109
+ .fromNow()}${
110
+ key.lastUsedAt
111
+ ? ` · last used ${dt.of(key.lastUsedAt).fromNow()}`
112
+ : " · never used"
113
+ }`}
114
+ >
115
+ <Button
116
+ variant="ghost"
117
+ size="sm"
118
+ onClick={() => revoke(key)}
119
+ aria-label={`Revoke ${key.name}`}
120
+ >
121
+ <Trash2 className="size-4" />
122
+ </Button>
123
+ </SettingsRow>
124
+ ))}
125
+
126
+ <SettingsRow
127
+ label="Create a key"
128
+ description="Shown once, at creation — it cannot be recovered afterwards."
129
+ >
130
+ <Button
131
+ variant="secondary"
132
+ size="sm"
133
+ onClick={() => setCreateOpen(true)}
134
+ >
135
+ <Plus className="size-4" />
136
+ New key
137
+ </Button>
138
+ </SettingsRow>
139
+ </SettingsSection>
140
+
141
+ <Dialog open={createOpen} onOpenChange={setCreateOpen}>
142
+ <DialogContent>
143
+ <DialogHeader>
144
+ <DialogTitle>New API key</DialogTitle>
145
+ </DialogHeader>
146
+ <form onSubmit={create} className="flex flex-col gap-4">
147
+ <div className="flex flex-col gap-1.5">
148
+ <Label htmlFor="apiKeyName">Name</Label>
149
+ <Input
150
+ id="apiKeyName"
151
+ value={name}
152
+ onChange={(event) => setName(event.target.value)}
153
+ placeholder="CI pipeline"
154
+ required
155
+ />
156
+ </div>
157
+ <div className="flex justify-end gap-2">
158
+ <Button
159
+ type="button"
160
+ variant="ghost"
161
+ onClick={() => setCreateOpen(false)}
162
+ >
163
+ Cancel
164
+ </Button>
165
+ <Button type="submit" disabled={creating}>
166
+ Create
167
+ </Button>
168
+ </div>
169
+ </form>
170
+ </DialogContent>
171
+ </Dialog>
172
+
173
+ {/* Deliberately not auto-dismissed: this is the only time the token
174
+ exists in a readable form. */}
175
+ <Dialog
176
+ open={Boolean(freshToken)}
177
+ onOpenChange={(next) => {
178
+ if (!next) {
179
+ setFreshToken(undefined);
180
+ setCopied(false);
181
+ }
182
+ }}
183
+ >
184
+ <DialogContent>
185
+ <DialogHeader>
186
+ <DialogTitle>Copy your key now</DialogTitle>
187
+ </DialogHeader>
188
+ <div className="flex flex-col gap-4">
189
+ <span className="text-muted-foreground text-sm">
190
+ This is the only time it is shown. Store it somewhere safe before
191
+ closing this dialog.
192
+ </span>
193
+ <code className="break-all rounded-md border bg-muted p-3 font-mono text-xs">
194
+ {freshToken}
195
+ </code>
196
+ <div className="flex justify-end gap-2">
197
+ <Button variant="secondary" onClick={copy}>
198
+ {copied ? (
199
+ <Check className="size-4" />
200
+ ) : (
201
+ <Clipboard className="size-4" />
202
+ )}
203
+ {copied ? "Copied" : "Copy"}
204
+ </Button>
205
+ <Button
206
+ onClick={() => {
207
+ setFreshToken(undefined);
208
+ setCopied(false);
209
+ }}
210
+ >
211
+ Done
212
+ </Button>
213
+ </div>
214
+ </div>
215
+ </DialogContent>
216
+ </Dialog>
217
+ </>
218
+ );
219
+ };
220
+
221
+ export default AccountKeys;
@@ -0,0 +1,63 @@
1
+ import { AccountHeader } from "@alepha/ui/components/account/account-header";
2
+ import { SettingsLayout } from "@alepha/ui/components/settings/settings-layout";
3
+ import { SettingsNav } from "@alepha/ui/components/settings/settings-nav";
4
+ import { useStore } from "alepha/react";
5
+ import { NestedView } from "alepha/react/router";
6
+ import { useNavEntries } from "../nav-shell/use-nav-entries.ts";
7
+ import { accountRouterOptionsAtom } from "./account-router-options.tsx";
8
+
9
+ /**
10
+ * The account shell: a centred column with a settings rail, deliberately not
11
+ * the admin console's chrome.
12
+ *
13
+ * The rail is derived from the route subtree anchored at `account` — every
14
+ * page carries its own `nav`, so there is no hand-synced list and a page
15
+ * added with `$pageAccount` appears without registering anywhere.
16
+ *
17
+ * Two things this does *not* do, both on purpose:
18
+ *
19
+ * - **No `<ColorScheme />`.** `AdminLayout` mounts one because `/admin` is
20
+ * never a child of the application's layout, so nothing else would apply
21
+ * the dark-mode class to `<html>`. This shell is normally adopted *into*
22
+ * the application's own layout (see `AccountRouter`'s `children` note), and
23
+ * an application mounting it standalone is already supplying `header` and
24
+ * owns its chrome. A second component writing to `<html>` would be the
25
+ * surprise, not the service.
26
+ * - **No scroll container.** The page scrolls. A nested `overflow-auto` would
27
+ * clip the flush card borders `SettingsSection` depends on and strand the
28
+ * sticky rail against the wrong scroll root.
29
+ *
30
+ * `useNavEntries` returns entries already filtered by `can` and permission,
31
+ * and sorted by group then order — and its `NavEntry` is structurally a
32
+ * superset of `SettingsNavItem`, so it passes straight through. That is safe
33
+ * here specifically because none of these routes is parameterised: see
34
+ * `SettingsNav` for why it takes resolved items rather than a route name.
35
+ */
36
+ export const AccountLayout = () => {
37
+ const [options] = useStore(accountRouterOptionsAtom);
38
+ const entries = useNavEntries({ root: "account" });
39
+
40
+ return (
41
+ <SettingsLayout
42
+ className={options.className}
43
+ /*
44
+ `!== undefined`, not `??`. `null ?? x` yields `x`, so nullish
45
+ coalescing would silently ignore `header: null` — the documented way
46
+ to say "this account area is nested in my own chrome, draw no bar".
47
+ Absent means default; explicit null means none.
48
+ */
49
+ header={
50
+ options.header !== undefined ? (
51
+ options.header
52
+ ) : (
53
+ <AccountHeader homeRouteName={options.homeRouteName} />
54
+ )
55
+ }
56
+ nav={<SettingsNav items={entries} />}
57
+ >
58
+ <NestedView />
59
+ </SettingsLayout>
60
+ );
61
+ };
62
+
63
+ export default AccountLayout;
@@ -0,0 +1,152 @@
1
+ import { ControlPassword } from "@alepha/ui/components/control-password/control-password";
2
+ import { Button } from "@alepha/ui/components/ui/button";
3
+ import {
4
+ Dialog,
5
+ DialogContent,
6
+ DialogHeader,
7
+ DialogTitle,
8
+ } from "@alepha/ui/components/ui/dialog";
9
+ import { useToast } from "@alepha/ui/components/use-toast/use-toast";
10
+ import type {
11
+ MyIdentityController,
12
+ MyPasswordController,
13
+ } from "alepha/api/users";
14
+ import { useClient } from "alepha/react";
15
+ import { type FormEvent, useState } from "react";
16
+
17
+ export interface AccountPasswordDialogProps {
18
+ open: boolean;
19
+
20
+ /**
21
+ * Whether the account already has a `credentials` identity. Decides both
22
+ * the copy and — critically — which endpoint is called.
23
+ */
24
+ hasPassword: boolean;
25
+
26
+ onOpenChange: (open: boolean) => void;
27
+
28
+ /**
29
+ * Called after a successful change so the caller can refresh its identity
30
+ * list — setting a first password creates one.
31
+ */
32
+ onDone?: () => void | Promise<void>;
33
+ }
34
+
35
+ /**
36
+ * Set a first password, or change an existing one.
37
+ *
38
+ * ⚠️ **These are two different endpoints and must stay so.**
39
+ * `setMyFirstPassword` takes only the new password and trusts the session,
40
+ * which is defensible when there is nothing to prove knowledge of. Reusing it
41
+ * to *change* a password would make any unattended signed-in browser a full
42
+ * account takeover.
43
+ *
44
+ * `changeMyPassword` verifies the current password and revokes every other
45
+ * session — the usual reason to change a password is believing someone else
46
+ * has it, and leaving their session alive would accomplish nothing. It
47
+ * reports how many it ended, which is why the toast can say so rather than
48
+ * leave the person wondering about their phone.
49
+ */
50
+ export const AccountPasswordDialog = (props: AccountPasswordDialogProps) => {
51
+ const passwordApi = useClient<MyPasswordController>();
52
+ const identityApi = useClient<MyIdentityController>();
53
+ const toaster = useToast();
54
+
55
+ const [currentPassword, setCurrentPassword] = useState("");
56
+ const [password, setPassword] = useState("");
57
+ const [confirmPassword, setConfirmPassword] = useState("");
58
+ const [submitting, setSubmitting] = useState(false);
59
+
60
+ const close = () => {
61
+ setCurrentPassword("");
62
+ setPassword("");
63
+ setConfirmPassword("");
64
+ props.onOpenChange(false);
65
+ };
66
+
67
+ const submit = async (event: FormEvent) => {
68
+ event.preventDefault();
69
+ if (password !== confirmPassword) {
70
+ toaster.show("The two passwords do not match", "danger");
71
+ return;
72
+ }
73
+ setSubmitting(true);
74
+ try {
75
+ if (props.hasPassword) {
76
+ const { otherSessionsRevoked } = await passwordApi.changeMyPassword({
77
+ body: { currentPassword, newPassword: password },
78
+ });
79
+ toaster.show(
80
+ otherSessionsRevoked > 0
81
+ ? `Password changed — ${otherSessionsRevoked} other session(s) signed out`
82
+ : "Password changed",
83
+ "success",
84
+ );
85
+ } else {
86
+ await identityApi.setMyFirstPassword({ body: { password } });
87
+ toaster.show("Password set", "success");
88
+ }
89
+ await props.onDone?.();
90
+ close();
91
+ } catch (error: any) {
92
+ // The realm's password policy and the wrong-current-password refusal
93
+ // both arrive here with messages worth showing verbatim.
94
+ toaster.show(error?.message ?? "Could not save that password", "danger");
95
+ } finally {
96
+ setSubmitting(false);
97
+ }
98
+ };
99
+
100
+ const title = props.hasPassword ? "Change password" : "Set a password";
101
+
102
+ return (
103
+ <Dialog open={props.open} onOpenChange={(next) => !next && close()}>
104
+ <DialogContent>
105
+ <DialogHeader>
106
+ <DialogTitle>{title}</DialogTitle>
107
+ </DialogHeader>
108
+ <form onSubmit={submit} className="flex flex-col gap-4">
109
+ <span className="text-muted-foreground text-sm">
110
+ {props.hasPassword
111
+ ? "Every other device will be signed out."
112
+ : "You will be able to sign in with your password as well as your existing method."}
113
+ </span>
114
+ {props.hasPassword ? (
115
+ <ControlPassword
116
+ id="currentPassword"
117
+ label="Current password"
118
+ value={currentPassword}
119
+ onChange={setCurrentPassword}
120
+ autoComplete="current-password"
121
+ required
122
+ />
123
+ ) : null}
124
+ <ControlPassword
125
+ id="newPassword"
126
+ label="New password"
127
+ value={password}
128
+ onChange={setPassword}
129
+ autoComplete="new-password"
130
+ required
131
+ />
132
+ <ControlPassword
133
+ id="confirmPassword"
134
+ label="Confirm new password"
135
+ value={confirmPassword}
136
+ onChange={setConfirmPassword}
137
+ autoComplete="new-password"
138
+ required
139
+ />
140
+ <div className="flex justify-end gap-2">
141
+ <Button type="button" variant="ghost" onClick={close}>
142
+ Cancel
143
+ </Button>
144
+ <Button type="submit" disabled={submitting}>
145
+ {title}
146
+ </Button>
147
+ </div>
148
+ </form>
149
+ </DialogContent>
150
+ </Dialog>
151
+ );
152
+ };
@@ -0,0 +1,229 @@
1
+ import { FileImage } from "@alepha/ui/components/file-image/file-image";
2
+ import { SettingsRow } from "@alepha/ui/components/settings/settings-row";
3
+ import { SettingsSection } from "@alepha/ui/components/settings/settings-section";
4
+ import { Badge } from "@alepha/ui/components/ui/badge";
5
+ import { Button } from "@alepha/ui/components/ui/button";
6
+ import { Input } from "@alepha/ui/components/ui/input";
7
+ import { useToast } from "@alepha/ui/components/use-toast/use-toast";
8
+ import type { MyProfile, MyProfileController } from "alepha/api/users";
9
+ import { useClient } from "alepha/react";
10
+ import { useI18n } from "alepha/react/i18n";
11
+ import { Camera, Trash2, User } from "lucide-react";
12
+ import { type ChangeEvent, type FormEvent, useRef, useState } from "react";
13
+
14
+ export interface AccountProfileProps {
15
+ /**
16
+ * Supplied by the route loader. Optional so the page can also be rendered
17
+ * standalone in a story or a test.
18
+ */
19
+ profile?: MyProfile;
20
+ }
21
+
22
+ /**
23
+ * Who you are: avatar, name, and the read-only facts about the account.
24
+ *
25
+ * Email is shown but not editable — changing it is a verification flow, not a
26
+ * profile edit, and `updateMyProfile` deliberately refuses it. Rather than
27
+ * render a disabled input with no explanation, the row says so.
28
+ */
29
+ const AccountProfile = (props: AccountProfileProps) => {
30
+ const api = useClient<MyProfileController>();
31
+ const toaster = useToast();
32
+ const { l } = useI18n();
33
+ const fileInput = useRef<HTMLInputElement>(null);
34
+
35
+ const [profile, setProfile] = useState<MyProfile | undefined>(props.profile);
36
+ const [firstName, setFirstName] = useState(props.profile?.firstName ?? "");
37
+ const [lastName, setLastName] = useState(props.profile?.lastName ?? "");
38
+ const [username, setUsername] = useState(props.profile?.username ?? "");
39
+ const [saving, setSaving] = useState(false);
40
+ const [uploading, setUploading] = useState(false);
41
+
42
+ if (!profile) {
43
+ return null;
44
+ }
45
+
46
+ const save = async (event: FormEvent) => {
47
+ event.preventDefault();
48
+ setSaving(true);
49
+ try {
50
+ const updated = await api.updateMyProfile({
51
+ body: {
52
+ firstName: firstName || undefined,
53
+ lastName: lastName || undefined,
54
+ username: username || undefined,
55
+ },
56
+ });
57
+ setProfile(updated);
58
+ toaster.show("Profile updated", "success");
59
+ } catch (error: any) {
60
+ // The username-taken 409 arrives here with its own message, which is
61
+ // the only one worth showing.
62
+ toaster.show(error?.message ?? "Could not update your profile", "danger");
63
+ } finally {
64
+ setSaving(false);
65
+ }
66
+ };
67
+
68
+ const onPickAvatar = async (event: ChangeEvent<HTMLInputElement>) => {
69
+ const file = event.target.files?.[0];
70
+ if (!file) {
71
+ return;
72
+ }
73
+ setUploading(true);
74
+ try {
75
+ setProfile(await api.updateMyAvatar({ body: { file } }));
76
+ toaster.show("Avatar updated", "success");
77
+ } catch (error: any) {
78
+ toaster.show(error?.message ?? "Could not upload that image", "danger");
79
+ } finally {
80
+ setUploading(false);
81
+ // Reset so picking the same file again still fires a change event.
82
+ if (fileInput.current) {
83
+ fileInput.current.value = "";
84
+ }
85
+ }
86
+ };
87
+
88
+ const removeAvatar = async () => {
89
+ setUploading(true);
90
+ try {
91
+ setProfile(await api.deleteMyAvatar());
92
+ } catch (error: any) {
93
+ toaster.show(error?.message ?? "Could not remove your avatar", "danger");
94
+ } finally {
95
+ setUploading(false);
96
+ }
97
+ };
98
+
99
+ return (
100
+ <>
101
+ <SettingsSection title="Profile picture">
102
+ <SettingsRow
103
+ label="Avatar"
104
+ description="PNG, JPEG, GIF or WebP, up to 5 MB."
105
+ >
106
+ <div className="flex items-center gap-3">
107
+ <button
108
+ type="button"
109
+ onClick={() => fileInput.current?.click()}
110
+ disabled={uploading}
111
+ aria-label="Change avatar"
112
+ className="relative size-14 shrink-0 cursor-pointer"
113
+ >
114
+ {/* Only the image is clipped to a circle; the camera badge sits
115
+ outside the wrapper so the round mask cannot crop it. */}
116
+ <div className="size-full overflow-hidden rounded-full border bg-muted">
117
+ <FileImage
118
+ id={profile.picture}
119
+ public
120
+ alt=""
121
+ className="size-full object-cover"
122
+ fallback={
123
+ <div className="flex size-full items-center justify-center">
124
+ <User className="size-6" />
125
+ </div>
126
+ }
127
+ />
128
+ </div>
129
+ <div className="absolute right-0 bottom-0 flex size-5 items-center justify-center rounded-full border bg-card">
130
+ <Camera className="size-3" />
131
+ </div>
132
+ </button>
133
+ {profile.picture ? (
134
+ <Button
135
+ variant="ghost"
136
+ size="sm"
137
+ onClick={removeAvatar}
138
+ disabled={uploading}
139
+ >
140
+ <Trash2 className="size-4" />
141
+ Remove
142
+ </Button>
143
+ ) : null}
144
+ </div>
145
+ </SettingsRow>
146
+ </SettingsSection>
147
+
148
+ <input
149
+ type="file"
150
+ ref={fileInput}
151
+ onChange={onPickAvatar}
152
+ accept="image/jpeg,image/png,image/webp,image/gif"
153
+ className="hidden"
154
+ />
155
+
156
+ <form onSubmit={save}>
157
+ <SettingsSection title="Name">
158
+ <SettingsRow
159
+ label="Username"
160
+ htmlFor="accountUsername"
161
+ description="Unique across this site."
162
+ >
163
+ <Input
164
+ id="accountUsername"
165
+ value={username}
166
+ onChange={(event) => setUsername(event.target.value)}
167
+ autoComplete="username"
168
+ className="sm:w-64"
169
+ />
170
+ </SettingsRow>
171
+ <SettingsRow label="First name" htmlFor="accountFirstName">
172
+ <Input
173
+ id="accountFirstName"
174
+ value={firstName}
175
+ onChange={(event) => setFirstName(event.target.value)}
176
+ autoComplete="given-name"
177
+ className="sm:w-64"
178
+ />
179
+ </SettingsRow>
180
+ <SettingsRow label="Last name" htmlFor="accountLastName">
181
+ <Input
182
+ id="accountLastName"
183
+ value={lastName}
184
+ onChange={(event) => setLastName(event.target.value)}
185
+ autoComplete="family-name"
186
+ className="sm:w-64"
187
+ />
188
+ </SettingsRow>
189
+ <SettingsRow label="">
190
+ <Button type="submit" disabled={saving}>
191
+ Save
192
+ </Button>
193
+ </SettingsRow>
194
+ </SettingsSection>
195
+ </form>
196
+
197
+ <SettingsSection title="Account">
198
+ <SettingsRow
199
+ label="Email"
200
+ description="Changing your email needs verification — it is not a profile edit."
201
+ >
202
+ <span className="text-muted-foreground text-sm">
203
+ {profile.email ?? "—"}
204
+ </span>
205
+ </SettingsRow>
206
+ <SettingsRow label="Roles">
207
+ <div className="flex flex-wrap gap-1">
208
+ {profile.roles.length > 0 ? (
209
+ profile.roles.map((role) => (
210
+ <Badge key={role} variant="outline" className="text-xs">
211
+ {role}
212
+ </Badge>
213
+ ))
214
+ ) : (
215
+ <span className="text-muted-foreground text-sm">—</span>
216
+ )}
217
+ </div>
218
+ </SettingsRow>
219
+ <SettingsRow label="Member since">
220
+ <span className="text-muted-foreground text-sm">
221
+ {String(l(profile.createdAt, { date: "LL" }))}
222
+ </span>
223
+ </SettingsRow>
224
+ </SettingsSection>
225
+ </>
226
+ );
227
+ };
228
+
229
+ export default AccountProfile;