@jgb-solutions/kit 0.7.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,1012 @@
1
+ // src/account/native/index.tsx
2
+ import { useState } from "react";
3
+ import { ActivityIndicator, Image, Pressable, Text, TextInput, View } from "react-native";
4
+ import Svg, { Path } from "react-native-svg";
5
+
6
+ // src/auth/rules.ts
7
+ var USERNAME = { min: 3, max: 30, pattern: /^[a-z0-9_.]+$/ };
8
+ var PASSWORD = { min: 12, max: 128 };
9
+ function usernameProblem(username) {
10
+ if (username.length < USERNAME.min)
11
+ return "too-short";
12
+ if (username.length > USERNAME.max)
13
+ return "too-long";
14
+ if (!USERNAME.pattern.test(username))
15
+ return "characters";
16
+ return null;
17
+ }
18
+ var PLACEHOLDER_EMAIL_DOMAIN = "users.invalid";
19
+ function isPlaceholderEmail(email) {
20
+ return email.toLowerCase().endsWith(`@${PLACEHOLDER_EMAIL_DOMAIN}`);
21
+ }
22
+ function placeholderEmail(randomId = () => globalThis.crypto.randomUUID()) {
23
+ return `${randomId().replaceAll("-", "")}@${PLACEHOLDER_EMAIL_DOMAIN}`;
24
+ }
25
+ function usernameFromName(name, email, fallback) {
26
+ 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, "");
27
+ for (const source of [name ?? "", (email ?? "").split("@")[0] ?? ""]) {
28
+ const base = clean(source);
29
+ if (base.length >= USERNAME.min)
30
+ return base;
31
+ }
32
+ return clean(fallback) || "member";
33
+ }
34
+ function usernameCandidates(base, random = Math.random) {
35
+ const numbered = Array.from({ length: 8 }, () => `${base}${Math.floor(1000 + random() * 9000)}`);
36
+ return [base, ...numbered];
37
+ }
38
+ function defineRoles(config) {
39
+ const known = config.roles;
40
+ const panel = config.adminPanel;
41
+ const isRole = (value) => typeof value === "string" && known.includes(value);
42
+ const split = (stored) => (stored ?? "").split(",").map((role) => role.trim());
43
+ return {
44
+ ROLES: config.roles,
45
+ DEFAULT_ROLE: config.defaultRole,
46
+ ADMIN_PANEL_ROLES: config.adminPanel,
47
+ isRole,
48
+ roleOf(stored) {
49
+ const roles = split(stored).filter(isRole);
50
+ return roles.find((role) => panel.includes(role)) ?? roles[0] ?? config.defaultRole;
51
+ },
52
+ canOpenAdmin(stored) {
53
+ return split(stored).some((role) => panel.includes(role));
54
+ }
55
+ };
56
+ }
57
+ function avatarSrc(image, px) {
58
+ if (!image)
59
+ return "";
60
+ try {
61
+ const url = new URL(image);
62
+ if (url.protocol !== "https:")
63
+ return "";
64
+ if (url.hostname.endsWith(".googleusercontent.com"))
65
+ return `${url.origin}${url.pathname.replace(/=[^/]*$/, "")}=s${px * 2}-c`;
66
+ return url.href;
67
+ } catch {
68
+ return "";
69
+ }
70
+ }
71
+ function avatarInitials(user) {
72
+ const words = (user.name.trim() || user.username).replace(/[_.-]+/g, " ").split(/\s+/).filter(Boolean);
73
+ const letters = words.length > 1 ? [words[0], words[words.length - 1]] : [words[0]];
74
+ return letters.map((word) => [...word ?? ""][0] ?? "").join("").toUpperCase();
75
+ }
76
+ var ACCOUNT_NOT_LINKED = "ACCOUNT_NOT_LINKED";
77
+ var GOOGLE_ALREADY_LINKED = "GOOGLE_ALREADY_LINKED";
78
+ var GOOGLE_EMAIL_NOT_CONFIRMED = "GOOGLE_EMAIL_NOT_CONFIRMED";
79
+ function authErrorCode(answer) {
80
+ const code = typeof answer === "string" ? answer : answer && typeof answer === "object" && ("code" in answer) ? String(answer.code) : "";
81
+ const message = answer && typeof answer === "object" && "message" in answer ? String(answer.message) : "";
82
+ if (code === "account_not_linked" || code === "OAUTH_LINK_ERROR" && message === "account not linked")
83
+ return ACCOUNT_NOT_LINKED;
84
+ if (code === "SOCIAL_ACCOUNT_ALREADY_LINKED" || code === "account_already_linked_to_different_user")
85
+ return GOOGLE_ALREADY_LINKED;
86
+ if (code === "LINKING_NOT_ALLOWED" || code === "email_not_verified" || code === "EMAIL_NOT_VERIFIED")
87
+ return GOOGLE_EMAIL_NOT_CONFIRMED;
88
+ return code;
89
+ }
90
+ var USERNAME_ERROR_CODES = {
91
+ "too-short": "USERNAME_TOO_SHORT",
92
+ "too-long": "USERNAME_TOO_LONG",
93
+ characters: "INVALID_USERNAME"
94
+ };
95
+ function usernameErrorCode(username) {
96
+ const problem = usernameProblem(username);
97
+ return problem ? USERNAME_ERROR_CODES[problem] : null;
98
+ }
99
+ var NAME_UNSAFE = /[\u0000-\u001f\u007f<>]/;
100
+ function displayNameProblem(name, max = 50) {
101
+ const trimmed = name.trim();
102
+ if (!trimmed)
103
+ return "empty";
104
+ if ([...trimmed].length > max)
105
+ return "too-long";
106
+ if (NAME_UNSAFE.test(trimmed))
107
+ return "characters";
108
+ return null;
109
+ }
110
+ function defineSignInReasons(reasons) {
111
+ return {
112
+ REASONS: reasons,
113
+ reasonOf(value) {
114
+ return reasons.find((reason) => reason === value);
115
+ }
116
+ };
117
+ }
118
+ var REDIRECT_UNSAFE = /[\u0000-\u001f\u007f\\]/;
119
+ function isSafeRedirectPath(path) {
120
+ if (path.length > 500 || !path.startsWith("/") || path.startsWith("//") || REDIRECT_UNSAFE.test(path))
121
+ return false;
122
+ let decoded;
123
+ try {
124
+ decoded = decodeURIComponent(path);
125
+ } catch {
126
+ return false;
127
+ }
128
+ if (REDIRECT_UNSAFE.test(decoded) || decoded.startsWith("//"))
129
+ return false;
130
+ const base = "https://this-site.invalid";
131
+ return new URL(path, base).origin === base;
132
+ }
133
+ function safeRedirect(value) {
134
+ return typeof value === "string" && isSafeRedirectPath(value) ? value : undefined;
135
+ }
136
+
137
+ // src/account/result.ts
138
+ async function attempt(action) {
139
+ try {
140
+ return await action();
141
+ } catch {
142
+ return { ok: false, code: "fallback" };
143
+ }
144
+ }
145
+
146
+ // src/account/text.ts
147
+ var dateFormat = new Intl.DateTimeFormat("en", { dateStyle: "medium", timeZone: "UTC" });
148
+ var accountTextEn = {
149
+ signIn: {
150
+ title: "Sign in",
151
+ submit: "Sign in",
152
+ passkey: "Sign in with a passkey",
153
+ google: "Continue with Google",
154
+ or: "or",
155
+ noAccount: "No account yet?",
156
+ createOne: "Create one",
157
+ newHere: "New here? Create a free account",
158
+ forgot: "Forgot your password? Sign in with a passkey: it is the only way back in."
159
+ },
160
+ signUp: {
161
+ title: "Create an account",
162
+ intro: "Free, and optional: everything here works without an account.",
163
+ submit: "Create my account",
164
+ haveAccount: "Already have an account?",
165
+ signIn: "Sign in",
166
+ 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."
167
+ },
168
+ signedIn: { title: "Signed in", body: "Taking you back…" },
169
+ fields: {
170
+ login: "Username or email",
171
+ username: "Username",
172
+ usernameHint: (min, max) => `${min}–${max} characters: letters, digits, dots and underscores.`,
173
+ email: "Email (optional)",
174
+ emailHint: "Only to sign in with it. We never send you email.",
175
+ password: "Password",
176
+ currentPassword: "Current password",
177
+ newPassword: "New password",
178
+ passwordHint: (min) => `At least ${min} characters.`
179
+ },
180
+ page: {
181
+ title: "Your account",
182
+ signOut: "Sign out",
183
+ welcome: "Your account is ready. Add a passkey now: it’s the only way back in if you forget your password.",
184
+ profile: "Profile",
185
+ displayName: "Name",
186
+ displayNameHint: "Shown on your account. Change it any time.",
187
+ username: "Username",
188
+ usernameRule: (days) => `You can change your username once every ${days} days. Your old one stays reserved for you for ${days} days.`,
189
+ email: "Email",
190
+ noEmail: "None",
191
+ edit: "Change",
192
+ editName: "Change your name",
193
+ editUsername: "Change your username",
194
+ save: "Save",
195
+ cancel: "Cancel",
196
+ nameSaved: "Name saved.",
197
+ usernameSaved: (username) => `Your username is now @${username}. Use it to sign in from now on.`,
198
+ usernameNextChange: (date) => `You can change it again on ${date}.`,
199
+ usernameWait: (date) => `You changed your username recently. You can change it again on ${date}.`,
200
+ admin: "Open the admin panel",
201
+ passkeys: "Passkeys",
202
+ 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.",
203
+ passkeysIntroGoogle: "A passkey signs you in with your fingerprint, face or device PIN, without going through Google.",
204
+ noPasskey: "No passkey yet. Without one, a forgotten password means a lost account.",
205
+ noPasskeyGoogle: "No passkey yet. You sign in with Google; a passkey is a quicker way in on this device.",
206
+ addPasskey: "Add a passkey",
207
+ passkeyAdded: "Passkey added.",
208
+ passkeyName: (number) => `Passkey ${number}`,
209
+ removePasskey: (name) => `Remove ${name}`,
210
+ added: (date) => `Added ${date}`,
211
+ google: "Google",
212
+ googleConnected: "Google is connected: you can sign in with Google too.",
213
+ googleIntro: "Connect your Google account to sign in with Google as well.",
214
+ connectGoogle: "Connect Google",
215
+ googleJustConnected: "Google connected. You can now sign in with Google too.",
216
+ password: "Change password",
217
+ passwordSubmit: "Change password",
218
+ passwordDone: "Password changed. Your other devices are signed out.",
219
+ setPassword: "Set a password",
220
+ setPasswordIntro: "You sign in with Google. Add a password to also sign in with your username.",
221
+ setPasswordDone: "Password set. You can now also sign in with your username.",
222
+ danger: "Delete account",
223
+ dangerIntro: "This deletes your account and everything saved in it, at once. It can’t be undone.",
224
+ dangerConfirm: "Type your password to confirm",
225
+ dangerSubmit: "Delete my account",
226
+ dangerReauth: (minutes) => `To confirm it’s you, sign in with Google again. You then have ${minutes} minutes to delete your account.`,
227
+ reauthGoogle: "Sign in with Google again",
228
+ dangerReady: (minutes) => `You signed in again less than ${minutes} minutes ago, so you can delete your account now.`,
229
+ dangerAreYouSure: "Yes, delete it"
230
+ },
231
+ errors: {
232
+ INVALID_USERNAME_OR_PASSWORD: "Wrong username or password.",
233
+ INVALID_EMAIL_OR_PASSWORD: "Wrong username, email or password.",
234
+ INVALID_PASSWORD: "That password is wrong.",
235
+ USERNAME_IS_ALREADY_TAKEN: "That username is taken. Try another one.",
236
+ USER_ALREADY_EXISTS: "An account already uses this email.",
237
+ USERNAME_TOO_SHORT: "That username is too short.",
238
+ USERNAME_TOO_LONG: "That username is too long.",
239
+ INVALID_USERNAME: "Use letters, digits, dots and underscores only.",
240
+ PASSWORD_TOO_SHORT: "That password is too short.",
241
+ PASSWORD_TOO_LONG: "That password is too long.",
242
+ BANNED_USER: "This account is suspended.",
243
+ TOO_MANY_REQUESTS: "Too many tries. Wait a minute and try again.",
244
+ REAUTH_REQUIRED: "For your safety, sign in with Google again, then delete your account.",
245
+ UNAUTHORIZED: "You were signed out. Sign in again.",
246
+ passkeyCancelled: "The passkey wasn’t used. Try again when you’re ready.",
247
+ passkeyUnsupported: "This browser can’t use passkeys.",
248
+ ACCOUNT_NOT_LINKED: "An account already uses this address. Sign in with your username and password first, then connect Google from your account page.",
249
+ GOOGLE_ALREADY_LINKED: "That Google account is already connected to another account.",
250
+ GOOGLE_EMAIL_NOT_CONFIRMED: "Google didn’t confirm this address, so it can’t be used here.",
251
+ signup_disabled: "New accounts are closed for now. If you already have one, sign in with your username.",
252
+ google: "Google sign-in didn’t work. Try again, or sign in with your username.",
253
+ SAME_USERNAME: "That’s already your username.",
254
+ USERNAME_CHANGED_RECENTLY: "You changed your username recently. Try again later.",
255
+ NAME_EMPTY: "Enter a name.",
256
+ NAME_TOO_LONG: "That name is too long.",
257
+ NAME_CHARACTERS: "Use letters, digits and punctuation only.",
258
+ fallback: "Something went wrong. Try again."
259
+ },
260
+ date: (iso) => {
261
+ const date = new Date(iso);
262
+ return Number.isNaN(date.getTime()) ? "" : dateFormat.format(date);
263
+ }
264
+ };
265
+ function errorText(text, code) {
266
+ return (code !== undefined && Object.hasOwn(text.errors, code) ? text.errors[code] : undefined) ?? text.errors.fallback;
267
+ }
268
+
269
+ // src/account/native/index.tsx
270
+ import { jsx, jsxs, Fragment } from "react/jsx-runtime";
271
+ var accountNativeDark = {
272
+ colors: {
273
+ page: "#0b0b0e",
274
+ surface: "#15151a",
275
+ raised: "#212127",
276
+ border: "rgba(238, 238, 241, 0.16)",
277
+ text: "#eeeef1",
278
+ muted: "#aaaab2",
279
+ brand: "#2f54c8",
280
+ brandInk: "#ffffff",
281
+ accent: "#9db0f2",
282
+ bad: "#ff7a6b",
283
+ badInk: "#17171c"
284
+ },
285
+ radius: { control: 12, button: 999 }
286
+ };
287
+ var accountNativeLight = {
288
+ colors: {
289
+ page: "#f4f4f6",
290
+ surface: "#ffffff",
291
+ raised: "#ececf0",
292
+ border: "#a9a9b4",
293
+ text: "#17171c",
294
+ muted: "#5b5b66",
295
+ brand: "#2f54c8",
296
+ brandInk: "#ffffff",
297
+ accent: "#2a4bb3",
298
+ bad: "#b42318",
299
+ badInk: "#ffffff"
300
+ },
301
+ radius: { control: 12, button: 999 }
302
+ };
303
+ var font = (theme, kind) => {
304
+ const family = theme.fonts?.[kind];
305
+ return family ? { fontFamily: family } : null;
306
+ };
307
+ function NativeField({
308
+ theme,
309
+ label,
310
+ hint,
311
+ value,
312
+ onChangeText,
313
+ secure,
314
+ autoComplete,
315
+ keyboardType = "default",
316
+ capitalize = "none",
317
+ maxLength
318
+ }) {
319
+ const c = theme.colors;
320
+ return /* @__PURE__ */ jsxs(View, {
321
+ style: { gap: 6 },
322
+ children: [
323
+ /* @__PURE__ */ jsx(Text, {
324
+ style: [{ color: c.text, fontSize: 15, fontWeight: "600" }, font(theme, "body")],
325
+ children: label
326
+ }),
327
+ /* @__PURE__ */ jsx(TextInput, {
328
+ accessibilityLabel: label,
329
+ accessibilityHint: hint,
330
+ value,
331
+ onChangeText,
332
+ secureTextEntry: secure,
333
+ autoComplete,
334
+ keyboardType,
335
+ autoCapitalize: capitalize,
336
+ autoCorrect: false,
337
+ maxLength,
338
+ placeholderTextColor: c.muted,
339
+ style: [
340
+ {
341
+ minHeight: 48,
342
+ borderRadius: theme.radius.control,
343
+ borderWidth: 1,
344
+ borderColor: c.border,
345
+ backgroundColor: c.surface,
346
+ color: c.text,
347
+ paddingHorizontal: 14,
348
+ fontSize: 16
349
+ },
350
+ font(theme, "body")
351
+ ]
352
+ }),
353
+ hint ? /* @__PURE__ */ jsx(Text, {
354
+ style: [{ color: c.muted, fontSize: 13 }, font(theme, "body")],
355
+ children: hint
356
+ }) : null
357
+ ]
358
+ });
359
+ }
360
+ function NativeButton({
361
+ theme,
362
+ label,
363
+ onPress,
364
+ variant = "quiet",
365
+ busy = false,
366
+ disabled = false,
367
+ accessibilityLabel
368
+ }) {
369
+ const c = theme.colors;
370
+ const [background, ink, border] = variant === "primary" ? [c.brand, c.brandInk, c.brand] : variant === "danger" ? [c.bad, c.badInk, c.bad] : [c.raised, c.text, c.border];
371
+ return /* @__PURE__ */ jsx(Pressable, {
372
+ accessibilityRole: "button",
373
+ accessibilityLabel: accessibilityLabel ?? label,
374
+ accessibilityState: { disabled: disabled || busy, busy },
375
+ disabled: disabled || busy,
376
+ onPress,
377
+ style: ({ pressed }) => ({
378
+ minHeight: 44,
379
+ borderRadius: theme.radius.button,
380
+ borderWidth: 1,
381
+ borderColor: border,
382
+ backgroundColor: background,
383
+ paddingHorizontal: 18,
384
+ alignItems: "center",
385
+ justifyContent: "center",
386
+ opacity: disabled ? 0.6 : pressed ? 0.8 : 1
387
+ }),
388
+ children: busy ? /* @__PURE__ */ jsx(ActivityIndicator, {
389
+ color: ink
390
+ }) : /* @__PURE__ */ jsx(Text, {
391
+ style: [{ color: ink, fontSize: 16, fontWeight: "700", textAlign: "center" }, font(theme, "body")],
392
+ children: label
393
+ })
394
+ });
395
+ }
396
+ function NativeGoogleButton({ label, onPress, busy = false }) {
397
+ return /* @__PURE__ */ jsxs(Pressable, {
398
+ accessibilityRole: "button",
399
+ accessibilityLabel: label,
400
+ accessibilityState: { disabled: busy, busy },
401
+ disabled: busy,
402
+ onPress,
403
+ style: ({ pressed }) => ({
404
+ minHeight: 44,
405
+ borderRadius: 999,
406
+ borderWidth: 1,
407
+ borderColor: "#dadce0",
408
+ backgroundColor: pressed ? "#f8f9fa" : "#ffffff",
409
+ flexDirection: "row",
410
+ gap: 12,
411
+ alignItems: "center",
412
+ justifyContent: "center",
413
+ paddingHorizontal: 16,
414
+ opacity: busy ? 0.6 : 1
415
+ }),
416
+ children: [
417
+ /* @__PURE__ */ jsxs(Svg, {
418
+ width: 18,
419
+ height: 18,
420
+ viewBox: "0 0 48 48",
421
+ accessibilityElementsHidden: true,
422
+ importantForAccessibility: "no-hide-descendants",
423
+ children: [
424
+ /* @__PURE__ */ jsx(Path, {
425
+ fill: "#EA4335",
426
+ 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"
427
+ }),
428
+ /* @__PURE__ */ jsx(Path, {
429
+ fill: "#4285F4",
430
+ 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"
431
+ }),
432
+ /* @__PURE__ */ jsx(Path, {
433
+ fill: "#FBBC05",
434
+ 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"
435
+ }),
436
+ /* @__PURE__ */ jsx(Path, {
437
+ fill: "#34A853",
438
+ 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"
439
+ })
440
+ ]
441
+ }),
442
+ /* @__PURE__ */ jsx(Text, {
443
+ style: { color: "#1f1f1f", fontSize: 15, fontWeight: "600" },
444
+ children: label
445
+ })
446
+ ]
447
+ });
448
+ }
449
+ function NativeError({ theme, message }) {
450
+ if (!message)
451
+ return null;
452
+ return /* @__PURE__ */ jsx(Text, {
453
+ accessibilityRole: "alert",
454
+ accessibilityLiveRegion: "assertive",
455
+ style: [{ color: theme.colors.bad, fontSize: 15, lineHeight: 21 }, font(theme, "body")],
456
+ children: message
457
+ });
458
+ }
459
+ function NativeNotice({ theme, children }) {
460
+ const c = theme.colors;
461
+ return /* @__PURE__ */ jsx(View, {
462
+ accessibilityLiveRegion: "polite",
463
+ style: { padding: 14, borderRadius: 14, borderCurve: "continuous", borderWidth: 1, borderColor: c.border, backgroundColor: c.raised },
464
+ children: /* @__PURE__ */ jsx(Text, {
465
+ style: [{ color: c.text, fontSize: 15, lineHeight: 21 }, font(theme, "body")],
466
+ children
467
+ })
468
+ });
469
+ }
470
+ function Heading({ theme, children, size = 24 }) {
471
+ return /* @__PURE__ */ jsx(Text, {
472
+ accessibilityRole: "header",
473
+ style: [{ color: theme.colors.text, fontSize: size, fontWeight: size > 20 ? "800" : "700" }, font(theme, "display")],
474
+ children
475
+ });
476
+ }
477
+ function Muted({ theme, children }) {
478
+ return /* @__PURE__ */ jsx(Text, {
479
+ style: [{ color: theme.colors.muted, fontSize: 14, lineHeight: 20 }, font(theme, "body")],
480
+ children
481
+ });
482
+ }
483
+ function useRun(text) {
484
+ const [error, setError] = useState(null);
485
+ const [busy, setBusy] = useState(false);
486
+ async function run(action) {
487
+ setBusy(true);
488
+ setError(null);
489
+ const result = await attempt(action);
490
+ setBusy(false);
491
+ if (!result.ok)
492
+ setError(errorText(text, result.code));
493
+ return result.ok;
494
+ }
495
+ return { error, setError, busy, run };
496
+ }
497
+ function SignInOrUp({
498
+ theme,
499
+ text,
500
+ signInIntro,
501
+ reason,
502
+ google,
503
+ passkey,
504
+ onSignIn,
505
+ onSignUp,
506
+ rules = { usernameMin: USERNAME.min, usernameMax: USERNAME.max, passwordMin: PASSWORD.min, passwordMax: PASSWORD.max },
507
+ initialMode = "in"
508
+ }) {
509
+ const [mode, setMode] = useState(initialMode);
510
+ const [login, setLogin] = useState("");
511
+ const [email, setEmail] = useState("");
512
+ const [password, setPassword] = useState("");
513
+ const { error, setError, busy, run } = useRun(text);
514
+ const c = theme.colors;
515
+ async function submit() {
516
+ if (mode === "up") {
517
+ const code = usernameErrorCode(login.trim().toLowerCase());
518
+ if (code)
519
+ return setError(errorText(text, code));
520
+ if (password.length < rules.passwordMin)
521
+ return setError(errorText(text, "PASSWORD_TOO_SHORT"));
522
+ }
523
+ const ok = await run(() => mode === "in" ? onSignIn({ login: login.trim(), password }) : onSignUp({ username: login.trim().toLowerCase(), email: email.trim(), password }));
524
+ if (ok)
525
+ setPassword("");
526
+ }
527
+ return /* @__PURE__ */ jsxs(View, {
528
+ style: { gap: 16 },
529
+ children: [
530
+ /* @__PURE__ */ jsx(Heading, {
531
+ theme,
532
+ children: mode === "in" ? text.signIn.title : text.signUp.title
533
+ }),
534
+ /* @__PURE__ */ jsx(Text, {
535
+ style: [{ color: c.muted, fontSize: 15, lineHeight: 21 }, font(theme, "body")],
536
+ children: mode === "in" ? signInIntro : text.signUp.intro
537
+ }),
538
+ reason ? /* @__PURE__ */ jsx(NativeNotice, {
539
+ theme,
540
+ children: reason
541
+ }) : null,
542
+ google ? /* @__PURE__ */ jsx(NativeGoogleButton, {
543
+ label: text.signIn.google,
544
+ busy,
545
+ onPress: () => void run(google)
546
+ }) : null,
547
+ mode === "in" && passkey ? /* @__PURE__ */ jsxs(Fragment, {
548
+ children: [
549
+ /* @__PURE__ */ jsx(NativeButton, {
550
+ theme,
551
+ label: text.signIn.passkey,
552
+ disabled: busy,
553
+ onPress: () => void run(passkey)
554
+ }),
555
+ /* @__PURE__ */ jsx(Text, {
556
+ style: [{ color: c.muted, textAlign: "center" }, font(theme, "body")],
557
+ children: text.signIn.or
558
+ })
559
+ ]
560
+ }) : null,
561
+ /* @__PURE__ */ jsx(NativeField, {
562
+ theme,
563
+ label: mode === "in" ? text.fields.login : text.fields.username,
564
+ hint: mode === "up" ? text.fields.usernameHint(rules.usernameMin, rules.usernameMax) : undefined,
565
+ value: login,
566
+ onChangeText: setLogin,
567
+ autoComplete: "username",
568
+ maxLength: mode === "up" ? rules.usernameMax : undefined
569
+ }),
570
+ mode === "up" ? /* @__PURE__ */ jsx(NativeField, {
571
+ theme,
572
+ label: text.fields.email,
573
+ hint: text.fields.emailHint,
574
+ value: email,
575
+ onChangeText: setEmail,
576
+ autoComplete: "email",
577
+ keyboardType: "email-address"
578
+ }) : null,
579
+ /* @__PURE__ */ jsx(NativeField, {
580
+ theme,
581
+ label: text.fields.password,
582
+ hint: mode === "up" ? text.fields.passwordHint(rules.passwordMin) : undefined,
583
+ value: password,
584
+ onChangeText: setPassword,
585
+ secure: true,
586
+ autoComplete: mode === "in" ? "current-password" : "new-password",
587
+ maxLength: rules.passwordMax
588
+ }),
589
+ mode === "up" ? /* @__PURE__ */ jsx(Text, {
590
+ style: [{ color: c.muted, fontSize: 13, lineHeight: 19 }, font(theme, "body")],
591
+ children: text.signUp.recovery
592
+ }) : null,
593
+ /* @__PURE__ */ jsx(NativeError, {
594
+ theme,
595
+ message: error
596
+ }),
597
+ /* @__PURE__ */ jsx(NativeButton, {
598
+ theme,
599
+ variant: "primary",
600
+ busy,
601
+ label: mode === "in" ? text.signIn.submit : text.signUp.submit,
602
+ onPress: () => void submit()
603
+ }),
604
+ /* @__PURE__ */ jsx(Pressable, {
605
+ accessibilityRole: "button",
606
+ onPress: () => {
607
+ setMode(mode === "in" ? "up" : "in");
608
+ setError(null);
609
+ },
610
+ style: { minHeight: 44, justifyContent: "center", alignItems: "center" },
611
+ children: /* @__PURE__ */ jsx(Text, {
612
+ style: [{ color: c.accent, fontSize: 15, fontWeight: "700" }, font(theme, "body")],
613
+ children: mode === "in" ? `${text.signIn.noAccount} ${text.signIn.createOne}` : `${text.signUp.haveAccount} ${text.signUp.signIn}`
614
+ })
615
+ })
616
+ ]
617
+ });
618
+ }
619
+ function NativeAvatar({
620
+ theme,
621
+ user,
622
+ size = 56
623
+ }) {
624
+ const src = avatarSrc(user.image, size);
625
+ const c = theme.colors;
626
+ return /* @__PURE__ */ jsx(View, {
627
+ accessibilityElementsHidden: true,
628
+ importantForAccessibility: "no-hide-descendants",
629
+ style: {
630
+ width: size,
631
+ height: size,
632
+ borderRadius: size / 2,
633
+ overflow: "hidden",
634
+ backgroundColor: c.raised,
635
+ alignItems: "center",
636
+ justifyContent: "center"
637
+ },
638
+ children: src ? /* @__PURE__ */ jsx(Image, {
639
+ source: { uri: src },
640
+ style: { width: size, height: size }
641
+ }) : /* @__PURE__ */ jsx(Text, {
642
+ style: { color: c.text, fontSize: Math.round(size * 0.36), fontWeight: "700" },
643
+ children: avatarInitials(user)
644
+ })
645
+ });
646
+ }
647
+ function NativeProfileRow({
648
+ theme,
649
+ text,
650
+ field,
651
+ value,
652
+ onSave,
653
+ lockedUntil,
654
+ usernameChangeDays,
655
+ nameMax
656
+ }) {
657
+ const t = text.page;
658
+ const c = theme.colors;
659
+ const [editing, setEditing] = useState(false);
660
+ const [typed, setTyped] = useState(value);
661
+ const [error, setError] = useState(null);
662
+ const [notice, setNotice] = useState(null);
663
+ const [busy, setBusy] = useState(false);
664
+ const label = field === "username" ? t.username : t.displayName;
665
+ async function save() {
666
+ const next = field === "username" ? typed.trim().toLowerCase() : typed.trim();
667
+ const problem = field === "username" ? usernameErrorCode(next) : { empty: "NAME_EMPTY", "too-long": "NAME_TOO_LONG", characters: "NAME_CHARACTERS", none: null }[displayNameProblem(next, nameMax) ?? "none"];
668
+ if (problem)
669
+ return setError(errorText(text, problem));
670
+ if (next === value)
671
+ return setEditing(false);
672
+ setBusy(true);
673
+ setError(null);
674
+ const result = await attempt(() => onSave(next));
675
+ setBusy(false);
676
+ if (result.ok) {
677
+ setEditing(false);
678
+ setNotice(field === "username" ? t.usernameSaved(next) : t.nameSaved);
679
+ } else if (result.code === "USERNAME_CHANGED_RECENTLY" && "until" in result && result.until)
680
+ setError(t.usernameWait(text.date(result.until)));
681
+ else
682
+ setError(errorText(text, result.code));
683
+ }
684
+ if (editing)
685
+ return /* @__PURE__ */ jsxs(View, {
686
+ style: { gap: 10 },
687
+ children: [
688
+ /* @__PURE__ */ jsx(NativeField, {
689
+ theme,
690
+ label,
691
+ hint: field === "username" ? t.usernameRule(usernameChangeDays) : t.displayNameHint,
692
+ value: typed,
693
+ onChangeText: setTyped,
694
+ autoComplete: field === "username" ? "username" : "name",
695
+ capitalize: field === "username" ? "none" : "words",
696
+ maxLength: field === "username" ? USERNAME.max : nameMax
697
+ }),
698
+ /* @__PURE__ */ jsx(NativeError, {
699
+ theme,
700
+ message: error
701
+ }),
702
+ /* @__PURE__ */ jsxs(View, {
703
+ style: { flexDirection: "row", gap: 10 },
704
+ children: [
705
+ /* @__PURE__ */ jsx(NativeButton, {
706
+ theme,
707
+ variant: "primary",
708
+ busy,
709
+ label: t.save,
710
+ onPress: () => void save()
711
+ }),
712
+ /* @__PURE__ */ jsx(NativeButton, {
713
+ theme,
714
+ disabled: busy,
715
+ label: t.cancel,
716
+ onPress: () => {
717
+ setEditing(false);
718
+ setError(null);
719
+ }
720
+ })
721
+ ]
722
+ })
723
+ ]
724
+ });
725
+ return /* @__PURE__ */ jsxs(View, {
726
+ style: { gap: 4 },
727
+ children: [
728
+ /* @__PURE__ */ jsx(Text, {
729
+ style: [{ color: c.muted, fontSize: 13 }, font(theme, "body")],
730
+ children: label
731
+ }),
732
+ /* @__PURE__ */ jsxs(View, {
733
+ style: { flexDirection: "row", alignItems: "center", gap: 12 },
734
+ children: [
735
+ /* @__PURE__ */ jsx(Text, {
736
+ selectable: true,
737
+ style: [{ flex: 1, color: c.text, fontSize: 17 }, font(theme, "body")],
738
+ children: field === "username" ? `@${value}` : value
739
+ }),
740
+ lockedUntil ? null : /* @__PURE__ */ jsx(Pressable, {
741
+ accessibilityRole: "button",
742
+ accessibilityLabel: field === "username" ? t.editUsername : t.editName,
743
+ hitSlop: 8,
744
+ onPress: () => {
745
+ setTyped(value);
746
+ setNotice(null);
747
+ setEditing(true);
748
+ },
749
+ style: ({ pressed }) => ({ minHeight: 36, justifyContent: "center", paddingHorizontal: 12, opacity: pressed ? 0.6 : 1 }),
750
+ children: /* @__PURE__ */ jsx(Text, {
751
+ style: [{ color: c.text, fontSize: 15, fontWeight: "700", textDecorationLine: "underline" }, font(theme, "body")],
752
+ children: t.edit
753
+ })
754
+ })
755
+ ]
756
+ }),
757
+ lockedUntil ? /* @__PURE__ */ jsx(Text, {
758
+ style: [{ color: c.muted, fontSize: 13 }, font(theme, "body")],
759
+ children: t.usernameNextChange(text.date(lockedUntil))
760
+ }) : null,
761
+ notice ? /* @__PURE__ */ jsx(Text, {
762
+ style: [{ color: c.text, fontSize: 15 }, font(theme, "body")],
763
+ children: notice
764
+ }) : null
765
+ ]
766
+ });
767
+ }
768
+ function AccountProfile({
769
+ theme,
770
+ text,
771
+ user,
772
+ onSaveName,
773
+ onSaveUsername,
774
+ usernameLockedUntil,
775
+ usernameChangeDays = 30,
776
+ nameMax = 50,
777
+ onAddPasskey,
778
+ googleOnly,
779
+ google,
780
+ links,
781
+ onSignOut,
782
+ deleteConfirm,
783
+ onDelete
784
+ }) {
785
+ const t = text.page;
786
+ const c = theme.colors;
787
+ const [passkeyNote, setPasskeyNote] = useState(null);
788
+ const [googleNote, setGoogleNote] = useState(null);
789
+ const [googleBusy, setGoogleBusy] = useState(false);
790
+ const [deleting, setDeleting] = useState(false);
791
+ const [asking, setAsking] = useState(false);
792
+ const [password, setPassword] = useState("");
793
+ const del = useRun(text);
794
+ return /* @__PURE__ */ jsxs(View, {
795
+ style: { gap: 18 },
796
+ children: [
797
+ /* @__PURE__ */ jsxs(View, {
798
+ style: { flexDirection: "row", alignItems: "center", gap: 14 },
799
+ children: [
800
+ /* @__PURE__ */ jsx(NativeAvatar, {
801
+ theme,
802
+ user
803
+ }),
804
+ /* @__PURE__ */ jsx(Heading, {
805
+ theme,
806
+ children: t.profile
807
+ })
808
+ ]
809
+ }),
810
+ /* @__PURE__ */ jsx(NativeProfileRow, {
811
+ theme,
812
+ text,
813
+ field: "name",
814
+ value: user.name.trim() || user.username,
815
+ onSave: onSaveName,
816
+ usernameChangeDays,
817
+ nameMax
818
+ }),
819
+ /* @__PURE__ */ jsx(NativeProfileRow, {
820
+ theme,
821
+ text,
822
+ field: "username",
823
+ value: user.username,
824
+ onSave: onSaveUsername,
825
+ lockedUntil: usernameLockedUntil,
826
+ usernameChangeDays,
827
+ nameMax
828
+ }),
829
+ /* @__PURE__ */ jsxs(View, {
830
+ style: { gap: 4 },
831
+ children: [
832
+ /* @__PURE__ */ jsx(Text, {
833
+ style: [{ color: c.muted, fontSize: 13 }, font(theme, "body")],
834
+ children: t.email
835
+ }),
836
+ /* @__PURE__ */ jsx(Text, {
837
+ selectable: true,
838
+ style: [{ color: c.text, fontSize: 17 }, font(theme, "body")],
839
+ children: user.email ?? t.noEmail
840
+ })
841
+ ]
842
+ }),
843
+ onAddPasskey ? /* @__PURE__ */ jsxs(View, {
844
+ style: { gap: 8 },
845
+ children: [
846
+ /* @__PURE__ */ jsx(Muted, {
847
+ theme,
848
+ children: googleOnly ? t.passkeysIntroGoogle : t.passkeysIntro
849
+ }),
850
+ /* @__PURE__ */ jsx(NativeButton, {
851
+ theme,
852
+ label: t.addPasskey,
853
+ onPress: () => {
854
+ setPasskeyNote(null);
855
+ attempt(onAddPasskey).then((result) => setPasskeyNote(result.ok ? t.passkeyAdded : errorText(text, result.code)));
856
+ }
857
+ }),
858
+ passkeyNote ? /* @__PURE__ */ jsx(Text, {
859
+ style: [{ color: c.text, fontSize: 15 }, font(theme, "body")],
860
+ children: passkeyNote
861
+ }) : null
862
+ ]
863
+ }) : null,
864
+ google ? /* @__PURE__ */ jsxs(View, {
865
+ style: { gap: 8 },
866
+ children: [
867
+ /* @__PURE__ */ jsx(Heading, {
868
+ theme,
869
+ size: 17,
870
+ children: t.google
871
+ }),
872
+ googleNote ? /* @__PURE__ */ jsx(Text, {
873
+ accessibilityLiveRegion: "polite",
874
+ style: [{ color: googleNote.kind === "error" ? c.bad : c.text, fontSize: 15 }, font(theme, "body")],
875
+ children: googleNote.text
876
+ }) : null,
877
+ google.connected ? googleNote?.kind === "done" ? null : /* @__PURE__ */ jsx(Muted, {
878
+ theme,
879
+ children: t.googleConnected
880
+ }) : /* @__PURE__ */ jsxs(Fragment, {
881
+ children: [
882
+ /* @__PURE__ */ jsx(Muted, {
883
+ theme,
884
+ children: t.googleIntro
885
+ }),
886
+ /* @__PURE__ */ jsx(NativeGoogleButton, {
887
+ label: t.connectGoogle,
888
+ busy: googleBusy,
889
+ onPress: async () => {
890
+ setGoogleNote(null);
891
+ setGoogleBusy(true);
892
+ const result = await attempt(google.onConnect);
893
+ setGoogleBusy(false);
894
+ setGoogleNote(result.ok ? { kind: "done", text: t.googleJustConnected } : { kind: "error", text: errorText(text, result.code) });
895
+ }
896
+ })
897
+ ]
898
+ })
899
+ ]
900
+ }) : null,
901
+ links,
902
+ /* @__PURE__ */ jsx(NativeButton, {
903
+ theme,
904
+ label: t.signOut,
905
+ onPress: () => void onSignOut()
906
+ }),
907
+ /* @__PURE__ */ jsxs(View, {
908
+ style: { gap: 10, paddingTop: 12, borderTopWidth: 1, borderTopColor: c.border },
909
+ children: [
910
+ /* @__PURE__ */ jsx(Heading, {
911
+ theme,
912
+ size: 17,
913
+ children: t.danger
914
+ }),
915
+ /* @__PURE__ */ jsx(Muted, {
916
+ theme,
917
+ children: t.dangerIntro
918
+ }),
919
+ !deleting ? /* @__PURE__ */ jsx(NativeButton, {
920
+ theme,
921
+ label: t.danger,
922
+ onPress: () => setDeleting(true)
923
+ }) : deleteConfirm.mode === "reauth" && !deleteConfirm.fresh ? /* @__PURE__ */ jsxs(Fragment, {
924
+ children: [
925
+ /* @__PURE__ */ jsx(Muted, {
926
+ theme,
927
+ children: t.dangerReauth(deleteConfirm.minutes ?? 5)
928
+ }),
929
+ /* @__PURE__ */ jsx(NativeError, {
930
+ theme,
931
+ message: del.error
932
+ }),
933
+ /* @__PURE__ */ jsx(NativeGoogleButton, {
934
+ label: t.reauthGoogle,
935
+ busy: del.busy,
936
+ onPress: () => void del.run(deleteConfirm.onReauth)
937
+ })
938
+ ]
939
+ }) : /* @__PURE__ */ jsxs(Fragment, {
940
+ children: [
941
+ deleteConfirm.mode === "password" ? /* @__PURE__ */ jsx(NativeField, {
942
+ theme,
943
+ label: t.dangerConfirm,
944
+ value: password,
945
+ onChangeText: setPassword,
946
+ secure: true,
947
+ autoComplete: "current-password"
948
+ }) : /* @__PURE__ */ jsx(Muted, {
949
+ theme,
950
+ children: t.dangerReady(deleteConfirm.minutes ?? 5)
951
+ }),
952
+ /* @__PURE__ */ jsx(NativeError, {
953
+ theme,
954
+ message: del.error
955
+ }),
956
+ asking ? /* @__PURE__ */ jsxs(View, {
957
+ style: { gap: 10 },
958
+ children: [
959
+ /* @__PURE__ */ jsx(NativeButton, {
960
+ theme,
961
+ disabled: del.busy,
962
+ label: t.cancel,
963
+ onPress: () => {
964
+ setAsking(false);
965
+ setPassword("");
966
+ }
967
+ }),
968
+ /* @__PURE__ */ jsx(NativeButton, {
969
+ theme,
970
+ variant: "danger",
971
+ busy: del.busy,
972
+ label: t.dangerAreYouSure,
973
+ onPress: async () => {
974
+ const ok = await del.run(() => onDelete(deleteConfirm.mode === "password" ? { password } : {}));
975
+ if (!ok) {
976
+ setPassword("");
977
+ setAsking(false);
978
+ }
979
+ }
980
+ })
981
+ ]
982
+ }) : /* @__PURE__ */ jsx(NativeButton, {
983
+ theme,
984
+ variant: "danger",
985
+ label: t.dangerSubmit,
986
+ onPress: () => {
987
+ if (deleteConfirm.mode === "password" && !password)
988
+ return del.setError(errorText(text, "INVALID_PASSWORD"));
989
+ setAsking(true);
990
+ }
991
+ })
992
+ ]
993
+ })
994
+ ]
995
+ })
996
+ ]
997
+ });
998
+ }
999
+ export {
1000
+ AccountProfile,
1001
+ NativeAvatar,
1002
+ NativeButton,
1003
+ NativeError,
1004
+ NativeField,
1005
+ NativeGoogleButton,
1006
+ NativeNotice,
1007
+ SignInOrUp,
1008
+ accountNativeDark,
1009
+ accountNativeLight,
1010
+ accountTextEn,
1011
+ errorText
1012
+ };