@eifi1/ui-kit 0.28.1 → 0.30.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 (228) hide show
  1. package/README.md +44 -4
  2. package/dist/account/account-parts.d.ts +21 -0
  3. package/dist/account/account-parts.js +40 -0
  4. package/dist/account/account-parts.js.map +1 -0
  5. package/dist/account/data-export-setting.d.ts +73 -0
  6. package/dist/account/data-export-setting.js +113 -0
  7. package/dist/account/data-export-setting.js.map +1 -0
  8. package/dist/account/delete-account-setting.d.ts +91 -0
  9. package/dist/account/delete-account-setting.js +199 -0
  10. package/dist/account/delete-account-setting.js.map +1 -0
  11. package/dist/account/email-change-setting.d.ts +136 -0
  12. package/dist/account/email-change-setting.js +277 -0
  13. package/dist/account/email-change-setting.js.map +1 -0
  14. package/dist/account/sessions-setting.d.ts +110 -0
  15. package/dist/account/sessions-setting.js +146 -0
  16. package/dist/account/sessions-setting.js.map +1 -0
  17. package/dist/admin/admin-action-confirm.d.ts +91 -0
  18. package/dist/admin/admin-action-confirm.js +276 -0
  19. package/dist/admin/admin-action-confirm.js.map +1 -0
  20. package/dist/admin/admin-action-log.d.ts +105 -0
  21. package/dist/admin/admin-action-log.js +153 -0
  22. package/dist/admin/admin-action-log.js.map +1 -0
  23. package/dist/admin/admin-parts.d.ts +40 -0
  24. package/dist/admin/admin-parts.js +45 -0
  25. package/dist/admin/admin-parts.js.map +1 -0
  26. package/dist/admin/invitations-panel.d.ts +91 -0
  27. package/dist/admin/invitations-panel.js +289 -0
  28. package/dist/admin/invitations-panel.js.map +1 -0
  29. package/dist/admin/reviewer-scope-editor.d.ts +91 -0
  30. package/dist/admin/reviewer-scope-editor.js +134 -0
  31. package/dist/admin/reviewer-scope-editor.js.map +1 -0
  32. package/dist/admin/role-select.d.ts +91 -0
  33. package/dist/admin/role-select.js +128 -0
  34. package/dist/admin/role-select.js.map +1 -0
  35. package/dist/admin/transfer-ownership-dialog.d.ts +91 -0
  36. package/dist/admin/transfer-ownership-dialog.js +83 -0
  37. package/dist/admin/transfer-ownership-dialog.js.map +1 -0
  38. package/dist/admin/user-roster.d.ts +91 -0
  39. package/dist/admin/user-roster.js +285 -0
  40. package/dist/admin/user-roster.js.map +1 -0
  41. package/dist/auth/accept-invitation.d.ts +90 -0
  42. package/dist/auth/accept-invitation.js +117 -0
  43. package/dist/auth/accept-invitation.js.map +1 -0
  44. package/dist/auth/auth-errors.d.ts +139 -0
  45. package/dist/auth/auth-errors.js +92 -0
  46. package/dist/auth/auth-errors.js.map +1 -0
  47. package/dist/auth/complete-name-dialog.d.ts +71 -0
  48. package/dist/auth/complete-name-dialog.js +168 -0
  49. package/dist/auth/complete-name-dialog.js.map +1 -0
  50. package/dist/auth/email-tag.d.ts +60 -0
  51. package/dist/auth/email-tag.js +23 -0
  52. package/dist/auth/email-tag.js.map +1 -0
  53. package/dist/auth/forgot-password-form.d.ts +86 -0
  54. package/dist/auth/forgot-password-form.js +123 -0
  55. package/dist/auth/forgot-password-form.js.map +1 -0
  56. package/dist/auth/form-rules.d.ts +27 -0
  57. package/dist/auth/form-rules.js +23 -0
  58. package/dist/auth/form-rules.js.map +1 -0
  59. package/dist/auth/new-password-fields.d.ts +38 -0
  60. package/dist/auth/new-password-fields.js +54 -0
  61. package/dist/auth/new-password-fields.js.map +1 -0
  62. package/dist/auth/not-found-page.d.ts +57 -0
  63. package/dist/auth/not-found-page.js +40 -0
  64. package/dist/auth/not-found-page.js.map +1 -0
  65. package/dist/auth/register-form.d.ts +91 -0
  66. package/dist/auth/register-form.js +270 -0
  67. package/dist/auth/register-form.js.map +1 -0
  68. package/dist/auth/reset-password-form.d.ts +131 -0
  69. package/dist/auth/reset-password-form.js +167 -0
  70. package/dist/auth/reset-password-form.js.map +1 -0
  71. package/dist/auth/sign-in-form.d.ts +295 -0
  72. package/dist/auth/sign-in-form.js +426 -0
  73. package/dist/auth/sign-in-form.js.map +1 -0
  74. package/dist/auth/status-parts.d.ts +78 -0
  75. package/dist/auth/status-parts.js +83 -0
  76. package/dist/auth/status-parts.js.map +1 -0
  77. package/dist/auth/verify-email.d.ts +126 -0
  78. package/dist/auth/verify-email.js +243 -0
  79. package/dist/auth/verify-email.js.map +1 -0
  80. package/dist/chart.d.ts +25 -5
  81. package/dist/components/account-chips.d.ts +47 -7
  82. package/dist/components/account-chips.js +19 -3
  83. package/dist/components/account-chips.js.map +1 -1
  84. package/dist/components/account-settings-labels.d.ts +12 -1
  85. package/dist/components/account-settings-labels.js +4 -1
  86. package/dist/components/account-settings-labels.js.map +1 -1
  87. package/dist/components/account-settings.d.ts +61 -6
  88. package/dist/components/account-settings.js +115 -15
  89. package/dist/components/account-settings.js.map +1 -1
  90. package/dist/components/amount-input.d.ts +26 -5
  91. package/dist/components/button-group.d.ts +26 -5
  92. package/dist/components/calculator.d.ts +26 -5
  93. package/dist/components/confirm-dialog.d.ts +26 -5
  94. package/dist/components/copy-button.d.ts +23 -2
  95. package/dist/components/danger-confirm.d.ts +26 -5
  96. package/dist/components/facing-pair.d.ts +26 -5
  97. package/dist/components/file-button.d.ts +26 -5
  98. package/dist/components/file-dropzone.d.ts +26 -5
  99. package/dist/components/form-actions.d.ts +26 -5
  100. package/dist/components/iban-input.d.ts +26 -5
  101. package/dist/components/language-select.d.ts +26 -12
  102. package/dist/components/legal-page.d.ts +26 -5
  103. package/dist/components/legal.d.ts +26 -5
  104. package/dist/components/money-field.d.ts +26 -5
  105. package/dist/components/number-field.d.ts +26 -5
  106. package/dist/components/number-input.d.ts +26 -5
  107. package/dist/components/numpad-sheet.d.ts +26 -5
  108. package/dist/components/phone-input.d.ts +26 -5
  109. package/dist/components/series-chart.d.ts +26 -5
  110. package/dist/components/settings-fields.d.ts +25 -7
  111. package/dist/components/share-card.d.ts +25 -4
  112. package/dist/components/text-link.d.ts +26 -5
  113. package/dist/components/time-input.d.ts +26 -5
  114. package/dist/components/ui.d.ts +23 -2
  115. package/dist/components/user-avatar.d.ts +16 -2
  116. package/dist/components/user-avatar.js +7 -3
  117. package/dist/components/user-avatar.js.map +1 -1
  118. package/dist/{kit-labels-Du0uHBq4.d.ts → delete-account-setting-CGVMckMw.d.ts} +6654 -5302
  119. package/dist/feedback/feedback-attachment.d.ts +26 -5
  120. package/dist/feedback/feedback-context.d.ts +26 -5
  121. package/dist/feedback/feedback-crash.d.ts +26 -5
  122. package/dist/feedback/feedback-dialog.d.ts +26 -5
  123. package/dist/feedback/feedback-inbox.d.ts +26 -5
  124. package/dist/feedback/feedback-labels.d.ts +26 -5
  125. package/dist/feedback/feedback-menu.d.ts +26 -5
  126. package/dist/feedback/feedback-record.d.ts +26 -5
  127. package/dist/feedback/feedback-row-detail.d.ts +26 -5
  128. package/dist/feedback/feedback-status-undo.d.ts +26 -5
  129. package/dist/feedback/feedback-submit.d.ts +26 -5
  130. package/dist/feedback/feedback-swipe.d.ts +26 -5
  131. package/dist/feedback/feedback-table.d.ts +23 -2
  132. package/dist/feedback/feedback-thread.d.ts +26 -5
  133. package/dist/feedback.d.ts +26 -5
  134. package/dist/hooks/use-anchored-panel.d.ts +6 -2
  135. package/dist/hooks/use-anchored-panel.js +2 -1
  136. package/dist/hooks/use-anchored-panel.js.map +1 -1
  137. package/dist/hooks/use-file-drop.d.ts +26 -5
  138. package/dist/i18n/defaults.d.ts +26 -5
  139. package/dist/i18n/defaults.js +40 -0
  140. package/dist/i18n/defaults.js.map +1 -1
  141. package/dist/i18n/german.d.ts +26 -5
  142. package/dist/i18n/german.js +343 -2
  143. package/dist/i18n/german.js.map +1 -1
  144. package/dist/i18n/kit-labels.d.ts +23 -2
  145. package/dist/i18n/kit-labels.js.map +1 -1
  146. package/dist/i18n/languages.d.ts +21 -204
  147. package/dist/i18n/locales/de-CH.d.ts +26 -5
  148. package/dist/i18n/locales/en.d.ts +26 -5
  149. package/dist/i18n/locales/es.d.ts +26 -5
  150. package/dist/i18n/locales/es.js +337 -3
  151. package/dist/i18n/locales/es.js.map +1 -1
  152. package/dist/i18n/locales/fr.d.ts +26 -5
  153. package/dist/i18n/locales/fr.js +344 -2
  154. package/dist/i18n/locales/fr.js.map +1 -1
  155. package/dist/i18n/locales/hu.d.ts +26 -5
  156. package/dist/i18n/locales/hu.js +337 -3
  157. package/dist/i18n/locales/hu.js.map +1 -1
  158. package/dist/i18n/locales/it.d.ts +26 -5
  159. package/dist/i18n/locales/it.js +342 -2
  160. package/dist/i18n/locales/it.js.map +1 -1
  161. package/dist/i18n/locales/zh.d.ts +26 -5
  162. package/dist/i18n/locales/zh.js +333 -2
  163. package/dist/i18n/locales/zh.js.map +1 -1
  164. package/dist/i18n/review.d.ts +26 -5
  165. package/dist/i18n/review.js +45 -1
  166. package/dist/i18n/review.js.map +1 -1
  167. package/dist/index.d.ts +19 -3
  168. package/dist/index.js +89 -0
  169. package/dist/index.js.map +1 -1
  170. package/dist/lib/person-name.d.ts +69 -0
  171. package/dist/lib/person-name.js +65 -0
  172. package/dist/lib/person-name.js.map +1 -0
  173. package/dist/rhf/fields.d.ts +26 -5
  174. package/dist/rhf/form.d.ts +26 -5
  175. package/dist/rhf.d.ts +26 -5
  176. package/dist/shell/app-shell.d.ts +26 -5
  177. package/dist/shell/company-switcher.d.ts +59 -0
  178. package/dist/shell/company-switcher.js +85 -0
  179. package/dist/shell/company-switcher.js.map +1 -0
  180. package/dist/shell/top-bar-brand.d.ts +26 -5
  181. package/dist/shell/topbar-action-menu.d.ts +1 -0
  182. package/dist/shell.d.ts +20 -5
  183. package/dist/wizard/stepper-nav.d.ts +26 -5
  184. package/dist/wizard.d.ts +26 -5
  185. package/package.json +1 -1
  186. package/src/account/account-parts.ts +67 -0
  187. package/src/account/data-export-setting.tsx +218 -0
  188. package/src/account/delete-account-setting.tsx +362 -0
  189. package/src/account/email-change-setting.tsx +457 -0
  190. package/src/account/sessions-setting.tsx +283 -0
  191. package/src/admin/admin-action-confirm.tsx +495 -0
  192. package/src/admin/admin-action-log.tsx +297 -0
  193. package/src/admin/admin-parts.ts +82 -0
  194. package/src/admin/invitations-panel.tsx +516 -0
  195. package/src/admin/reviewer-scope-editor.tsx +234 -0
  196. package/src/admin/role-select.tsx +286 -0
  197. package/src/admin/transfer-ownership-dialog.tsx +172 -0
  198. package/src/admin/user-roster.tsx +573 -0
  199. package/src/auth/accept-invitation.tsx +227 -0
  200. package/src/auth/auth-errors.ts +262 -0
  201. package/src/auth/complete-name-dialog.tsx +253 -0
  202. package/src/auth/email-tag.ts +75 -0
  203. package/src/auth/forgot-password-form.tsx +229 -0
  204. package/src/auth/form-rules.ts +40 -0
  205. package/src/auth/new-password-fields.tsx +85 -0
  206. package/src/auth/not-found-page.tsx +98 -0
  207. package/src/auth/register-form.tsx +477 -0
  208. package/src/auth/reset-password-form.tsx +351 -0
  209. package/src/auth/sign-in-form.tsx +859 -0
  210. package/src/auth/status-parts.tsx +138 -0
  211. package/src/auth/verify-email.tsx +409 -0
  212. package/src/components/account-chips.tsx +64 -7
  213. package/src/components/account-settings-labels.ts +15 -1
  214. package/src/components/account-settings.tsx +199 -23
  215. package/src/components/user-avatar.tsx +22 -4
  216. package/src/hooks/use-anchored-panel.ts +14 -3
  217. package/src/i18n/defaults.ts +40 -0
  218. package/src/i18n/german.ts +379 -0
  219. package/src/i18n/kit-labels.tsx +60 -0
  220. package/src/i18n/locales/es.ts +371 -1
  221. package/src/i18n/locales/fr.ts +379 -0
  222. package/src/i18n/locales/hu.ts +366 -1
  223. package/src/i18n/locales/it.ts +373 -0
  224. package/src/i18n/locales/zh.ts +343 -0
  225. package/src/i18n/review.ts +44 -0
  226. package/src/index.ts +146 -0
  227. package/src/lib/person-name.ts +157 -0
  228. package/src/shell/company-switcher.tsx +140 -0
@@ -0,0 +1,457 @@
1
+ import { useEffect, useId, useRef, useState } from "react";
2
+ import type { ComponentPropsWithoutRef, FormEvent, ReactNode } from "react";
3
+ import { MailCheck } from "lucide-react";
4
+
5
+ import { DEFAULT_COMMON_LABELS, useKitLabels } from "../i18n/kit-labels";
6
+ import { cn } from "../lib/cn";
7
+ import { AlertBanner } from "../components/alert-banner";
8
+ import { hasMessage } from "../components/choice-parts";
9
+ import { CurrentPasswordInput } from "../components/danger-confirm";
10
+ import { Button, Card, Input } from "../components/ui";
11
+ import { authErrorCode, englishRateLimited, isRateLimited, retryAfterSeconds } from "../auth/auth-errors";
12
+ import { emailParts, taggedEmail } from "../auth/email-tag";
13
+ import { CARD_DESCRIPTION_CLASS, CARD_TITLE_CLASS, settle, useMounted } from "./account-parts";
14
+
15
+ /* ── Labels ──────────────────────────────────────────────────────────────── */
16
+
17
+ /** Every string {@link EmailChangeSetting} renders — the `emailChange` namespace of
18
+ * `<UiKitProvider labels>`, overridable per instance through `labels`. */
19
+ export interface EmailChangeLabels {
20
+ /** The card's title. */
21
+ title: string;
22
+ /** Names the address the account has now: "Current address: ada@example.com". */
23
+ current: string;
24
+ /** The new address's field. */
25
+ newEmail: string;
26
+ /** The password field. */
27
+ password: string;
28
+ /** Under the password field: why it is asked again. */
29
+ passwordHint: string;
30
+ /** The address-tag button (auth §4.5), given the exact address it would use. */
31
+ emailTagUse: (address: string) => string;
32
+ /** The line after the address-tag button. */
33
+ emailTagHint: string;
34
+ /** The submit button. */
35
+ submit: string;
36
+ /** Under the new address while it is the current one. */
37
+ sameAsCurrent: string;
38
+ /** `email_taken`, under the new address. */
39
+ emailTaken: string;
40
+ /** `password_incorrect`, under the password. */
41
+ wrongPassword: string;
42
+ /** The pending state, given the new address the link went to. */
43
+ pending: (newEmail: string) => string;
44
+ /** Under it, given the current address — which still signs in until the link is
45
+ * confirmed. */
46
+ pendingHint: (currentEmail: string) => string;
47
+ /** Mails the link again. */
48
+ resend: string;
49
+ /** Said once it went. */
50
+ resent: string;
51
+ /** Drops the change; the current address stays. */
52
+ cancel: string;
53
+ /** The done state (`confirmed`), given the address the account has now. */
54
+ confirmed: (email: string) => string;
55
+ /** Under it (`passkeyNote`): a passkey's label in the authenticator keeps the old
56
+ * address (§6.2). */
57
+ passkeyNote: string;
58
+ /** A request was throttled — HTTP 429 — given its `Retry-After` wait in seconds, or
59
+ * `undefined` without one. */
60
+ rateLimited: (seconds?: number) => string;
61
+ /** Any other failure, of any of the three requests. */
62
+ failed: string;
63
+ }
64
+
65
+ export const DEFAULT_EMAIL_CHANGE_LABELS: EmailChangeLabels = {
66
+ title: "Email address",
67
+ current: "Current address",
68
+ newEmail: "New email address",
69
+ password: "Current password",
70
+ passwordHint: "Your address is how you sign in, so changing it takes your password.",
71
+ emailTagUse: (address) => `Use ${address}`,
72
+ emailTagHint:
73
+ "Many providers deliver name+tag@… to the same inbox, so mail from this app is easy to filter and trace. Check yours does before you rely on it: you would sign in with the tagged address.",
74
+ submit: "Change email address",
75
+ sameAsCurrent: "This is already your address.",
76
+ emailTaken: "An account with this email address already exists.",
77
+ wrongPassword: "The password is incorrect.",
78
+ pending: (newEmail) => `Confirm the link we sent to ${newEmail}.`,
79
+ pendingHint: (currentEmail) =>
80
+ `Until you do, you keep signing in with ${currentEmail}. Check your spam folder too.`,
81
+ resend: "Send the link again",
82
+ resent: "We sent the link again.",
83
+ cancel: "Cancel the change",
84
+ confirmed: (email) => `Your email address is now ${email}.`,
85
+ passkeyNote: "Your passkeys keep working. Your device may still list them under your old address.",
86
+ rateLimited: englishRateLimited,
87
+ failed: "That didn’t work. Please try again.",
88
+ };
89
+
90
+ /* ── Props ───────────────────────────────────────────────────────────────── */
91
+
92
+ /** What {@link EmailChangeSettingProps.onRequest} gets: the `POST /auth/me/email` body. */
93
+ export interface EmailChangeValues {
94
+ /** Trimmed. A `+tag` stays (auth §4.5); lower-casing is the server's. */
95
+ newEmail: string;
96
+ /** As typed. */
97
+ password: string;
98
+ }
99
+
100
+ /** Which request failed — {@link EmailChangeSettingProps.describeError}'s second
101
+ * argument. */
102
+ export type EmailChangeAction = "request" | "resend" | "cancel";
103
+
104
+ export interface EmailChangeSettingProps extends Omit<ComponentPropsWithoutRef<"div">, "children" | "onSubmit"> {
105
+ /** The account's address now. */
106
+ currentEmail: string;
107
+ /**
108
+ * The address a change waits on, from the app's server state — so a reload, or
109
+ * another tab, shows the pending state. `null`: none, whatever the card did. Left out
110
+ * (`undefined`), the card keeps its own: pending from a resolved `onRequest` until a
111
+ * resolved `onCancel`.
112
+ */
113
+ pendingEmail?: string | null;
114
+ /**
115
+ * The form was submitted: send `POST /auth/me/email {new_email, password}` (§6.2).
116
+ * Resolve once the link is mailed — refresh `pendingEmail` first if the app passes
117
+ * it — and the card shows "Confirm the link we sent to …". Reject with the client's
118
+ * error: `email_taken` shows under the new address, `password_incorrect` (server-kit's
119
+ * `400` for a wrong current password; an older `invalid_credentials` too) under the
120
+ * password, a 429 as `rateLimited`, anything else as `failed` unless `describeError`
121
+ * has the app's words.
122
+ */
123
+ onRequest: (values: EmailChangeValues) => Promise<unknown> | void;
124
+ /** Mail the link again. Left out, no button. */
125
+ onResend?: () => Promise<unknown> | void;
126
+ /** Drop the pending change. Left out, no button. */
127
+ onCancel?: () => Promise<unknown> | void;
128
+ /**
129
+ * The change was just confirmed — the app's confirm route (`POST /auth/me/email/confirm`)
130
+ * came back here. Says "Your email address is now …" over the form, with
131
+ * `passkeyNote`.
132
+ */
133
+ confirmed?: boolean;
134
+ /** With `confirmed`: say that a passkey's label keeps the old address (§6.2). Default
135
+ * `true`; `false` for an account without passkeys. */
136
+ passkeyNote?: boolean;
137
+ /**
138
+ * The app's address tag (`"kastlan"`, auth §4.5): once the new address is plausible a
139
+ * button under it offers the tagged form — the rule of `RegisterForm`, since this is
140
+ * the person's own address. Never applied on its own. Left out, no offer.
141
+ */
142
+ emailTag?: string;
143
+ /** The app's own words for a failure, or `undefined` for the kit's. Asked first, for
144
+ * every failure (a demo account's 403, the device offline). */
145
+ describeError?: (error: unknown, action: EmailChangeAction) => ReactNode | undefined;
146
+ labels?: Partial<EmailChangeLabels>;
147
+ }
148
+
149
+ interface Failure {
150
+ action: EmailChangeAction;
151
+ message: ReactNode;
152
+ }
153
+
154
+ /**
155
+ * Changing the account's email address (docs/user-admin-harmonization.md §6.2, §2.5):
156
+ * the current address, the new one, the password — then a pending state until the new
157
+ * address confirms its link.
158
+ *
159
+ * **The new address must confirm before it takes effect.** The server mails a one-time
160
+ * link to the NEW address (and a notice to the old one); until it is opened, the account
161
+ * keeps the old address and signs in with it. So the card's answer to a request is not
162
+ * "changed" but "Confirm the link we sent to …", with "Send the link again" and "Cancel
163
+ * the change". The state is the server's: pass `pendingEmail` and a reload shows it too.
164
+ *
165
+ * **The password is asked again** because the address is the identity: whoever holds the
166
+ * session must not move the account to a mailbox of their own. It is a
167
+ * {@link CurrentPasswordInput}, so a password manager fills it and offers no new one.
168
+ *
169
+ * **The `+tag` offer** (`emailTag`) is RegisterForm's: the address is the person's own,
170
+ * so `you+kastlan@…` is offered under the field, applied only on a click.
171
+ *
172
+ * **It never sends a request** (auth §8): `onRequest`, `onResend` and `onCancel` are the
173
+ * app's, and their refusals are read by code — `email_taken` and `password_incorrect`
174
+ * under their fields, which take the focus; a 429 as "Too many attempts". A wrong
175
+ * password is a `400 password_incorrect`, never a 401: an app's client reads a 401 on a
176
+ * signed-in route as an ended session, and would sign the person out for a typo.
177
+ *
178
+ * **Done** (`confirmed`): "Your email address is now …" and, unless `passkeyNote` is
179
+ * off, that a passkey keeps its old label in the device's list — the authenticator
180
+ * stored the name it was given, and only a new passkey carries the new one.
181
+ */
182
+ export function EmailChangeSetting({
183
+ currentEmail,
184
+ pendingEmail,
185
+ onRequest,
186
+ onResend,
187
+ onCancel,
188
+ confirmed,
189
+ passkeyNote = true,
190
+ emailTag,
191
+ describeError,
192
+ labels: labelsProp,
193
+ className,
194
+ ...rest
195
+ }: EmailChangeSettingProps) {
196
+ const labels = useKitLabels("emailChange", DEFAULT_EMAIL_CHANGE_LABELS, labelsProp);
197
+ const common = useKitLabels("common", DEFAULT_COMMON_LABELS);
198
+ const [newEmail, setNewEmail] = useState("");
199
+ const [password, setPassword] = useState("");
200
+ const [ownPending, setOwnPending] = useState<string | null>(null);
201
+ const [busy, setBusy] = useState<EmailChangeAction | null>(null);
202
+ const [failure, setFailure] = useState<Failure | null>(null);
203
+ // `email_taken` belongs to the address it was answered for; another one clears it.
204
+ const [taken, setTaken] = useState<string | null>(null);
205
+ const [wrongPassword, setWrongPassword] = useState(false);
206
+ const [resent, setResent] = useState(false);
207
+ const mounted = useMounted();
208
+ const titleId = useId();
209
+ const tagHintId = useId();
210
+ const emailRef = useRef<HTMLInputElement>(null);
211
+ const passwordRef = useRef<HTMLInputElement>(null);
212
+ const pendingRef = useRef<HTMLDivElement>(null);
213
+
214
+ const pending = pendingEmail !== undefined ? pendingEmail : ownPending;
215
+ const isPending = pending !== null && pending !== "";
216
+
217
+ // The state swapped under the button that was pressed (the form for the pending
218
+ // message, or back): the focus goes to what came in, never down to <body>. Only after
219
+ // the person's own request — a pending state that arrives with the page takes nothing.
220
+ const focusNext = useRef<"pending" | "form" | null>(null);
221
+ useEffect(() => {
222
+ const target = focusNext.current;
223
+ if (target === "pending" && isPending) {
224
+ focusNext.current = null;
225
+ pendingRef.current?.focus();
226
+ } else if (target === "form" && !isPending) {
227
+ focusNext.current = null;
228
+ emailRef.current?.focus();
229
+ }
230
+ }, [isPending]);
231
+
232
+ const messageFor = (error: unknown, action: EmailChangeAction): ReactNode => {
233
+ const own = describeError?.(error, action);
234
+ if (hasMessage(own)) return own;
235
+ return isRateLimited(error) ? labels.rateLimited(retryAfterSeconds(error)) : labels.failed;
236
+ };
237
+
238
+ /* ── The pending state ── */
239
+ if (isPending) {
240
+ const run = (action: "resend" | "cancel", call: () => unknown) => {
241
+ if (busy) return;
242
+ setBusy(action);
243
+ setFailure(null);
244
+ setResent(false);
245
+ settle(call).then(
246
+ () => {
247
+ if (!mounted.current) return;
248
+ setBusy(null);
249
+ if (action === "resend") {
250
+ setResent(true);
251
+ return;
252
+ }
253
+ focusNext.current = "form";
254
+ setOwnPending(null);
255
+ },
256
+ (error: unknown) => {
257
+ if (!mounted.current) return;
258
+ setBusy(null);
259
+ setFailure({ action, message: messageFor(error, action) });
260
+ },
261
+ );
262
+ };
263
+ return (
264
+ <Card {...rest} data-state="pending" className={cn("p-4 space-y-3", className)}>
265
+ <div>
266
+ <div id={titleId} className={CARD_TITLE_CLASS}>
267
+ {labels.title}
268
+ </div>
269
+ <div className={cn(CARD_DESCRIPTION_CLASS, "break-all")}>{common.fieldValue(labels.current, currentEmail)}</div>
270
+ </div>
271
+ {/* Focusable, not a control: the request's answer is read out when it takes the
272
+ focus from the submit that went with the form. */}
273
+ <div ref={pendingRef} tabIndex={-1} className="flex gap-2 outline-none">
274
+ <MailCheck aria-hidden className="mt-0.5 size-4 shrink-0 text-[var(--success)]" />
275
+ <div className="min-w-0 space-y-1">
276
+ <p className="break-words text-sm">{labels.pending(pending)}</p>
277
+ <p className={CARD_DESCRIPTION_CLASS}>{labels.pendingHint(currentEmail)}</p>
278
+ </div>
279
+ </div>
280
+ {(onResend || onCancel) && (
281
+ <div className="flex flex-wrap items-center gap-2">
282
+ {onResend && (
283
+ <Button
284
+ type="button"
285
+ variant="secondary"
286
+ size="sm"
287
+ commit
288
+ pending={busy === "resend"}
289
+ disabled={busy === "cancel"}
290
+ onClick={() => run("resend", onResend)}
291
+ >
292
+ {labels.resend}
293
+ </Button>
294
+ )}
295
+ {onCancel && (
296
+ <Button
297
+ type="button"
298
+ variant="ghost"
299
+ size="sm"
300
+ commit
301
+ pending={busy === "cancel"}
302
+ disabled={busy === "resend"}
303
+ onClick={() => run("cancel", onCancel)}
304
+ >
305
+ {labels.cancel}
306
+ </Button>
307
+ )}
308
+ {/* Always in the tree, so the "sent again" that appears in it is announced
309
+ (VerifyEmailStatus's resend line). */}
310
+ <span role="status" className="text-xs text-[var(--success)]">
311
+ {resent ? labels.resent : null}
312
+ </span>
313
+ </div>
314
+ )}
315
+ {failure && (
316
+ <AlertBanner tone="danger" size="sm" role="alert">
317
+ {failure.message}
318
+ </AlertBanner>
319
+ )}
320
+ </Card>
321
+ );
322
+ }
323
+
324
+ /* ── The form ── */
325
+ const typed = newEmail.trim();
326
+ const same = typed !== "" && typed.toLowerCase() === currentEmail.trim().toLowerCase();
327
+ const working = busy !== null;
328
+ const canSubmit = emailParts(newEmail) !== null && !same && password !== "";
329
+ const suggestion = emailTag ? taggedEmail(newEmail, emailTag) : null;
330
+ const emailError = same ? labels.sameAsCurrent : taken !== null && taken === typed ? labels.emailTaken : undefined;
331
+
332
+ const submit = (e: FormEvent) => {
333
+ e.preventDefault();
334
+ if (!canSubmit || working) return;
335
+ const values: EmailChangeValues = { newEmail: typed, password };
336
+ setBusy("request");
337
+ setFailure(null);
338
+ setTaken(null);
339
+ setWrongPassword(false);
340
+ settle(() => onRequest(values)).then(
341
+ () => {
342
+ if (!mounted.current) return;
343
+ setBusy(null);
344
+ // The password has done its work; nothing of the form stays in state.
345
+ setNewEmail("");
346
+ setPassword("");
347
+ setResent(false);
348
+ focusNext.current = "pending";
349
+ setOwnPending(values.newEmail);
350
+ },
351
+ (error: unknown) => {
352
+ if (!mounted.current) return;
353
+ setBusy(null);
354
+ const own = describeError?.(error, "request");
355
+ if (hasMessage(own)) {
356
+ setFailure({ action: "request", message: own });
357
+ return;
358
+ }
359
+ if (isRateLimited(error)) {
360
+ setFailure({ action: "request", message: labels.rateLimited(retryAfterSeconds(error)) });
361
+ return;
362
+ }
363
+ const code = authErrorCode(error);
364
+ if (code === "email_taken") {
365
+ setTaken(values.newEmail);
366
+ emailRef.current?.focus();
367
+ return;
368
+ }
369
+ // `invalid_credentials` too, for a server that answered a wrong password so
370
+ // before server-kit 0.4.0 named `password_incorrect`.
371
+ if (code === "password_incorrect" || code === "invalid_credentials") {
372
+ setWrongPassword(true);
373
+ passwordRef.current?.focus();
374
+ return;
375
+ }
376
+ setFailure({ action: "request", message: labels.failed });
377
+ },
378
+ );
379
+ };
380
+
381
+ return (
382
+ <Card {...rest} data-state={confirmed ? "confirmed" : "form"} className={cn("p-4 space-y-3", className)}>
383
+ <div>
384
+ <div id={titleId} className={CARD_TITLE_CLASS}>
385
+ {labels.title}
386
+ </div>
387
+ <div className={cn(CARD_DESCRIPTION_CLASS, "break-all")}>{common.fieldValue(labels.current, currentEmail)}</div>
388
+ </div>
389
+ {confirmed && (
390
+ <AlertBanner tone="success" size="sm">
391
+ <span className="block">{labels.confirmed(currentEmail)}</span>
392
+ {passkeyNote && <span className="mt-1 block">{labels.passkeyNote}</span>}
393
+ </AlertBanner>
394
+ )}
395
+ <form className="space-y-3" aria-labelledby={titleId} onSubmit={submit} noValidate>
396
+ <div>
397
+ <Input
398
+ ref={emailRef}
399
+ type="email"
400
+ name="email"
401
+ autoComplete="email"
402
+ label={labels.newEmail}
403
+ value={newEmail}
404
+ onChange={(e) => {
405
+ setNewEmail(e.target.value);
406
+ if (failure) setFailure(null);
407
+ }}
408
+ readOnly={working}
409
+ error={emailError}
410
+ required
411
+ />
412
+ {/* RegisterForm's offer (auth §4.5): one click, the exact address on the
413
+ button, reversible by typing. */}
414
+ {suggestion && (
415
+ <p className="mt-1 text-xs text-[var(--text-muted)]">
416
+ <Button
417
+ type="button"
418
+ variant="link"
419
+ size="sm"
420
+ className="h-auto p-0 align-baseline"
421
+ disabled={working}
422
+ aria-describedby={tagHintId}
423
+ onClick={() => setNewEmail(suggestion)}
424
+ >
425
+ {labels.emailTagUse(suggestion)}
426
+ </Button>{" "}
427
+ <span id={tagHintId}>{labels.emailTagHint}</span>
428
+ </p>
429
+ )}
430
+ </div>
431
+ <CurrentPasswordInput
432
+ ref={passwordRef}
433
+ name="password"
434
+ label={labels.password}
435
+ value={password}
436
+ onValueChange={(next) => {
437
+ setPassword(next);
438
+ setWrongPassword(false);
439
+ if (failure) setFailure(null);
440
+ }}
441
+ hint={labels.passwordHint}
442
+ error={wrongPassword ? labels.wrongPassword : undefined}
443
+ busy={working}
444
+ required
445
+ />
446
+ {failure && (
447
+ <AlertBanner tone="danger" size="sm" role="alert">
448
+ {failure.message}
449
+ </AlertBanner>
450
+ )}
451
+ <Button type="submit" commit disabled={!working && !canSubmit} pending={working}>
452
+ {labels.submit}
453
+ </Button>
454
+ </form>
455
+ </Card>
456
+ );
457
+ }