@jgb-solutions/kit 0.6.0 → 0.8.0

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.
@@ -0,0 +1,1396 @@
1
+ // src/admin/contrast.ts
2
+ function parseHex(color) {
3
+ const hex = color.trim().replace(/^#/, "");
4
+ const full = hex.length === 3 ? [...hex].map((c) => c + c).join("") : hex;
5
+ if (!/^[0-9a-f]{6}$/i.test(full))
6
+ return null;
7
+ return [0, 2, 4].map((at) => Number.parseInt(full.slice(at, at + 2), 16));
8
+ }
9
+ function luminance([r, g, b]) {
10
+ const channel = (value) => {
11
+ const c = value / 255;
12
+ return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
13
+ };
14
+ return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
15
+ }
16
+ function contrastRatio(a, b) {
17
+ const ca = parseHex(a);
18
+ const cb = parseHex(b);
19
+ if (!ca || !cb)
20
+ return null;
21
+ const [hi, lo] = [luminance(ca), luminance(cb)].sort((x, y) => y - x);
22
+ return (hi + 0.05) / (lo + 0.05);
23
+ }
24
+ function inkOn(brand, light = "#ffffff", dark = "#1c1930") {
25
+ const onLight = contrastRatio(brand, light);
26
+ const onDark = contrastRatio(brand, dark);
27
+ if (onLight === null || onDark === null)
28
+ return light;
29
+ return onLight >= 4.5 || onLight >= onDark ? light : dark;
30
+ }
31
+
32
+ // src/account/theme.tsx
33
+ import { jsx } from "react/jsx-runtime";
34
+ var VARS = {
35
+ brand: "--jac-brand",
36
+ brandInk: "--jac-brand-ink",
37
+ brandText: "--jac-brand-text",
38
+ page: "--jac-page",
39
+ surface: "--jac-surface",
40
+ raised: "--jac-raised",
41
+ line: "--jac-line",
42
+ fieldLine: "--jac-field-line",
43
+ ink: "--jac-ink",
44
+ muted: "--jac-muted",
45
+ good: "--jac-good",
46
+ warn: "--jac-warn",
47
+ bad: "--jac-bad",
48
+ info: "--jac-info",
49
+ badFill: "--jac-bad-fill",
50
+ badInk: "--jac-bad-ink",
51
+ focus: "--jac-focus",
52
+ radiusCard: "--jac-radius-card",
53
+ radiusControl: "--jac-radius-control",
54
+ radiusButton: "--jac-radius-button",
55
+ fontDisplay: "--jac-font-display",
56
+ fontBody: "--jac-font-body"
57
+ };
58
+ var SAFE = /^[^;{}<>\\\n\r@!]*$/;
59
+ var LOADS = /url\(|image-set\(|image\(|expression\(|\/\*/i;
60
+ function accountTheme(theme = {}) {
61
+ const style = {};
62
+ for (const [key, value] of Object.entries(theme)) {
63
+ if (value === undefined || !Object.hasOwn(VARS, key) || !SAFE.test(value) || LOADS.test(value))
64
+ continue;
65
+ style[VARS[key]] = value;
66
+ }
67
+ if (theme.brand && theme.brandInk === undefined && /^#[0-9a-f]{3}([0-9a-f]{3})?$/i.test(theme.brand))
68
+ style["--jac-brand-ink"] = inkOn(theme.brand, "#ffffff", "#17171c");
69
+ return style;
70
+ }
71
+ function AccountRoot({ theme, scheme = "system", fill = false, className, children }) {
72
+ return /* @__PURE__ */ jsx("div", {
73
+ className: className ? `jac-root ${className}` : "jac-root",
74
+ "data-theme": scheme === "system" ? undefined : scheme,
75
+ "data-fill": fill ? "" : undefined,
76
+ style: accountTheme(theme),
77
+ children
78
+ });
79
+ }
80
+ // src/account/text.ts
81
+ var dateFormat = new Intl.DateTimeFormat("en", { dateStyle: "medium", timeZone: "UTC" });
82
+ var accountTextEn = {
83
+ signIn: {
84
+ title: "Sign in",
85
+ submit: "Sign in",
86
+ passkey: "Sign in with a passkey",
87
+ google: "Continue with Google",
88
+ or: "or",
89
+ noAccount: "No account yet?",
90
+ createOne: "Create one",
91
+ newHere: "New here? Create a free account",
92
+ forgot: "Forgot your password? Sign in with a passkey: it is the only way back in."
93
+ },
94
+ signUp: {
95
+ title: "Create an account",
96
+ intro: "Free, and optional: everything here works without an account.",
97
+ submit: "Create my account",
98
+ haveAccount: "Already have an account?",
99
+ signIn: "Sign in",
100
+ recovery: "We never send email, so we can’t reset a forgotten password. Add a passkey once your account is ready: it’s the only way back in."
101
+ },
102
+ signedIn: { title: "Signed in", body: "Taking you back…" },
103
+ fields: {
104
+ login: "Username or email",
105
+ username: "Username",
106
+ usernameHint: (min, max) => `${min}–${max} characters: letters, digits, dots and underscores.`,
107
+ email: "Email (optional)",
108
+ emailHint: "Only to sign in with it. We never send you email.",
109
+ password: "Password",
110
+ currentPassword: "Current password",
111
+ newPassword: "New password",
112
+ passwordHint: (min) => `At least ${min} characters.`
113
+ },
114
+ page: {
115
+ title: "Your account",
116
+ signOut: "Sign out",
117
+ welcome: "Your account is ready. Add a passkey now: it’s the only way back in if you forget your password.",
118
+ profile: "Profile",
119
+ displayName: "Name",
120
+ displayNameHint: "Shown on your account. Change it any time.",
121
+ username: "Username",
122
+ usernameRule: (days) => `You can change your username once every ${days} days. Your old one stays reserved for you for ${days} days.`,
123
+ email: "Email",
124
+ noEmail: "None",
125
+ edit: "Change",
126
+ editName: "Change your name",
127
+ editUsername: "Change your username",
128
+ save: "Save",
129
+ cancel: "Cancel",
130
+ nameSaved: "Name saved.",
131
+ usernameSaved: (username) => `Your username is now @${username}. Use it to sign in from now on.`,
132
+ usernameNextChange: (date) => `You can change it again on ${date}.`,
133
+ usernameWait: (date) => `You changed your username recently. You can change it again on ${date}.`,
134
+ admin: "Open the admin panel",
135
+ passkeys: "Passkeys",
136
+ passkeysIntro: "A passkey signs you in with your fingerprint, face or device PIN, and it is the only way back in if you forget your password.",
137
+ passkeysIntroGoogle: "A passkey signs you in with your fingerprint, face or device PIN, without going through Google.",
138
+ noPasskey: "No passkey yet. Without one, a forgotten password means a lost account.",
139
+ noPasskeyGoogle: "No passkey yet. You sign in with Google; a passkey is a quicker way in on this device.",
140
+ addPasskey: "Add a passkey",
141
+ passkeyAdded: "Passkey added.",
142
+ passkeyName: (number) => `Passkey ${number}`,
143
+ removePasskey: (name) => `Remove ${name}`,
144
+ added: (date) => `Added ${date}`,
145
+ google: "Google",
146
+ googleConnected: "Google is connected: you can sign in with Google too.",
147
+ googleIntro: "Connect your Google account to sign in with Google as well.",
148
+ connectGoogle: "Connect Google",
149
+ googleJustConnected: "Google connected. You can now sign in with Google too.",
150
+ password: "Change password",
151
+ passwordSubmit: "Change password",
152
+ passwordDone: "Password changed. Your other devices are signed out.",
153
+ setPassword: "Set a password",
154
+ setPasswordIntro: "You sign in with Google. Add a password to also sign in with your username.",
155
+ setPasswordDone: "Password set. You can now also sign in with your username.",
156
+ danger: "Delete account",
157
+ dangerIntro: "This deletes your account and everything saved in it, at once. It can’t be undone.",
158
+ dangerConfirm: "Type your password to confirm",
159
+ dangerSubmit: "Delete my account",
160
+ dangerReauth: (minutes) => `To confirm it’s you, sign in with Google again. You then have ${minutes} minutes to delete your account.`,
161
+ reauthGoogle: "Sign in with Google again",
162
+ dangerReady: (minutes) => `You signed in again less than ${minutes} minutes ago, so you can delete your account now.`,
163
+ dangerAreYouSure: "Yes, delete it"
164
+ },
165
+ errors: {
166
+ INVALID_USERNAME_OR_PASSWORD: "Wrong username or password.",
167
+ INVALID_EMAIL_OR_PASSWORD: "Wrong username, email or password.",
168
+ INVALID_PASSWORD: "That password is wrong.",
169
+ USERNAME_IS_ALREADY_TAKEN: "That username is taken. Try another one.",
170
+ USER_ALREADY_EXISTS: "An account already uses this email.",
171
+ USERNAME_TOO_SHORT: "That username is too short.",
172
+ USERNAME_TOO_LONG: "That username is too long.",
173
+ INVALID_USERNAME: "Use letters, digits, dots and underscores only.",
174
+ PASSWORD_TOO_SHORT: "That password is too short.",
175
+ PASSWORD_TOO_LONG: "That password is too long.",
176
+ BANNED_USER: "This account is suspended.",
177
+ TOO_MANY_REQUESTS: "Too many tries. Wait a minute and try again.",
178
+ REAUTH_REQUIRED: "For your safety, sign in with Google again, then delete your account.",
179
+ UNAUTHORIZED: "You were signed out. Sign in again.",
180
+ passkeyCancelled: "The passkey wasn’t used. Try again when you’re ready.",
181
+ passkeyUnsupported: "This browser can’t use passkeys.",
182
+ ACCOUNT_NOT_LINKED: "An account already uses this address. Sign in with your username and password first, then connect Google from your account page.",
183
+ GOOGLE_ALREADY_LINKED: "That Google account is already connected to another account.",
184
+ GOOGLE_EMAIL_NOT_CONFIRMED: "Google didn’t confirm this address, so it can’t be used here.",
185
+ signup_disabled: "New accounts are closed for now. If you already have one, sign in with your username.",
186
+ google: "Google sign-in didn’t work. Try again, or sign in with your username.",
187
+ SAME_USERNAME: "That’s already your username.",
188
+ USERNAME_CHANGED_RECENTLY: "You changed your username recently. Try again later.",
189
+ NAME_EMPTY: "Enter a name.",
190
+ NAME_TOO_LONG: "That name is too long.",
191
+ NAME_CHARACTERS: "Use letters, digits and punctuation only.",
192
+ fallback: "Something went wrong. Try again."
193
+ },
194
+ date: (iso) => {
195
+ const date = new Date(iso);
196
+ return Number.isNaN(date.getTime()) ? "" : dateFormat.format(date);
197
+ }
198
+ };
199
+ function errorText(text, code) {
200
+ return (code !== undefined && Object.hasOwn(text.errors, code) ? text.errors[code] : undefined) ?? text.errors.fallback;
201
+ }
202
+ // src/account/ui.tsx
203
+ import { useId } from "react";
204
+
205
+ // src/auth/rules.ts
206
+ var USERNAME = { min: 3, max: 30, pattern: /^[a-z0-9_.]+$/ };
207
+ var PASSWORD = { min: 12, max: 128 };
208
+ function usernameProblem(username) {
209
+ if (username.length < USERNAME.min)
210
+ return "too-short";
211
+ if (username.length > USERNAME.max)
212
+ return "too-long";
213
+ if (!USERNAME.pattern.test(username))
214
+ return "characters";
215
+ return null;
216
+ }
217
+ var PLACEHOLDER_EMAIL_DOMAIN = "users.invalid";
218
+ function isPlaceholderEmail(email) {
219
+ return email.toLowerCase().endsWith(`@${PLACEHOLDER_EMAIL_DOMAIN}`);
220
+ }
221
+ function placeholderEmail(randomId = () => globalThis.crypto.randomUUID()) {
222
+ return `${randomId().replaceAll("-", "")}@${PLACEHOLDER_EMAIL_DOMAIN}`;
223
+ }
224
+ function usernameFromName(name, email, fallback) {
225
+ const clean = (text) => (typeof text.normalize === "function" ? text.normalize("NFKD") : text).replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[\s-]+/g, ".").replace(/[^a-z0-9_.]/g, "").replace(/\.{2,}/g, ".").replace(/^[._]+|[._]+$/g, "").slice(0, USERNAME.max - 5).replace(/[._]+$/g, "");
226
+ for (const source of [name ?? "", (email ?? "").split("@")[0] ?? ""]) {
227
+ const base = clean(source);
228
+ if (base.length >= USERNAME.min)
229
+ return base;
230
+ }
231
+ return clean(fallback) || "member";
232
+ }
233
+ function usernameCandidates(base, random = Math.random) {
234
+ const numbered = Array.from({ length: 8 }, () => `${base}${Math.floor(1000 + random() * 9000)}`);
235
+ return [base, ...numbered];
236
+ }
237
+ function defineRoles(config) {
238
+ const known = config.roles;
239
+ const panel = config.adminPanel;
240
+ const isRole = (value) => typeof value === "string" && known.includes(value);
241
+ const split = (stored) => (stored ?? "").split(",").map((role) => role.trim());
242
+ return {
243
+ ROLES: config.roles,
244
+ DEFAULT_ROLE: config.defaultRole,
245
+ ADMIN_PANEL_ROLES: config.adminPanel,
246
+ isRole,
247
+ roleOf(stored) {
248
+ const roles = split(stored).filter(isRole);
249
+ return roles.find((role) => panel.includes(role)) ?? roles[0] ?? config.defaultRole;
250
+ },
251
+ canOpenAdmin(stored) {
252
+ return split(stored).some((role) => panel.includes(role));
253
+ }
254
+ };
255
+ }
256
+ function avatarSrc(image, px) {
257
+ if (!image)
258
+ return "";
259
+ try {
260
+ const url = new URL(image);
261
+ if (url.protocol !== "https:")
262
+ return "";
263
+ if (url.hostname.endsWith(".googleusercontent.com"))
264
+ return `${url.origin}${url.pathname.replace(/=[^/]*$/, "")}=s${px * 2}-c`;
265
+ return url.href;
266
+ } catch {
267
+ return "";
268
+ }
269
+ }
270
+ function avatarInitials(user) {
271
+ const words = (user.name.trim() || user.username).replace(/[_.-]+/g, " ").split(/\s+/).filter(Boolean);
272
+ const letters = words.length > 1 ? [words[0], words[words.length - 1]] : [words[0]];
273
+ return letters.map((word) => [...word ?? ""][0] ?? "").join("").toUpperCase();
274
+ }
275
+ var ACCOUNT_NOT_LINKED = "ACCOUNT_NOT_LINKED";
276
+ var GOOGLE_ALREADY_LINKED = "GOOGLE_ALREADY_LINKED";
277
+ var GOOGLE_EMAIL_NOT_CONFIRMED = "GOOGLE_EMAIL_NOT_CONFIRMED";
278
+ function authErrorCode(answer) {
279
+ const code = typeof answer === "string" ? answer : answer && typeof answer === "object" && ("code" in answer) ? String(answer.code) : "";
280
+ const message = answer && typeof answer === "object" && "message" in answer ? String(answer.message) : "";
281
+ if (code === "account_not_linked" || code === "OAUTH_LINK_ERROR" && message === "account not linked")
282
+ return ACCOUNT_NOT_LINKED;
283
+ if (code === "SOCIAL_ACCOUNT_ALREADY_LINKED" || code === "account_already_linked_to_different_user")
284
+ return GOOGLE_ALREADY_LINKED;
285
+ if (code === "LINKING_NOT_ALLOWED" || code === "email_not_verified" || code === "EMAIL_NOT_VERIFIED")
286
+ return GOOGLE_EMAIL_NOT_CONFIRMED;
287
+ return code;
288
+ }
289
+ var USERNAME_ERROR_CODES = {
290
+ "too-short": "USERNAME_TOO_SHORT",
291
+ "too-long": "USERNAME_TOO_LONG",
292
+ characters: "INVALID_USERNAME"
293
+ };
294
+ function usernameErrorCode(username) {
295
+ const problem = usernameProblem(username);
296
+ return problem ? USERNAME_ERROR_CODES[problem] : null;
297
+ }
298
+ var NAME_UNSAFE = /[\u0000-\u001f\u007f<>]/;
299
+ function displayNameProblem(name, max = 50) {
300
+ const trimmed = name.trim();
301
+ if (!trimmed)
302
+ return "empty";
303
+ if ([...trimmed].length > max)
304
+ return "too-long";
305
+ if (NAME_UNSAFE.test(trimmed))
306
+ return "characters";
307
+ return null;
308
+ }
309
+ function defineSignInReasons(reasons) {
310
+ return {
311
+ REASONS: reasons,
312
+ reasonOf(value) {
313
+ return reasons.find((reason) => reason === value);
314
+ }
315
+ };
316
+ }
317
+ var REDIRECT_UNSAFE = /[\u0000-\u001f\u007f\\]/;
318
+ function isSafeRedirectPath(path) {
319
+ if (path.length > 500 || !path.startsWith("/") || path.startsWith("//") || REDIRECT_UNSAFE.test(path))
320
+ return false;
321
+ let decoded;
322
+ try {
323
+ decoded = decodeURIComponent(path);
324
+ } catch {
325
+ return false;
326
+ }
327
+ if (REDIRECT_UNSAFE.test(decoded) || decoded.startsWith("//"))
328
+ return false;
329
+ const base = "https://this-site.invalid";
330
+ return new URL(path, base).origin === base;
331
+ }
332
+ function safeRedirect(value) {
333
+ return typeof value === "string" && isSafeRedirectPath(value) ? value : undefined;
334
+ }
335
+
336
+ // src/account/result.ts
337
+ async function attempt(action) {
338
+ try {
339
+ return await action();
340
+ } catch {
341
+ return { ok: false, code: "fallback" };
342
+ }
343
+ }
344
+
345
+ // src/account/ui.tsx
346
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
347
+ var plainAccountLink = ({ href, className, children }) => /* @__PURE__ */ jsx2("a", {
348
+ href,
349
+ className,
350
+ children
351
+ });
352
+ function Svg({ children }) {
353
+ return /* @__PURE__ */ jsx2("svg", {
354
+ viewBox: "0 0 24 24",
355
+ fill: "none",
356
+ stroke: "currentColor",
357
+ strokeWidth: 2,
358
+ strokeLinecap: "round",
359
+ strokeLinejoin: "round",
360
+ "aria-hidden": "true",
361
+ focusable: "false",
362
+ children
363
+ });
364
+ }
365
+ var KeyIcon = () => /* @__PURE__ */ jsxs(Svg, {
366
+ children: [
367
+ /* @__PURE__ */ jsx2("path", {
368
+ d: "M2.586 17.414A2 2 0 0 0 2 18.828V21a1 1 0 0 0 1 1h3a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h1a1 1 0 0 0 1-1v-1a1 1 0 0 1 1-1h.172a2 2 0 0 0 1.414-.586l.814-.814a6.5 6.5 0 1 0-4-4z"
369
+ }),
370
+ /* @__PURE__ */ jsx2("circle", {
371
+ cx: "16.5",
372
+ cy: "7.5",
373
+ r: ".5",
374
+ fill: "currentColor"
375
+ })
376
+ ]
377
+ });
378
+ var SignOutIcon = () => /* @__PURE__ */ jsxs(Svg, {
379
+ children: [
380
+ /* @__PURE__ */ jsx2("path", {
381
+ d: "m16 17 5-5-5-5"
382
+ }),
383
+ /* @__PURE__ */ jsx2("path", {
384
+ d: "M21 12H9"
385
+ }),
386
+ /* @__PURE__ */ jsx2("path", {
387
+ d: "M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"
388
+ })
389
+ ]
390
+ });
391
+ var TrashIcon = () => /* @__PURE__ */ jsxs(Svg, {
392
+ children: [
393
+ /* @__PURE__ */ jsx2("path", {
394
+ d: "M3 6h18"
395
+ }),
396
+ /* @__PURE__ */ jsx2("path", {
397
+ d: "M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"
398
+ }),
399
+ /* @__PURE__ */ jsx2("path", {
400
+ d: "M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"
401
+ }),
402
+ /* @__PURE__ */ jsx2("path", {
403
+ d: "M10 11v6"
404
+ }),
405
+ /* @__PURE__ */ jsx2("path", {
406
+ d: "M14 11v6"
407
+ })
408
+ ]
409
+ });
410
+ var InfoIcon = () => /* @__PURE__ */ jsxs(Svg, {
411
+ children: [
412
+ /* @__PURE__ */ jsx2("circle", {
413
+ cx: "12",
414
+ cy: "12",
415
+ r: "10"
416
+ }),
417
+ /* @__PURE__ */ jsx2("path", {
418
+ d: "M12 16v-4"
419
+ }),
420
+ /* @__PURE__ */ jsx2("path", {
421
+ d: "M12 8h.01"
422
+ })
423
+ ]
424
+ });
425
+ function GoogleMark() {
426
+ return /* @__PURE__ */ jsxs("svg", {
427
+ viewBox: "0 0 48 48",
428
+ "aria-hidden": "true",
429
+ focusable: "false",
430
+ children: [
431
+ /* @__PURE__ */ jsx2("path", {
432
+ fill: "#EA4335",
433
+ d: "M24 9.5c3.54 0 6.71 1.22 9.21 3.6l6.85-6.85C35.9 2.38 30.47 0 24 0 14.62 0 6.51 5.38 2.56 13.22l7.98 6.19C12.43 13.72 17.74 9.5 24 9.5z"
434
+ }),
435
+ /* @__PURE__ */ jsx2("path", {
436
+ fill: "#4285F4",
437
+ d: "M46.98 24.55c0-1.57-.15-3.09-.38-4.55H24v9.02h12.94c-.58 2.96-2.26 5.48-4.78 7.18l7.73 6c4.51-4.18 7.09-10.36 7.09-17.65z"
438
+ }),
439
+ /* @__PURE__ */ jsx2("path", {
440
+ fill: "#FBBC05",
441
+ d: "M10.53 28.59c-.48-1.45-.76-2.99-.76-4.59s.27-3.14.76-4.59l-7.98-6.19C.92 16.46 0 20.12 0 24c0 3.88.92 7.54 2.56 10.78l7.97-6.19z"
442
+ }),
443
+ /* @__PURE__ */ jsx2("path", {
444
+ fill: "#34A853",
445
+ d: "M24 48c6.48 0 11.93-2.13 15.89-5.81l-7.73-6c-2.15 1.45-4.92 2.3-8.16 2.3-6.26 0-11.57-4.22-13.47-9.91l-7.98 6.19C6.51 42.62 14.62 48 24 48z"
446
+ })
447
+ ]
448
+ });
449
+ }
450
+ function AuthCard({
451
+ title,
452
+ intro,
453
+ footer,
454
+ children
455
+ }) {
456
+ return /* @__PURE__ */ jsxs("div", {
457
+ className: "jac-auth",
458
+ children: [
459
+ /* @__PURE__ */ jsxs("div", {
460
+ className: "jac-card",
461
+ children: [
462
+ /* @__PURE__ */ jsx2("h1", {
463
+ className: "jac-title",
464
+ children: title
465
+ }),
466
+ intro && /* @__PURE__ */ jsx2("p", {
467
+ className: "jac-intro",
468
+ children: intro
469
+ }),
470
+ /* @__PURE__ */ jsx2("div", {
471
+ className: "jac-card-body",
472
+ children
473
+ })
474
+ ]
475
+ }),
476
+ footer && /* @__PURE__ */ jsx2("p", {
477
+ className: "jac-footer",
478
+ children: footer
479
+ })
480
+ ]
481
+ });
482
+ }
483
+ function AuthField({ label, hint, error, id, ...input }) {
484
+ const auto = useId();
485
+ const fieldId = id ?? `jac-${auto}`;
486
+ const describedBy = [error ? `${fieldId}-error` : "", hint ? `${fieldId}-hint` : ""].filter(Boolean).join(" ");
487
+ return /* @__PURE__ */ jsxs("div", {
488
+ className: "jac-field",
489
+ children: [
490
+ /* @__PURE__ */ jsx2("label", {
491
+ htmlFor: fieldId,
492
+ className: "jac-label",
493
+ children: label
494
+ }),
495
+ /* @__PURE__ */ jsx2("input", {
496
+ id: fieldId,
497
+ className: "jac-input",
498
+ "aria-describedby": describedBy || undefined,
499
+ "aria-invalid": error ? true : undefined,
500
+ ...input
501
+ }),
502
+ error && /* @__PURE__ */ jsx2("p", {
503
+ id: `${fieldId}-error`,
504
+ role: "alert",
505
+ className: "jac-field-error",
506
+ children: error
507
+ }),
508
+ hint && /* @__PURE__ */ jsx2("p", {
509
+ id: `${fieldId}-hint`,
510
+ className: "jac-hint",
511
+ children: hint
512
+ })
513
+ ]
514
+ });
515
+ }
516
+ function FormMessage({ kind, children }) {
517
+ return /* @__PURE__ */ jsx2("p", {
518
+ role: kind === "error" ? "alert" : "status",
519
+ className: "jac-message",
520
+ "data-kind": kind,
521
+ children
522
+ });
523
+ }
524
+ function Notice({ children, action }) {
525
+ return /* @__PURE__ */ jsxs("div", {
526
+ role: "status",
527
+ className: "jac-notice",
528
+ children: [
529
+ /* @__PURE__ */ jsx2(InfoIcon, {}),
530
+ /* @__PURE__ */ jsxs("div", {
531
+ children: [
532
+ /* @__PURE__ */ jsx2("p", {
533
+ children
534
+ }),
535
+ action
536
+ ]
537
+ })
538
+ ]
539
+ });
540
+ }
541
+ function AccountButton({ variant = "primary", block = false, icon, type = "button", children, ...rest }) {
542
+ return /* @__PURE__ */ jsxs("button", {
543
+ type,
544
+ className: "jac-button",
545
+ "data-variant": variant === "primary" ? undefined : variant,
546
+ "data-block": block ? "" : undefined,
547
+ ...rest,
548
+ children: [
549
+ icon,
550
+ children
551
+ ]
552
+ });
553
+ }
554
+ function GoogleButton({ label, onClick, disabled }) {
555
+ return /* @__PURE__ */ jsxs("button", {
556
+ type: "button",
557
+ className: "jac-google",
558
+ onClick,
559
+ disabled,
560
+ children: [
561
+ /* @__PURE__ */ jsx2(GoogleMark, {}),
562
+ label
563
+ ]
564
+ });
565
+ }
566
+ function OrDivider({ label }) {
567
+ return /* @__PURE__ */ jsx2("p", {
568
+ className: "jac-or",
569
+ children: label
570
+ });
571
+ }
572
+ function AccountAvatar({
573
+ user,
574
+ size = 56
575
+ }) {
576
+ const src = avatarSrc(user.image, size);
577
+ return /* @__PURE__ */ jsx2("span", {
578
+ className: "jac-avatar",
579
+ style: { width: size, height: size, fontSize: Math.round(size * 0.36) },
580
+ "aria-hidden": "true",
581
+ children: src ? /* @__PURE__ */ jsx2("img", {
582
+ src,
583
+ alt: "",
584
+ width: size,
585
+ height: size,
586
+ referrerPolicy: "no-referrer"
587
+ }) : avatarInitials(user)
588
+ });
589
+ }
590
+ // src/account/SignIn.tsx
591
+ import { useState } from "react";
592
+ import { jsx as jsx3, jsxs as jsxs2, Fragment } from "react/jsx-runtime";
593
+ function usePanel(text, initialError) {
594
+ const [error, setError] = useState(initialError ? errorText(text, initialError) : null);
595
+ const [busy, setBusy] = useState(false);
596
+ async function run(action) {
597
+ setBusy(true);
598
+ setError(null);
599
+ const result = await attempt(action);
600
+ if (result.ok)
601
+ return;
602
+ setError(errorText(text, result.code));
603
+ setBusy(false);
604
+ }
605
+ return { error, busy, run };
606
+ }
607
+ function SignInPanel({
608
+ text,
609
+ reason,
610
+ initialError,
611
+ google,
612
+ passkey,
613
+ onSignIn,
614
+ signUpHref,
615
+ renderLink = plainAccountLink
616
+ }) {
617
+ const { error, busy, run } = usePanel(text, initialError);
618
+ const t = text.signIn;
619
+ function submit(event) {
620
+ event.preventDefault();
621
+ const form = new FormData(event.currentTarget);
622
+ run(() => onSignIn({ login: String(form.get("login") ?? "").trim(), password: String(form.get("password") ?? "") }));
623
+ }
624
+ return /* @__PURE__ */ jsxs2(AuthCard, {
625
+ title: t.title,
626
+ footer: /* @__PURE__ */ jsxs2(Fragment, {
627
+ children: [
628
+ t.noAccount,
629
+ " ",
630
+ renderLink({ href: signUpHref, className: "jac-link", children: t.createOne })
631
+ ]
632
+ }),
633
+ children: [
634
+ reason && /* @__PURE__ */ jsx3(Notice, {
635
+ action: renderLink({ href: signUpHref, className: "jac-link", children: t.newHere }),
636
+ children: reason
637
+ }),
638
+ error && /* @__PURE__ */ jsx3(FormMessage, {
639
+ kind: "error",
640
+ children: error
641
+ }),
642
+ /* @__PURE__ */ jsxs2("div", {
643
+ className: "jac-stack-sm",
644
+ children: [
645
+ google && /* @__PURE__ */ jsx3(GoogleButton, {
646
+ label: t.google,
647
+ disabled: busy,
648
+ onClick: () => void run(google)
649
+ }),
650
+ passkey && /* @__PURE__ */ jsx3(AccountButton, {
651
+ variant: "quiet",
652
+ block: true,
653
+ icon: /* @__PURE__ */ jsx3(KeyIcon, {}),
654
+ disabled: busy,
655
+ onClick: () => void run(passkey),
656
+ children: t.passkey
657
+ })
658
+ ]
659
+ }),
660
+ (google || passkey) && /* @__PURE__ */ jsx3(OrDivider, {
661
+ label: t.or
662
+ }),
663
+ /* @__PURE__ */ jsxs2("form", {
664
+ onSubmit: submit,
665
+ noValidate: true,
666
+ children: [
667
+ /* @__PURE__ */ jsx3(AuthField, {
668
+ label: text.fields.login,
669
+ name: "login",
670
+ autoComplete: "username webauthn",
671
+ autoCapitalize: "none",
672
+ spellCheck: false,
673
+ required: true
674
+ }),
675
+ /* @__PURE__ */ jsx3(AuthField, {
676
+ label: text.fields.password,
677
+ name: "password",
678
+ type: "password",
679
+ autoComplete: "current-password",
680
+ maxLength: PASSWORD.max,
681
+ required: true
682
+ }),
683
+ /* @__PURE__ */ jsx3(AccountButton, {
684
+ type: "submit",
685
+ block: true,
686
+ disabled: busy,
687
+ children: t.submit
688
+ })
689
+ ]
690
+ }),
691
+ /* @__PURE__ */ jsx3("p", {
692
+ className: "jac-note",
693
+ children: t.forgot
694
+ })
695
+ ]
696
+ });
697
+ }
698
+ var DEFAULT_SIGN_UP_RULES = {
699
+ usernameMin: USERNAME.min,
700
+ usernameMax: USERNAME.max,
701
+ passwordMin: PASSWORD.min,
702
+ passwordMax: PASSWORD.max
703
+ };
704
+ function SignUpPanel({
705
+ text,
706
+ reason,
707
+ initialError,
708
+ google,
709
+ onSignUp,
710
+ signInHref,
711
+ rules = DEFAULT_SIGN_UP_RULES,
712
+ renderLink = plainAccountLink
713
+ }) {
714
+ const { error, busy, run } = usePanel(text, initialError);
715
+ const t = text.signUp;
716
+ function submit(event) {
717
+ event.preventDefault();
718
+ const form = new FormData(event.currentTarget);
719
+ run(() => onSignUp({
720
+ username: String(form.get("username") ?? "").trim().toLowerCase(),
721
+ email: String(form.get("email") ?? "").trim(),
722
+ password: String(form.get("password") ?? "")
723
+ }));
724
+ }
725
+ return /* @__PURE__ */ jsxs2(AuthCard, {
726
+ title: t.title,
727
+ intro: t.intro,
728
+ footer: /* @__PURE__ */ jsxs2(Fragment, {
729
+ children: [
730
+ t.haveAccount,
731
+ " ",
732
+ renderLink({ href: signInHref, className: "jac-link", children: t.signIn })
733
+ ]
734
+ }),
735
+ children: [
736
+ reason && /* @__PURE__ */ jsx3(Notice, {
737
+ children: reason
738
+ }),
739
+ error && /* @__PURE__ */ jsx3(FormMessage, {
740
+ kind: "error",
741
+ children: error
742
+ }),
743
+ google && /* @__PURE__ */ jsxs2(Fragment, {
744
+ children: [
745
+ /* @__PURE__ */ jsx3(GoogleButton, {
746
+ label: text.signIn.google,
747
+ disabled: busy,
748
+ onClick: () => void run(google)
749
+ }),
750
+ /* @__PURE__ */ jsx3(OrDivider, {
751
+ label: text.signIn.or
752
+ })
753
+ ]
754
+ }),
755
+ /* @__PURE__ */ jsxs2("form", {
756
+ onSubmit: submit,
757
+ noValidate: true,
758
+ children: [
759
+ /* @__PURE__ */ jsx3(AuthField, {
760
+ label: text.fields.username,
761
+ hint: text.fields.usernameHint(rules.usernameMin, rules.usernameMax),
762
+ name: "username",
763
+ autoComplete: "username",
764
+ autoCapitalize: "none",
765
+ spellCheck: false,
766
+ minLength: rules.usernameMin,
767
+ maxLength: rules.usernameMax,
768
+ pattern: "[A-Za-z0-9_.]+",
769
+ required: true
770
+ }),
771
+ /* @__PURE__ */ jsx3(AuthField, {
772
+ label: text.fields.email,
773
+ hint: text.fields.emailHint,
774
+ name: "email",
775
+ type: "email",
776
+ autoComplete: "email"
777
+ }),
778
+ /* @__PURE__ */ jsx3(AuthField, {
779
+ label: text.fields.password,
780
+ hint: text.fields.passwordHint(rules.passwordMin),
781
+ name: "password",
782
+ type: "password",
783
+ autoComplete: "new-password",
784
+ minLength: rules.passwordMin,
785
+ maxLength: rules.passwordMax,
786
+ required: true
787
+ }),
788
+ /* @__PURE__ */ jsx3(FormMessage, {
789
+ kind: "warning",
790
+ children: t.recovery
791
+ }),
792
+ /* @__PURE__ */ jsx3(AccountButton, {
793
+ type: "submit",
794
+ block: true,
795
+ disabled: busy,
796
+ children: t.submit
797
+ })
798
+ ]
799
+ })
800
+ ]
801
+ });
802
+ }
803
+ function SignedInPanel({ text }) {
804
+ return /* @__PURE__ */ jsx3(AuthCard, {
805
+ title: text.signedIn.title,
806
+ children: /* @__PURE__ */ jsx3("p", {
807
+ role: "status",
808
+ className: "jac-text",
809
+ children: text.signedIn.body
810
+ })
811
+ });
812
+ }
813
+ // src/account/AccountPage.tsx
814
+ import { useEffect, useId as useId2, useRef, useState as useState2 } from "react";
815
+ import { jsx as jsx4, jsxs as jsxs3, Fragment as Fragment2 } from "react/jsx-runtime";
816
+ function AccountSection({
817
+ title,
818
+ tone,
819
+ children,
820
+ sectionRef
821
+ }) {
822
+ const id = useId2();
823
+ return /* @__PURE__ */ jsxs3("section", {
824
+ ref: sectionRef,
825
+ "aria-labelledby": id,
826
+ className: "jac-section",
827
+ "data-tone": tone,
828
+ children: [
829
+ /* @__PURE__ */ jsx4("h2", {
830
+ id,
831
+ className: "jac-h2",
832
+ children: title
833
+ }),
834
+ children
835
+ ]
836
+ });
837
+ }
838
+ function AccountHeader({ text, onSignOut }) {
839
+ const [busy, setBusy] = useState2(false);
840
+ return /* @__PURE__ */ jsxs3("div", {
841
+ className: "jac-page-head",
842
+ children: [
843
+ /* @__PURE__ */ jsx4("h1", {
844
+ className: "jac-page-title",
845
+ children: text.page.title
846
+ }),
847
+ /* @__PURE__ */ jsx4(AccountButton, {
848
+ variant: "quiet",
849
+ icon: /* @__PURE__ */ jsx4(SignOutIcon, {}),
850
+ disabled: busy,
851
+ onClick: async () => {
852
+ setBusy(true);
853
+ try {
854
+ await onSignOut();
855
+ } finally {
856
+ setBusy(false);
857
+ }
858
+ },
859
+ children: text.page.signOut
860
+ })
861
+ ]
862
+ });
863
+ }
864
+ function ProfileRow({ text, field, value, onSave, lockedUntil, usernameChangeDays, nameMax }) {
865
+ const t = text.page;
866
+ const [editing, setEditing] = useState2(false);
867
+ const [error, setError] = useState2(null);
868
+ const [notice, setNotice] = useState2(null);
869
+ const [busy, setBusy] = useState2(false);
870
+ const label = field === "username" ? t.username : t.displayName;
871
+ const editLabel = field === "username" ? t.editUsername : t.editName;
872
+ async function save(typed) {
873
+ const next = field === "username" ? typed.trim().toLowerCase() : typed.trim();
874
+ const problem = field === "username" ? usernameErrorCode(next) : { empty: "NAME_EMPTY", "too-long": "NAME_TOO_LONG", characters: "NAME_CHARACTERS", none: null }[displayNameProblem(next, nameMax) ?? "none"];
875
+ if (problem)
876
+ return setError(errorText(text, problem));
877
+ if (next === value)
878
+ return setEditing(false);
879
+ setBusy(true);
880
+ setError(null);
881
+ const result = await attempt(() => onSave(next));
882
+ setBusy(false);
883
+ if (result.ok) {
884
+ setEditing(false);
885
+ setNotice(field === "username" ? t.usernameSaved(next) : t.nameSaved);
886
+ } else if (result.code === "USERNAME_CHANGED_RECENTLY" && "until" in result && result.until) {
887
+ setError(t.usernameWait(text.date(result.until)));
888
+ } else
889
+ setError(errorText(text, result.code));
890
+ }
891
+ if (editing)
892
+ return /* @__PURE__ */ jsxs3("form", {
893
+ noValidate: true,
894
+ className: "jac-row",
895
+ "aria-label": editLabel,
896
+ onSubmit: (event) => {
897
+ event.preventDefault();
898
+ save(String(new FormData(event.currentTarget).get(field) ?? ""));
899
+ },
900
+ children: [
901
+ /* @__PURE__ */ jsx4(AuthField, {
902
+ label,
903
+ hint: field === "username" ? t.usernameRule(usernameChangeDays) : t.displayNameHint,
904
+ error,
905
+ name: field,
906
+ defaultValue: value,
907
+ autoFocus: true,
908
+ required: true,
909
+ ...field === "username" ? { autoComplete: "username", autoCapitalize: "none", spellCheck: false, minLength: USERNAME.min, maxLength: USERNAME.max } : { autoComplete: "name", maxLength: nameMax }
910
+ }),
911
+ /* @__PURE__ */ jsxs3("div", {
912
+ className: "jac-actions",
913
+ children: [
914
+ /* @__PURE__ */ jsx4(AccountButton, {
915
+ type: "submit",
916
+ disabled: busy,
917
+ children: t.save
918
+ }),
919
+ /* @__PURE__ */ jsx4(AccountButton, {
920
+ variant: "quiet",
921
+ disabled: busy,
922
+ onClick: () => {
923
+ setEditing(false);
924
+ setError(null);
925
+ },
926
+ children: t.cancel
927
+ })
928
+ ]
929
+ })
930
+ ]
931
+ });
932
+ return /* @__PURE__ */ jsxs3("div", {
933
+ className: "jac-row",
934
+ children: [
935
+ notice && /* @__PURE__ */ jsx4(FormMessage, {
936
+ kind: "success",
937
+ children: notice
938
+ }),
939
+ /* @__PURE__ */ jsxs3("div", {
940
+ className: "jac-row-line",
941
+ children: [
942
+ /* @__PURE__ */ jsx4("span", {
943
+ className: "jac-row-label",
944
+ children: label
945
+ }),
946
+ /* @__PURE__ */ jsx4("span", {
947
+ className: "jac-row-value",
948
+ "data-strong": field === "username" ? "" : undefined,
949
+ children: field === "username" ? `@${value}` : value
950
+ }),
951
+ !lockedUntil && /* @__PURE__ */ jsx4("button", {
952
+ type: "button",
953
+ className: "jac-row-edit",
954
+ "aria-label": editLabel,
955
+ onClick: () => {
956
+ setEditing(true);
957
+ setError(null);
958
+ setNotice(null);
959
+ },
960
+ children: t.edit
961
+ })
962
+ ]
963
+ }),
964
+ lockedUntil && /* @__PURE__ */ jsx4("p", {
965
+ className: "jac-row-note",
966
+ children: t.usernameNextChange(text.date(lockedUntil))
967
+ })
968
+ ]
969
+ });
970
+ }
971
+ function ProfileSection({
972
+ text,
973
+ user,
974
+ onSaveName,
975
+ onSaveUsername,
976
+ usernameLockedUntil,
977
+ usernameChangeDays = 30,
978
+ nameMax = 50,
979
+ adminHref,
980
+ renderLink = plainAccountLink
981
+ }) {
982
+ const t = text.page;
983
+ const shown = user.name.trim() || user.username;
984
+ return /* @__PURE__ */ jsxs3(AccountSection, {
985
+ title: t.profile,
986
+ children: [
987
+ /* @__PURE__ */ jsxs3("div", {
988
+ className: "jac-person",
989
+ children: [
990
+ /* @__PURE__ */ jsx4(AccountAvatar, {
991
+ user
992
+ }),
993
+ /* @__PURE__ */ jsx4("p", {
994
+ className: "jac-person-name",
995
+ children: shown
996
+ })
997
+ ]
998
+ }),
999
+ /* @__PURE__ */ jsxs3("div", {
1000
+ className: "jac-rows",
1001
+ children: [
1002
+ /* @__PURE__ */ jsx4(ProfileRow, {
1003
+ text,
1004
+ field: "name",
1005
+ value: shown,
1006
+ onSave: onSaveName,
1007
+ usernameChangeDays,
1008
+ nameMax
1009
+ }),
1010
+ /* @__PURE__ */ jsx4(ProfileRow, {
1011
+ text,
1012
+ field: "username",
1013
+ value: user.username,
1014
+ onSave: onSaveUsername,
1015
+ lockedUntil: usernameLockedUntil,
1016
+ usernameChangeDays,
1017
+ nameMax
1018
+ }),
1019
+ /* @__PURE__ */ jsx4("div", {
1020
+ className: "jac-row",
1021
+ children: /* @__PURE__ */ jsxs3("div", {
1022
+ className: "jac-row-line",
1023
+ children: [
1024
+ /* @__PURE__ */ jsx4("span", {
1025
+ className: "jac-row-label",
1026
+ children: t.email
1027
+ }),
1028
+ /* @__PURE__ */ jsx4("span", {
1029
+ className: "jac-row-value",
1030
+ children: user.email ?? t.noEmail
1031
+ })
1032
+ ]
1033
+ })
1034
+ })
1035
+ ]
1036
+ }),
1037
+ adminHref && renderLink({ href: adminHref, className: "jac-link jac-admin-link", children: t.admin })
1038
+ ]
1039
+ });
1040
+ }
1041
+ function PasskeysSection({ text, passkeys, googleOnly, onAdd, onRemove }) {
1042
+ const t = text.page;
1043
+ const [message, setMessage] = useState2(null);
1044
+ const [busy, setBusy] = useState2(false);
1045
+ const label = (passkey, i) => passkey.name ?? t.passkeyName(passkeys.length - i);
1046
+ async function act(action, done) {
1047
+ setBusy(true);
1048
+ setMessage(null);
1049
+ const result = await attempt(action);
1050
+ setMessage(result.ok ? done ? { kind: "success", text: done } : null : { kind: "error", text: errorText(text, result.code) });
1051
+ setBusy(false);
1052
+ }
1053
+ return /* @__PURE__ */ jsxs3(AccountSection, {
1054
+ title: t.passkeys,
1055
+ children: [
1056
+ /* @__PURE__ */ jsx4("p", {
1057
+ className: "jac-text",
1058
+ children: googleOnly ? t.passkeysIntroGoogle : t.passkeysIntro
1059
+ }),
1060
+ message && /* @__PURE__ */ jsx4(FormMessage, {
1061
+ kind: message.kind,
1062
+ children: message.text
1063
+ }),
1064
+ passkeys.length === 0 ? /* @__PURE__ */ jsx4(FormMessage, {
1065
+ kind: "warning",
1066
+ children: googleOnly ? t.noPasskeyGoogle : t.noPasskey
1067
+ }) : /* @__PURE__ */ jsx4("ul", {
1068
+ className: "jac-list",
1069
+ children: passkeys.map((passkey, i) => /* @__PURE__ */ jsxs3("li", {
1070
+ children: [
1071
+ /* @__PURE__ */ jsx4(KeyIcon, {}),
1072
+ /* @__PURE__ */ jsxs3("div", {
1073
+ className: "jac-list-main",
1074
+ children: [
1075
+ /* @__PURE__ */ jsx4("p", {
1076
+ className: "jac-list-name",
1077
+ children: label(passkey, i)
1078
+ }),
1079
+ passkey.createdAt && /* @__PURE__ */ jsx4("p", {
1080
+ className: "jac-list-meta",
1081
+ children: t.added(text.date(passkey.createdAt))
1082
+ })
1083
+ ]
1084
+ }),
1085
+ /* @__PURE__ */ jsx4("button", {
1086
+ type: "button",
1087
+ className: "jac-icon-button",
1088
+ "aria-label": t.removePasskey(label(passkey, i)),
1089
+ title: t.removePasskey(label(passkey, i)),
1090
+ disabled: busy,
1091
+ onClick: () => void act(() => onRemove(passkey.id), null),
1092
+ children: /* @__PURE__ */ jsx4(TrashIcon, {})
1093
+ })
1094
+ ]
1095
+ }, passkey.id))
1096
+ }),
1097
+ /* @__PURE__ */ jsx4(AccountButton, {
1098
+ icon: /* @__PURE__ */ jsx4(KeyIcon, {}),
1099
+ disabled: busy,
1100
+ onClick: () => void act(() => onAdd(t.passkeyName(passkeys.length + 1)), t.passkeyAdded),
1101
+ children: t.addPasskey
1102
+ })
1103
+ ]
1104
+ });
1105
+ }
1106
+ function GoogleSection({ text, connected, justConnected = false, initialError, onConnect }) {
1107
+ const t = text.page;
1108
+ const [failed, setFailed] = useState2(initialError ? errorText(text, initialError) : null);
1109
+ const [busy, setBusy] = useState2(false);
1110
+ const section = useRef(null);
1111
+ const fresh = justConnected && connected;
1112
+ useEffect(() => {
1113
+ if (fresh || initialError)
1114
+ section.current?.scrollIntoView({ block: "center" });
1115
+ }, [fresh, initialError]);
1116
+ return /* @__PURE__ */ jsxs3(AccountSection, {
1117
+ title: t.google,
1118
+ sectionRef: section,
1119
+ children: [
1120
+ fresh && /* @__PURE__ */ jsx4(FormMessage, {
1121
+ kind: "success",
1122
+ children: t.googleJustConnected
1123
+ }),
1124
+ failed && /* @__PURE__ */ jsx4(FormMessage, {
1125
+ kind: "error",
1126
+ children: failed
1127
+ }),
1128
+ connected ? !fresh && /* @__PURE__ */ jsx4("p", {
1129
+ className: "jac-text",
1130
+ children: t.googleConnected
1131
+ }) : /* @__PURE__ */ jsxs3(Fragment2, {
1132
+ children: [
1133
+ /* @__PURE__ */ jsx4("p", {
1134
+ className: "jac-text",
1135
+ children: t.googleIntro
1136
+ }),
1137
+ /* @__PURE__ */ jsx4("div", {
1138
+ className: "jac-narrow",
1139
+ children: /* @__PURE__ */ jsx4(GoogleButton, {
1140
+ label: t.connectGoogle,
1141
+ disabled: busy,
1142
+ onClick: async () => {
1143
+ setBusy(true);
1144
+ setFailed(null);
1145
+ const result = await attempt(onConnect);
1146
+ if (result.ok)
1147
+ return;
1148
+ setFailed(errorText(text, result.code));
1149
+ setBusy(false);
1150
+ }
1151
+ })
1152
+ })
1153
+ ]
1154
+ })
1155
+ ]
1156
+ });
1157
+ }
1158
+ function PasswordSection({
1159
+ text,
1160
+ hasPassword,
1161
+ onChange,
1162
+ onSet,
1163
+ passwordMin = PASSWORD.min,
1164
+ passwordMax = PASSWORD.max
1165
+ }) {
1166
+ const t = text.page;
1167
+ const [message, setMessage] = useState2(null);
1168
+ const [busy, setBusy] = useState2(false);
1169
+ const newPassword = /* @__PURE__ */ jsx4(AuthField, {
1170
+ label: text.fields.newPassword,
1171
+ hint: text.fields.passwordHint(passwordMin),
1172
+ name: "newPassword",
1173
+ type: "password",
1174
+ autoComplete: "new-password",
1175
+ minLength: passwordMin,
1176
+ maxLength: passwordMax,
1177
+ required: true
1178
+ });
1179
+ async function submit(event) {
1180
+ event.preventDefault();
1181
+ const formEl = event.currentTarget;
1182
+ const form = new FormData(formEl);
1183
+ const next = String(form.get("newPassword") ?? "");
1184
+ setBusy(true);
1185
+ setMessage(null);
1186
+ const result = await attempt(() => hasPassword ? onChange({ currentPassword: String(form.get("currentPassword") ?? ""), newPassword: next }) : onSet({ newPassword: next }));
1187
+ if (result.ok)
1188
+ formEl.reset();
1189
+ setMessage(result.ok ? { kind: "success", text: hasPassword ? t.passwordDone : t.setPasswordDone } : { kind: "error", text: errorText(text, result.code) });
1190
+ setBusy(false);
1191
+ }
1192
+ return /* @__PURE__ */ jsxs3(AccountSection, {
1193
+ title: hasPassword ? t.password : t.setPassword,
1194
+ children: [
1195
+ !hasPassword && /* @__PURE__ */ jsx4("p", {
1196
+ className: "jac-text",
1197
+ children: t.setPasswordIntro
1198
+ }),
1199
+ message && /* @__PURE__ */ jsx4(FormMessage, {
1200
+ kind: message.kind,
1201
+ children: message.text
1202
+ }),
1203
+ /* @__PURE__ */ jsxs3("form", {
1204
+ noValidate: true,
1205
+ onSubmit: submit,
1206
+ children: [
1207
+ hasPassword && /* @__PURE__ */ jsx4(AuthField, {
1208
+ label: text.fields.currentPassword,
1209
+ name: "currentPassword",
1210
+ type: "password",
1211
+ autoComplete: "current-password",
1212
+ required: true
1213
+ }),
1214
+ newPassword,
1215
+ /* @__PURE__ */ jsx4(AccountButton, {
1216
+ type: "submit",
1217
+ variant: "quiet",
1218
+ disabled: busy,
1219
+ children: hasPassword ? t.passwordSubmit : t.setPassword
1220
+ })
1221
+ ]
1222
+ })
1223
+ ]
1224
+ });
1225
+ }
1226
+ function DangerSection({ text, confirm, onDelete }) {
1227
+ const t = text.page;
1228
+ const [error, setError] = useState2(null);
1229
+ const [busy, setBusy] = useState2(false);
1230
+ const [asking, setAsking] = useState2(false);
1231
+ async function run(action) {
1232
+ setBusy(true);
1233
+ setError(null);
1234
+ const result = await attempt(action);
1235
+ if (result.ok)
1236
+ return;
1237
+ setError(errorText(text, result.code));
1238
+ setBusy(false);
1239
+ }
1240
+ const deleteButton = (props) => /* @__PURE__ */ jsx4(AccountButton, {
1241
+ variant: "danger",
1242
+ icon: /* @__PURE__ */ jsx4(TrashIcon, {}),
1243
+ disabled: busy,
1244
+ type: props.type ?? "button",
1245
+ onClick: props.onClick,
1246
+ children: props.label ?? t.dangerSubmit
1247
+ });
1248
+ return /* @__PURE__ */ jsxs3(AccountSection, {
1249
+ title: t.danger,
1250
+ tone: "danger",
1251
+ children: [
1252
+ /* @__PURE__ */ jsx4("p", {
1253
+ className: "jac-text",
1254
+ children: t.dangerIntro
1255
+ }),
1256
+ error && /* @__PURE__ */ jsx4(FormMessage, {
1257
+ kind: "error",
1258
+ children: error
1259
+ }),
1260
+ confirm.mode === "password" ? /* @__PURE__ */ jsxs3("form", {
1261
+ noValidate: true,
1262
+ onSubmit: (event) => {
1263
+ event.preventDefault();
1264
+ const password = String(new FormData(event.currentTarget).get("deletePassword") ?? "");
1265
+ if (!password)
1266
+ return setError(errorText(text, "INVALID_PASSWORD"));
1267
+ run(() => onDelete({ password }));
1268
+ },
1269
+ children: [
1270
+ /* @__PURE__ */ jsx4(AuthField, {
1271
+ label: t.dangerConfirm,
1272
+ name: "deletePassword",
1273
+ type: "password",
1274
+ autoComplete: "current-password",
1275
+ required: true
1276
+ }),
1277
+ deleteButton({ type: "submit" })
1278
+ ]
1279
+ }) : !confirm.fresh ? /* @__PURE__ */ jsxs3(Fragment2, {
1280
+ children: [
1281
+ /* @__PURE__ */ jsx4("p", {
1282
+ className: "jac-text",
1283
+ children: t.dangerReauth(confirm.minutes ?? 5)
1284
+ }),
1285
+ /* @__PURE__ */ jsx4("div", {
1286
+ className: "jac-narrow",
1287
+ children: /* @__PURE__ */ jsx4(GoogleButton, {
1288
+ label: t.reauthGoogle,
1289
+ disabled: busy,
1290
+ onClick: () => void run(confirm.onReauth)
1291
+ })
1292
+ })
1293
+ ]
1294
+ }) : /* @__PURE__ */ jsxs3(Fragment2, {
1295
+ children: [
1296
+ /* @__PURE__ */ jsx4("p", {
1297
+ className: "jac-text",
1298
+ children: t.dangerReady(confirm.minutes ?? 5)
1299
+ }),
1300
+ asking ? /* @__PURE__ */ jsxs3("div", {
1301
+ className: "jac-actions",
1302
+ children: [
1303
+ /* @__PURE__ */ jsx4(AccountButton, {
1304
+ variant: "quiet",
1305
+ disabled: busy,
1306
+ autoFocus: true,
1307
+ onClick: () => setAsking(false),
1308
+ children: t.cancel
1309
+ }),
1310
+ deleteButton({ label: t.dangerAreYouSure, onClick: () => void run(() => onDelete({})) })
1311
+ ]
1312
+ }) : deleteButton({ onClick: () => setAsking(true) })
1313
+ ]
1314
+ })
1315
+ ]
1316
+ });
1317
+ }
1318
+ function AccountPage({
1319
+ text,
1320
+ user,
1321
+ onSignOut,
1322
+ welcome = false,
1323
+ profile,
1324
+ passkeys,
1325
+ google,
1326
+ password,
1327
+ danger,
1328
+ afterProfile,
1329
+ renderLink
1330
+ }) {
1331
+ return /* @__PURE__ */ jsxs3("div", {
1332
+ className: "jac-page",
1333
+ children: [
1334
+ /* @__PURE__ */ jsx4(AccountHeader, {
1335
+ text,
1336
+ onSignOut
1337
+ }),
1338
+ welcome && passkeys.passkeys.length === 0 && /* @__PURE__ */ jsx4(FormMessage, {
1339
+ kind: "success",
1340
+ children: text.page.welcome
1341
+ }),
1342
+ /* @__PURE__ */ jsx4(ProfileSection, {
1343
+ text,
1344
+ user,
1345
+ renderLink,
1346
+ ...profile
1347
+ }),
1348
+ afterProfile,
1349
+ /* @__PURE__ */ jsx4(PasskeysSection, {
1350
+ text,
1351
+ googleOnly: !password.hasPassword,
1352
+ ...passkeys
1353
+ }),
1354
+ google && /* @__PURE__ */ jsx4(GoogleSection, {
1355
+ text,
1356
+ ...google
1357
+ }),
1358
+ /* @__PURE__ */ jsx4(PasswordSection, {
1359
+ text,
1360
+ ...password
1361
+ }),
1362
+ /* @__PURE__ */ jsx4(DangerSection, {
1363
+ text,
1364
+ ...danger
1365
+ })
1366
+ ]
1367
+ });
1368
+ }
1369
+ export {
1370
+ AccountAvatar,
1371
+ AccountButton,
1372
+ AccountHeader,
1373
+ AccountPage,
1374
+ AccountRoot,
1375
+ AccountSection,
1376
+ AuthCard,
1377
+ AuthField,
1378
+ DEFAULT_SIGN_UP_RULES,
1379
+ DangerSection,
1380
+ FormMessage,
1381
+ GoogleButton,
1382
+ GoogleMark,
1383
+ GoogleSection,
1384
+ Notice,
1385
+ OrDivider,
1386
+ PasskeysSection,
1387
+ PasswordSection,
1388
+ ProfileSection,
1389
+ SignInPanel,
1390
+ SignUpPanel,
1391
+ SignedInPanel,
1392
+ accountTextEn,
1393
+ accountTheme,
1394
+ errorText,
1395
+ plainAccountLink
1396
+ };