@oxy.so/services 14.0.1 → 14.1.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.
Files changed (152) hide show
  1. package/lib/commonjs/ui/components/accountSettings/AccountSettings.js +5 -5
  2. package/lib/commonjs/ui/components/accountSettings/AccountSettings.js.map +1 -1
  3. package/lib/commonjs/ui/components/signIn/OxyAuthenticatorPanel.js +2 -1
  4. package/lib/commonjs/ui/components/signIn/OxyAuthenticatorPanel.js.map +1 -1
  5. package/lib/commonjs/ui/components/signIn/OxyDeleteAccountPanel.js +9 -3
  6. package/lib/commonjs/ui/components/signIn/OxyDeleteAccountPanel.js.map +1 -1
  7. package/lib/commonjs/ui/components/signIn/OxyEmailPanel.js +2 -1
  8. package/lib/commonjs/ui/components/signIn/OxyEmailPanel.js.map +1 -1
  9. package/lib/commonjs/ui/components/signIn/OxyLinkCommonsPanel.js +2 -1
  10. package/lib/commonjs/ui/components/signIn/OxyLinkCommonsPanel.js.map +1 -1
  11. package/lib/commonjs/ui/components/signIn/OxyPasswordPanel.js +8 -1
  12. package/lib/commonjs/ui/components/signIn/OxyPasswordPanel.js.map +1 -1
  13. package/lib/commonjs/ui/components/signIn/OxySignInPanel.js +6 -3
  14. package/lib/commonjs/ui/components/signIn/OxySignInPanel.js.map +1 -1
  15. package/lib/commonjs/ui/components/signIn/OxySignUpPanel.js +39 -10
  16. package/lib/commonjs/ui/components/signIn/OxySignUpPanel.js.map +1 -1
  17. package/lib/commonjs/ui/components/signIn/ReauthStep.js +7 -1
  18. package/lib/commonjs/ui/components/signIn/ReauthStep.js.map +1 -1
  19. package/lib/commonjs/ui/components/signIn/accountFlowParts.js +16 -86
  20. package/lib/commonjs/ui/components/signIn/accountFlowParts.js.map +1 -1
  21. package/lib/commonjs/ui/hooks/mutations/mutationFactory.js +5 -4
  22. package/lib/commonjs/ui/hooks/mutations/mutationFactory.js.map +1 -1
  23. package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js +31 -7
  24. package/lib/commonjs/ui/hooks/mutations/useAccountMutations.js.map +1 -1
  25. package/lib/commonjs/ui/hooks/mutations/useServicesMutations.js +22 -5
  26. package/lib/commonjs/ui/hooks/mutations/useServicesMutations.js.map +1 -1
  27. package/lib/commonjs/ui/hooks/useUsernameAvailability.js +8 -3
  28. package/lib/commonjs/ui/hooks/useUsernameAvailability.js.map +1 -1
  29. package/lib/commonjs/ui/screens/AccountMembersScreen.js +25 -21
  30. package/lib/commonjs/ui/screens/AccountMembersScreen.js.map +1 -1
  31. package/lib/commonjs/ui/screens/CreateAccountScreen.js +15 -19
  32. package/lib/commonjs/ui/screens/CreateAccountScreen.js.map +1 -1
  33. package/lib/commonjs/ui/screens/DomainsScreen.js +25 -11
  34. package/lib/commonjs/ui/screens/DomainsScreen.js.map +1 -1
  35. package/lib/commonjs/ui/screens/EditProfileFieldScreen.js +10 -5
  36. package/lib/commonjs/ui/screens/EditProfileFieldScreen.js.map +1 -1
  37. package/lib/commonjs/ui/screens/WelcomeNewUserScreen.js +27 -7
  38. package/lib/commonjs/ui/screens/WelcomeNewUserScreen.js.map +1 -1
  39. package/lib/commonjs/ui/utils/apiErrorMessage.js +117 -0
  40. package/lib/commonjs/ui/utils/apiErrorMessage.js.map +1 -0
  41. package/lib/commonjs/ui/utils/usernamePolicy.js +75 -0
  42. package/lib/commonjs/ui/utils/usernamePolicy.js.map +1 -0
  43. package/lib/module/ui/components/accountSettings/AccountSettings.js +5 -5
  44. package/lib/module/ui/components/accountSettings/AccountSettings.js.map +1 -1
  45. package/lib/module/ui/components/signIn/OxyAuthenticatorPanel.js +3 -2
  46. package/lib/module/ui/components/signIn/OxyAuthenticatorPanel.js.map +1 -1
  47. package/lib/module/ui/components/signIn/OxyDeleteAccountPanel.js +9 -3
  48. package/lib/module/ui/components/signIn/OxyDeleteAccountPanel.js.map +1 -1
  49. package/lib/module/ui/components/signIn/OxyEmailPanel.js +3 -2
  50. package/lib/module/ui/components/signIn/OxyEmailPanel.js.map +1 -1
  51. package/lib/module/ui/components/signIn/OxyLinkCommonsPanel.js +3 -2
  52. package/lib/module/ui/components/signIn/OxyLinkCommonsPanel.js.map +1 -1
  53. package/lib/module/ui/components/signIn/OxyPasswordPanel.js +8 -1
  54. package/lib/module/ui/components/signIn/OxyPasswordPanel.js.map +1 -1
  55. package/lib/module/ui/components/signIn/OxySignInPanel.js +7 -4
  56. package/lib/module/ui/components/signIn/OxySignInPanel.js.map +1 -1
  57. package/lib/module/ui/components/signIn/OxySignUpPanel.js +38 -9
  58. package/lib/module/ui/components/signIn/OxySignUpPanel.js.map +1 -1
  59. package/lib/module/ui/components/signIn/ReauthStep.js +9 -3
  60. package/lib/module/ui/components/signIn/ReauthStep.js.map +1 -1
  61. package/lib/module/ui/components/signIn/accountFlowParts.js +15 -82
  62. package/lib/module/ui/components/signIn/accountFlowParts.js.map +1 -1
  63. package/lib/module/ui/hooks/mutations/mutationFactory.js +5 -4
  64. package/lib/module/ui/hooks/mutations/mutationFactory.js.map +1 -1
  65. package/lib/module/ui/hooks/mutations/useAccountMutations.js +31 -7
  66. package/lib/module/ui/hooks/mutations/useAccountMutations.js.map +1 -1
  67. package/lib/module/ui/hooks/mutations/useServicesMutations.js +22 -5
  68. package/lib/module/ui/hooks/mutations/useServicesMutations.js.map +1 -1
  69. package/lib/module/ui/hooks/useUsernameAvailability.js +8 -3
  70. package/lib/module/ui/hooks/useUsernameAvailability.js.map +1 -1
  71. package/lib/module/ui/screens/AccountMembersScreen.js +25 -21
  72. package/lib/module/ui/screens/AccountMembersScreen.js.map +1 -1
  73. package/lib/module/ui/screens/CreateAccountScreen.js +16 -20
  74. package/lib/module/ui/screens/CreateAccountScreen.js.map +1 -1
  75. package/lib/module/ui/screens/DomainsScreen.js +26 -10
  76. package/lib/module/ui/screens/DomainsScreen.js.map +1 -1
  77. package/lib/module/ui/screens/EditProfileFieldScreen.js +11 -6
  78. package/lib/module/ui/screens/EditProfileFieldScreen.js.map +1 -1
  79. package/lib/module/ui/screens/WelcomeNewUserScreen.js +29 -9
  80. package/lib/module/ui/screens/WelcomeNewUserScreen.js.map +1 -1
  81. package/lib/module/ui/utils/apiErrorMessage.js +107 -0
  82. package/lib/module/ui/utils/apiErrorMessage.js.map +1 -0
  83. package/lib/module/ui/utils/usernamePolicy.js +70 -0
  84. package/lib/module/ui/utils/usernamePolicy.js.map +1 -0
  85. package/lib/typescript/commonjs/ui/components/accountSettings/AccountSettings.d.ts.map +1 -1
  86. package/lib/typescript/commonjs/ui/components/signIn/OxyDeleteAccountPanel.d.ts.map +1 -1
  87. package/lib/typescript/commonjs/ui/components/signIn/OxyLinkCommonsPanel.d.ts.map +1 -1
  88. package/lib/typescript/commonjs/ui/components/signIn/OxyPasswordPanel.d.ts.map +1 -1
  89. package/lib/typescript/commonjs/ui/components/signIn/OxySignInPanel.d.ts.map +1 -1
  90. package/lib/typescript/commonjs/ui/components/signIn/OxySignUpPanel.d.ts.map +1 -1
  91. package/lib/typescript/commonjs/ui/components/signIn/ReauthStep.d.ts.map +1 -1
  92. package/lib/typescript/commonjs/ui/components/signIn/accountFlowParts.d.ts +7 -16
  93. package/lib/typescript/commonjs/ui/components/signIn/accountFlowParts.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/ui/hooks/mutations/mutationFactory.d.ts +4 -1
  95. package/lib/typescript/commonjs/ui/hooks/mutations/mutationFactory.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
  97. package/lib/typescript/commonjs/ui/hooks/mutations/useServicesMutations.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/ui/hooks/useUsernameAvailability.d.ts.map +1 -1
  99. package/lib/typescript/commonjs/ui/screens/AccountMembersScreen.d.ts.map +1 -1
  100. package/lib/typescript/commonjs/ui/screens/CreateAccountScreen.d.ts.map +1 -1
  101. package/lib/typescript/commonjs/ui/screens/DomainsScreen.d.ts.map +1 -1
  102. package/lib/typescript/commonjs/ui/screens/EditProfileFieldScreen.d.ts.map +1 -1
  103. package/lib/typescript/commonjs/ui/screens/WelcomeNewUserScreen.d.ts.map +1 -1
  104. package/lib/typescript/commonjs/ui/utils/apiErrorMessage.d.ts +30 -0
  105. package/lib/typescript/commonjs/ui/utils/apiErrorMessage.d.ts.map +1 -0
  106. package/lib/typescript/commonjs/ui/utils/usernamePolicy.d.ts +33 -0
  107. package/lib/typescript/commonjs/ui/utils/usernamePolicy.d.ts.map +1 -0
  108. package/lib/typescript/module/ui/components/accountSettings/AccountSettings.d.ts.map +1 -1
  109. package/lib/typescript/module/ui/components/signIn/OxyDeleteAccountPanel.d.ts.map +1 -1
  110. package/lib/typescript/module/ui/components/signIn/OxyLinkCommonsPanel.d.ts.map +1 -1
  111. package/lib/typescript/module/ui/components/signIn/OxyPasswordPanel.d.ts.map +1 -1
  112. package/lib/typescript/module/ui/components/signIn/OxySignInPanel.d.ts.map +1 -1
  113. package/lib/typescript/module/ui/components/signIn/OxySignUpPanel.d.ts.map +1 -1
  114. package/lib/typescript/module/ui/components/signIn/ReauthStep.d.ts.map +1 -1
  115. package/lib/typescript/module/ui/components/signIn/accountFlowParts.d.ts +7 -16
  116. package/lib/typescript/module/ui/components/signIn/accountFlowParts.d.ts.map +1 -1
  117. package/lib/typescript/module/ui/hooks/mutations/mutationFactory.d.ts +4 -1
  118. package/lib/typescript/module/ui/hooks/mutations/mutationFactory.d.ts.map +1 -1
  119. package/lib/typescript/module/ui/hooks/mutations/useAccountMutations.d.ts.map +1 -1
  120. package/lib/typescript/module/ui/hooks/mutations/useServicesMutations.d.ts.map +1 -1
  121. package/lib/typescript/module/ui/hooks/useUsernameAvailability.d.ts.map +1 -1
  122. package/lib/typescript/module/ui/screens/AccountMembersScreen.d.ts.map +1 -1
  123. package/lib/typescript/module/ui/screens/CreateAccountScreen.d.ts.map +1 -1
  124. package/lib/typescript/module/ui/screens/DomainsScreen.d.ts.map +1 -1
  125. package/lib/typescript/module/ui/screens/EditProfileFieldScreen.d.ts.map +1 -1
  126. package/lib/typescript/module/ui/screens/WelcomeNewUserScreen.d.ts.map +1 -1
  127. package/lib/typescript/module/ui/utils/apiErrorMessage.d.ts +30 -0
  128. package/lib/typescript/module/ui/utils/apiErrorMessage.d.ts.map +1 -0
  129. package/lib/typescript/module/ui/utils/usernamePolicy.d.ts +33 -0
  130. package/lib/typescript/module/ui/utils/usernamePolicy.d.ts.map +1 -0
  131. package/package.json +6 -6
  132. package/src/ui/components/accountSettings/AccountSettings.tsx +5 -7
  133. package/src/ui/components/signIn/OxyAuthenticatorPanel.tsx +2 -2
  134. package/src/ui/components/signIn/OxyDeleteAccountPanel.tsx +7 -3
  135. package/src/ui/components/signIn/OxyEmailPanel.tsx +2 -2
  136. package/src/ui/components/signIn/OxyLinkCommonsPanel.tsx +3 -7
  137. package/src/ui/components/signIn/OxyPasswordPanel.tsx +6 -1
  138. package/src/ui/components/signIn/OxySignInPanel.tsx +13 -6
  139. package/src/ui/components/signIn/OxySignUpPanel.tsx +38 -11
  140. package/src/ui/components/signIn/ReauthStep.tsx +9 -3
  141. package/src/ui/components/signIn/accountFlowParts.tsx +16 -87
  142. package/src/ui/hooks/mutations/mutationFactory.ts +9 -10
  143. package/src/ui/hooks/mutations/useAccountMutations.ts +20 -9
  144. package/src/ui/hooks/mutations/useServicesMutations.ts +12 -5
  145. package/src/ui/hooks/useUsernameAvailability.ts +6 -3
  146. package/src/ui/screens/AccountMembersScreen.tsx +29 -27
  147. package/src/ui/screens/CreateAccountScreen.tsx +20 -40
  148. package/src/ui/screens/DomainsScreen.tsx +22 -11
  149. package/src/ui/screens/EditProfileFieldScreen.tsx +10 -8
  150. package/src/ui/screens/WelcomeNewUserScreen.tsx +36 -14
  151. package/src/ui/utils/apiErrorMessage.ts +111 -0
  152. package/src/ui/utils/usernamePolicy.ts +81 -0
@@ -10,11 +10,9 @@ import { PhoneInput, COUNTRIES, findCountry } from '@oxy.so/bloom/phone-input';
10
10
  import { Textarea } from '@oxy.so/bloom/textarea';
11
11
  import { DatePicker } from '@oxy.so/bloom/date-picker';
12
12
  import { Field } from '@oxy.so/bloom/field';
13
- import {
14
- USERNAME_INVALID_MESSAGE,
15
- usernameCheck as usernamePolicyCheck,
16
- } from '@oxy.so/contracts/client';
13
+ import { USERNAME_MAX_LENGTH, stripDisallowedUsernameCharacters } from '@oxy.so/contracts/client';
17
14
  import { useUsernameAvailability } from '../hooks/useUsernameAvailability';
15
+ import { checkUsernameDraft } from '../utils/usernamePolicy';
18
16
  import { GroupedSection, GroupedSectionItem } from '@oxy.so/bloom/grouped-section';
19
17
  import type { User } from '@oxy.so/core';
20
18
  import { useI18n } from '../hooks/useI18n';
@@ -263,12 +261,14 @@ const EditProfileFieldScreen: React.FC<EditProfileFieldScreenProps> = ({
263
261
  label: t('editProfile.items.username.label') || 'Username',
264
262
  placeholder: t('editProfile.items.username.placeholder') || 'Choose a username',
265
263
  validation: (value) => {
266
- const checked = usernamePolicyCheck(value, []);
267
- return checked.ok ? undefined : checked.issue.message;
264
+ const checked = checkUsernameDraft(value, t, kind);
265
+ return checked.ok ? undefined : checked.error;
268
266
  },
269
267
  inputProps: {
270
268
  autoCapitalize: 'none',
271
269
  autoCorrect: false,
270
+ autoComplete: 'username',
271
+ maxLength: USERNAME_MAX_LENGTH,
272
272
  },
273
273
  },
274
274
  ],
@@ -366,7 +366,9 @@ const EditProfileFieldScreen: React.FC<EditProfileFieldScreenProps> = ({
366
366
 
367
367
  // Field change handler
368
368
  const handleFieldChange = useCallback(
369
- (key: string, value: string) => {
369
+ (key: string, raw: string) => {
370
+ // A username field takes only the characters the policy allows.
371
+ const value = key === 'username' ? stripDisallowedUsernameCharacters(raw) : raw;
370
372
  setFieldValues((prev) => ({ ...prev, [key]: value }));
371
373
  if (fieldErrors[key]) {
372
374
  setFieldErrors((prev) => ({ ...prev, [key]: undefined }));
@@ -565,7 +567,7 @@ const EditProfileFieldScreen: React.FC<EditProfileFieldScreenProps> = ({
565
567
  ? t('editProfile.items.username.checking')
566
568
  : usernameCheck.status === 'available'
567
569
  ? t('accounts.create.username.available')
568
- : USERNAME_INVALID_MESSAGE
570
+ : t('usernamePolicy.hint')
569
571
  : undefined
570
572
  }
571
573
  >
@@ -4,16 +4,21 @@ import { View, StyleSheet, Platform, Animated } from 'react-native';
4
4
  import type { BaseScreenProps } from '../types/navigation';
5
5
  import { Avatar } from '@oxy.so/bloom/avatar';
6
6
  import Ionicons from '../icons/Ionicons';
7
- import { toast } from '@oxy.so/bloom/toast';
8
7
  import { useTheme } from '@oxy.so/bloom/theme';
9
8
  import { Button } from '@oxy.so/bloom/button';
10
9
  import { H1, Text } from '@oxy.so/bloom/typography';
11
- import { TextField, TextFieldInput } from '@oxy.so/bloom/text-field';
10
+ import { TextField, TextFieldHint, TextFieldInput } from '@oxy.so/bloom/text-field';
12
11
  import { useI18n } from '../hooks/useI18n';
13
12
  import { useSurfaceHeader } from '../hooks/useSurfaceHeader';
14
13
  import { useOxy } from '../context/oxyRuntimeContext';
15
14
  import { useUpdateProfile } from '../hooks/mutations/useAccountMutations';
16
- import { getNormalizedUserHandle, type User } from '@oxy.so/core';
15
+ import {
16
+ accountNameInvalidKey,
17
+ accountNameMaxLength,
18
+ getNormalizedUserHandle,
19
+ isValidAccountName,
20
+ type User,
21
+ } from '@oxy.so/core';
17
22
 
18
23
  const GAP = 12;
19
24
  const INNER_GAP = 8;
@@ -54,6 +59,8 @@ const WelcomeNewUserScreen: React.FC<BaseScreenProps & { newUser?: User }> = ({
54
59
  const [firstName, setFirstName] = useState(() => (currentUser?.name?.first ?? '').trim());
55
60
  const [lastName, setLastName] = useState(() => (currentUser?.name?.last ?? '').trim());
56
61
  const [savingName, setSavingName] = useState(false);
62
+ const [nameInvalid, setNameInvalid] = useState(false);
63
+ const nameKind = currentUser?.kind;
57
64
 
58
65
  // Derived, not mirrored: `openAvatarPicker` writes through the shared avatar
59
66
  // path, which updates the active account — including CLEARING it when the
@@ -193,20 +200,22 @@ const WelcomeNewUserScreen: React.FC<BaseScreenProps & { newUser?: User }> = ({
193
200
  const submitName = useCallback(async () => {
194
201
  const f = firstName.trim();
195
202
  if (!f) return;
203
+ // The name policy `PUT /users/me` holds the account's kind to, checked
204
+ // before sending so the refusal names the rule, inline.
205
+ if (!isValidAccountName(f, nameKind) || !isValidAccountName(lastName, nameKind)) {
206
+ setNameInvalid(true);
207
+ return;
208
+ }
196
209
  setSavingName(true);
197
210
  try {
198
211
  await updateProfileMutation.mutateAsync({ name: { first: f, last: lastName.trim() } });
199
212
  animateToStepCallback(currentStep + 1);
200
- } catch (e: unknown) {
201
- toast.error(
202
- (e instanceof Error ? e.message : null) ||
203
- t('welcomeNew.name.saveFailed') ||
204
- 'Could not save your name',
205
- );
213
+ } catch {
214
+ // `useUpdateProfile` already told the person why, in their language.
206
215
  } finally {
207
216
  setSavingName(false);
208
217
  }
209
- }, [firstName, lastName, updateProfileMutation, animateToStepCallback, currentStep, t]);
218
+ }, [firstName, lastName, nameKind, updateProfileMutation, animateToStepCallback, currentStep]);
210
219
  /**
211
220
  * Onboarding uses the SAME avatar flow as everywhere else — the ChangeAvatar
212
221
  * surface (source list → crop → upload). It used to run its own
@@ -360,28 +369,41 @@ const WelcomeNewUserScreen: React.FC<BaseScreenProps & { newUser?: User }> = ({
360
369
  {step.showNameForm && (
361
370
  <View style={[styles.nameForm, styles.sectionSpacing]}>
362
371
  <View style={styles.nameField}>
363
- <TextField>
372
+ <TextField isInvalid={nameInvalid}>
364
373
  <TextFieldInput
365
374
  floatingLabel
366
375
  label={t('welcomeNew.name.firstLabel') || 'First name'}
367
376
  value={firstName}
368
- onChangeText={setFirstName}
377
+ onChangeText={(value) => {
378
+ setFirstName(value);
379
+ setNameInvalid(false);
380
+ }}
381
+ maxLength={accountNameMaxLength(nameKind)}
369
382
  autoFocus
370
383
  autoCapitalize="words"
371
384
  />
372
385
  </TextField>
373
386
  </View>
374
387
  <View style={styles.nameField}>
375
- <TextField>
388
+ <TextField isInvalid={nameInvalid}>
376
389
  <TextFieldInput
377
390
  floatingLabel
378
391
  label={t('welcomeNew.name.lastLabel') || 'Last name'}
379
392
  value={lastName}
380
- onChangeText={setLastName}
393
+ onChangeText={(value) => {
394
+ setLastName(value);
395
+ setNameInvalid(false);
396
+ }}
397
+ maxLength={accountNameMaxLength(nameKind)}
381
398
  autoCapitalize="words"
382
399
  />
383
400
  </TextField>
384
401
  </View>
402
+ {nameInvalid ? (
403
+ <TextFieldHint invalid>
404
+ {t(`editProfile.items.displayName.${accountNameInvalidKey(nameKind)}`)}
405
+ </TextFieldHint>
406
+ ) : null}
385
407
  </View>
386
408
  )}
387
409
  {step.showAvatar && (
@@ -0,0 +1,111 @@
1
+ /**
2
+ * What a failed SDK call means to the person who made it.
3
+ *
4
+ * Every screen and mutation that shows a failure words it here, from the API's
5
+ * machine code and HTTP status. A server `message` is never shown: it is
6
+ * English, and for a validation failure it is "Validation failed".
7
+ */
8
+
9
+ import { EMAIL_VERIFICATION_ERROR_CODES, SIGN_IN_ERROR_CODES } from '@oxy.so/contracts/client';
10
+ import type { Translate } from '../hooks/useI18n';
11
+
12
+ /** How long a screen waits after a 429 that names no wait of its own. */
13
+ export const RATE_LIMIT_SECONDS = 60;
14
+
15
+ /** The API error code a thrown SDK error carries (`error.code`), if any. */
16
+ export function errorCode(error: unknown): string | undefined {
17
+ const code = (error as { code?: unknown } | null)?.code;
18
+ return typeof code === 'string' ? code : undefined;
19
+ }
20
+
21
+ /** An HTTP status off a thrown SDK error. */
22
+ export function errorStatus(error: unknown): number | undefined {
23
+ return (
24
+ (error as { status?: number } | undefined)?.status ??
25
+ (error as { response?: { status?: number } } | undefined)?.response?.status
26
+ );
27
+ }
28
+
29
+ /** A 429: too many attempts or emails. */
30
+ export function isRateLimited(error: unknown): boolean {
31
+ return errorStatus(error) === 429;
32
+ }
33
+
34
+ /** How long to wait after a 429, from the API's `retryAfterSeconds` when it sent one. */
35
+ export function retryAfterSeconds(error: unknown): number {
36
+ const seconds = (error as { details?: { retryAfterSeconds?: unknown } } | undefined)?.details
37
+ ?.retryAfterSeconds;
38
+ return typeof seconds === 'number' && seconds > 0 ? Math.ceil(seconds) : RATE_LIMIT_SECONDS;
39
+ }
40
+
41
+ /**
42
+ * A failed API call, in the person's language — never the server's `message`,
43
+ * which is English and written for developers. Never says whether an account
44
+ * exists: every "no" is the same sentence.
45
+ *
46
+ * `fallback` names what the caller was doing ("Couldn't save your profile"),
47
+ * for a failure with no more specific reason; it defaults to the generic line.
48
+ */
49
+ export function describeApiError(error: unknown, t: Translate, fallback?: string): string {
50
+ switch (errorCode(error)) {
51
+ case SIGN_IN_ERROR_CODES.invalidCredentials:
52
+ return t('signin.errors.invalidCredentials');
53
+ case SIGN_IN_ERROR_CODES.requestInvalid:
54
+ case SIGN_IN_ERROR_CODES.linkInvalid:
55
+ return t('signin.errors.requestExpired');
56
+ case SIGN_IN_ERROR_CODES.secondFactorInvalid:
57
+ case SIGN_IN_ERROR_CODES.totpCodeInvalid:
58
+ return t('signin.errors.secondFactorInvalid');
59
+ case SIGN_IN_ERROR_CODES.reauthInvalid:
60
+ case SIGN_IN_ERROR_CODES.reauthRequired:
61
+ return t('reauth.errors.invalid');
62
+ case SIGN_IN_ERROR_CODES.totpRequired:
63
+ return t('reauth.errors.totpRequired');
64
+ case SIGN_IN_ERROR_CODES.originNotAllowed:
65
+ return t('signin.errors.originNotAllowed');
66
+ case SIGN_IN_ERROR_CODES.usernameTaken:
67
+ return t('signup.username.taken');
68
+ case SIGN_IN_ERROR_CODES.emailAccountsFull:
69
+ return t('signin.errors.emailAccountsFull');
70
+ case SIGN_IN_ERROR_CODES.lastSignInMethod:
71
+ return t('signInSecurity.errors.lastSignInMethod');
72
+ case SIGN_IN_ERROR_CODES.passwordRequired:
73
+ return t('signInSecurity.errors.passwordRequired');
74
+ case SIGN_IN_ERROR_CODES.emailUnchanged:
75
+ return t('signInSecurity.errors.emailUnchanged');
76
+ case EMAIL_VERIFICATION_ERROR_CODES.codeInvalid:
77
+ return t('emailCode.errors.codeInvalid');
78
+ case EMAIL_VERIFICATION_ERROR_CODES.tooManyAttempts:
79
+ return t('emailCode.errors.tooManyAttempts');
80
+ case EMAIL_VERIFICATION_ERROR_CODES.ticketInvalid:
81
+ case EMAIL_VERIFICATION_ERROR_CODES.ticketRequired:
82
+ return t('emailCode.errors.expired');
83
+ case EMAIL_VERIFICATION_ERROR_CODES.unavailable:
84
+ return t('emailCode.errors.unavailable');
85
+ // A malformed request: every screen checks the contract before sending, so
86
+ // this is the backstop for a rule the client does not know yet.
87
+ case 'BAD_REQUEST':
88
+ return t('signin.errors.invalidInput');
89
+ case 'NETWORK_ERROR':
90
+ case 'CONNECTION_FAILED':
91
+ case 'TIMEOUT':
92
+ return t('signin.errors.network');
93
+ default: {
94
+ if (isRateLimited(error))
95
+ return t('signin.errors.rateLimited', { seconds: retryAfterSeconds(error) });
96
+ const status = errorStatus(error);
97
+ if (status === 0) return t('signin.errors.network');
98
+ if (status !== undefined && status >= 500) return t('signin.errors.server');
99
+ return fallback ?? t('signin.errors.generic');
100
+ }
101
+ }
102
+ }
103
+
104
+ /** Whether the confirmation this flow holds is gone and it must start again. */
105
+ export function isTicketExpired(error: unknown): boolean {
106
+ const code = errorCode(error);
107
+ return (
108
+ code === EMAIL_VERIFICATION_ERROR_CODES.ticketInvalid ||
109
+ code === EMAIL_VERIFICATION_ERROR_CODES.ticketRequired
110
+ );
111
+ }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * The username policy, in the words of the person typing.
3
+ *
4
+ * Every field that takes a username (signing up, editing yours, creating an
5
+ * account you manage) asks the ONE policy in `@oxy.so/contracts/client` and
6
+ * says WHICH rule the candidate broke, localized. The policy's own messages are
7
+ * English and catch-all; shown raw, or not checked at all, a name like
8
+ * `hash saah` reached the server, was refused with a 400, and the person read
9
+ * "Something went wrong".
10
+ */
11
+
12
+ import type { AccountKind } from '@oxy.so/core';
13
+ import {
14
+ BOT_USERNAME_INVALID_MESSAGE,
15
+ NUMERIC_USERNAME_MESSAGE,
16
+ RESERVED_USERNAME_MESSAGE,
17
+ USERNAME_MAX_LENGTH,
18
+ USERNAME_MIN_LENGTH,
19
+ usernameCheckForAccountKind,
20
+ } from '@oxy.so/contracts/client';
21
+ import type { Translate } from '../hooks/useI18n';
22
+
23
+ export type UsernameDraftResult = { ok: true; username: string } | { ok: false; error: string };
24
+
25
+ /**
26
+ * Hold a typed username to the policy for `kind` (`bot` handles end in `bot`).
27
+ * On success `username` is the trimmed handle to send; on failure `error` is
28
+ * the localized reason, specific to the rule that failed.
29
+ */
30
+ export function checkUsernameDraft(
31
+ value: string,
32
+ t: Translate,
33
+ kind?: AccountKind | null,
34
+ ): UsernameDraftResult {
35
+ const username = value.trim();
36
+ if (!username) return { ok: false, error: t('usernamePolicy.required') };
37
+ const checked = usernameCheckForAccountKind(kind)(username, []);
38
+ if (checked.ok) return { ok: true, username: checked.value };
39
+ return { ok: false, error: describeUsernameIssue(username, checked.issue.message, t) };
40
+ }
41
+
42
+ /**
43
+ * The server's refusal of `username` (a 400 naming the `username` field, whose
44
+ * message is the policy's), in the same words as {@link checkUsernameDraft};
45
+ * `null` when `reason` is something else. The backstop for a rule this client
46
+ * does not know yet: the person still reads which rule, in their language.
47
+ */
48
+ export function describeUsernameRefusal(
49
+ reason: unknown,
50
+ username: string,
51
+ t: Translate,
52
+ ): string | null {
53
+ const error = reason as {
54
+ code?: unknown;
55
+ message?: unknown;
56
+ details?: { field?: unknown };
57
+ } | null;
58
+ if (error?.code !== 'BAD_REQUEST' || error.details?.field !== 'username') return null;
59
+ const message = typeof error.message === 'string' ? error.message : '';
60
+ return describeUsernameIssue(username.trim(), message, t);
61
+ }
62
+
63
+ function describeUsernameIssue(username: string, message: string, t: Translate): string {
64
+ switch (message) {
65
+ case NUMERIC_USERNAME_MESSAGE:
66
+ return t('usernamePolicy.onlyNumbers');
67
+ case RESERVED_USERNAME_MESSAGE:
68
+ return t('usernamePolicy.reserved');
69
+ case BOT_USERNAME_INVALID_MESSAGE:
70
+ return t('usernamePolicy.mustEndInBot');
71
+ default:
72
+ // `USERNAME_INVALID_MESSAGE` covers length and shape alike; the length is
73
+ // read here so the reason names the rule that actually failed.
74
+ if (username.length < USERNAME_MIN_LENGTH)
75
+ return t('usernamePolicy.tooShort', { min: USERNAME_MIN_LENGTH });
76
+ if (username.length > USERNAME_MAX_LENGTH)
77
+ return t('usernamePolicy.tooLong', { max: USERNAME_MAX_LENGTH });
78
+ if (/\s/.test(username)) return t('usernamePolicy.noSpaces');
79
+ return t('usernamePolicy.characters');
80
+ }
81
+ }