@rapidmx/web-client 0.8.0 → 0.10.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 (247) hide show
  1. package/README.md +192 -88
  2. package/apps/shared/auth/accountUrl.ts +9 -0
  3. package/apps/shared/auth/adminAccess.ts +99 -0
  4. package/apps/shared/components/admin/elevation.ts +61 -0
  5. package/apps/shared/components/admin/layout/AdminShell.tsx +73 -7
  6. package/apps/shared/components/admin/settings/BrandingForm.tsx +2 -1
  7. package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +87 -24
  8. package/apps/shared/components/calendar/layout/CalendarShell.tsx +8 -2
  9. package/apps/shared/components/contacts/ContactsToolbar.tsx +125 -115
  10. package/apps/shared/components/contacts/layout/ContactsShell.tsx +15 -3
  11. package/apps/shared/components/layout/AppShell.tsx +411 -288
  12. package/apps/shared/components/layout/BrandingChrome.tsx +3 -2
  13. package/apps/shared/components/layout/KeyEnrollmentGate.tsx +398 -389
  14. package/apps/shared/components/layout/MailboxProvisioning.tsx +168 -126
  15. package/apps/shared/components/layout/UserMenu.tsx +303 -150
  16. package/apps/shared/components/mail/ConversationList.tsx +269 -253
  17. package/apps/shared/components/mail/ConversationThreadPane.tsx +475 -434
  18. package/apps/shared/components/mail/LazyReadingPane.tsx +115 -0
  19. package/apps/shared/components/mail/MailAddress.tsx +88 -0
  20. package/apps/shared/components/mail/MailSelectionBar.tsx +235 -222
  21. package/apps/shared/components/mail/MessageDetailPane.tsx +1458 -1383
  22. package/apps/shared/components/mail/NewMailToasts.tsx +141 -0
  23. package/apps/shared/components/mail/compose/ComposeContext.tsx +279 -160
  24. package/apps/shared/components/mail/compose/ComposeToolbar.tsx +422 -418
  25. package/apps/shared/components/mail/compose/ComposeWindow.tsx +1842 -1700
  26. package/apps/shared/components/mail/compose/ComposeWindowPlaceholder.tsx +111 -0
  27. package/apps/shared/components/mail/compose/RichTextEditor.tsx +147 -118
  28. package/apps/shared/components/mail/compose/SendFailureAlert.tsx +48 -0
  29. package/apps/shared/components/mail/compose/composePerf.ts +46 -0
  30. package/apps/shared/components/mail/compose/quotedBody.ts +161 -100
  31. package/apps/shared/components/mail/layout/MailShell.tsx +476 -445
  32. package/apps/shared/components/mail/unreadStyle.tsx +75 -0
  33. package/apps/shared/components/settings/layout/SettingsShell.tsx +252 -240
  34. package/apps/shared/components/tasks/layout/TasksShell.tsx +15 -3
  35. package/apps/shared/keyboard/GlobalShortcuts.tsx +51 -0
  36. package/apps/shared/keyboard/ShortcutProvider.tsx +62 -0
  37. package/apps/shared/keyboard/ShortcutsDialog.tsx +84 -0
  38. package/apps/shared/keyboard/dispatch.ts +124 -0
  39. package/apps/shared/keyboard/format.ts +89 -0
  40. package/apps/shared/keyboard/keymap.ts +114 -0
  41. package/apps/shared/keyboard/match.ts +44 -0
  42. package/apps/shared/keyboard/parse.ts +136 -0
  43. package/apps/shared/keyboard/platform.ts +34 -0
  44. package/apps/shared/keyboard/registry.ts +65 -0
  45. package/apps/shared/keyboard/targets.ts +79 -0
  46. package/apps/shared/keyboard/useShortcut.ts +50 -0
  47. package/apps/shared/keyboard/useShortcutProps.ts +17 -0
  48. package/apps/shared/mail/folderCounts.ts +302 -0
  49. package/apps/shared/mail/listSnapshots.ts +87 -0
  50. package/apps/shared/mail/mergeFirstPage.ts +47 -0
  51. package/apps/shared/mail/messageReadState.ts +85 -0
  52. package/apps/shared/mail/newMailNotifications.ts +183 -0
  53. package/apps/shared/mail/useMailConnection.ts +150 -0
  54. package/apps/shared/mail/useMailLiveUpdates.ts +232 -0
  55. package/apps/shared/mail/useMarkMessageRead.ts +47 -0
  56. package/apps/shared/mail/useNewMailNotifications.ts +163 -0
  57. package/apps/shared/mail/useUnreadTitle.ts +42 -0
  58. package/apps/shared/navigation/AppRouter.tsx +300 -0
  59. package/apps/shared/navigation/appHrefs.ts +23 -0
  60. package/apps/shared/navigation/frameContext.tsx +35 -0
  61. package/apps/shared/navigation/idle.ts +45 -0
  62. package/apps/shared/navigation/routerContext.tsx +83 -0
  63. package/apps/shared/navigation/routes.ts +72 -0
  64. package/apps/shared/search/LocalIndexLifecycle.tsx +114 -98
  65. package/apps/shared/styles/app.css +28 -10
  66. package/apps/www/_routedPage.tsx +24 -0
  67. package/apps/www/_routes.ts +35 -0
  68. package/apps/www/calendar/index.tsx +55 -19
  69. package/apps/www/contacts/[uid].tsx +112 -107
  70. package/apps/www/contacts/index.tsx +584 -567
  71. package/apps/www/index.tsx +2280 -1854
  72. package/apps/www/messages/[uid].tsx +106 -101
  73. package/apps/www/settings/auto-reply/index.tsx +4 -1
  74. package/apps/www/settings/encryption/index.tsx +1252 -1249
  75. package/apps/www/settings/filters/[uid].tsx +4 -1
  76. package/apps/www/settings/filters/index.tsx +102 -99
  77. package/apps/www/settings/filters/new/index.tsx +138 -133
  78. package/apps/www/settings/labels/index.tsx +204 -201
  79. package/apps/www/settings/privacy/index.tsx +4 -1
  80. package/apps/www/settings/read-receipts/index.tsx +4 -1
  81. package/apps/www/settings/sharing/index.tsx +277 -274
  82. package/apps/www/settings/signatures/[uid].tsx +170 -167
  83. package/apps/www/settings/signatures/index.tsx +88 -85
  84. package/apps/www/settings/signatures/new/index.tsx +134 -129
  85. package/apps/www/tasks/index.tsx +18 -2
  86. package/dist/apps/shared/auth/accountUrl.d.ts +2 -0
  87. package/dist/apps/shared/auth/accountUrl.js +8 -0
  88. package/dist/apps/shared/auth/adminAccess.d.ts +30 -0
  89. package/dist/apps/shared/auth/adminAccess.js +89 -0
  90. package/dist/apps/shared/components/admin/elevation.d.ts +24 -0
  91. package/dist/apps/shared/components/admin/elevation.js +56 -0
  92. package/dist/apps/shared/components/admin/layout/AdminShell.d.ts +12 -4
  93. package/dist/apps/shared/components/admin/layout/AdminShell.js +49 -6
  94. package/dist/apps/shared/components/admin/settings/BrandingForm.js +1 -1
  95. package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +31 -13
  96. package/dist/apps/shared/components/calendar/layout/CalendarShell.d.ts +1 -1
  97. package/dist/apps/shared/components/calendar/layout/CalendarShell.js +8 -4
  98. package/dist/apps/shared/components/contacts/ContactsToolbar.d.ts +3 -1
  99. package/dist/apps/shared/components/contacts/ContactsToolbar.js +7 -4
  100. package/dist/apps/shared/components/contacts/layout/ContactsShell.d.ts +1 -1
  101. package/dist/apps/shared/components/contacts/layout/ContactsShell.js +15 -5
  102. package/dist/apps/shared/components/layout/AppShell.d.ts +29 -4
  103. package/dist/apps/shared/components/layout/AppShell.js +73 -14
  104. package/dist/apps/shared/components/layout/BrandingChrome.d.ts +3 -2
  105. package/dist/apps/shared/components/layout/BrandingChrome.js +3 -2
  106. package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +10 -9
  107. package/dist/apps/shared/components/layout/MailboxProvisioning.d.ts +5 -2
  108. package/dist/apps/shared/components/layout/MailboxProvisioning.js +45 -8
  109. package/dist/apps/shared/components/layout/UserMenu.d.ts +28 -9
  110. package/dist/apps/shared/components/layout/UserMenu.js +87 -14
  111. package/dist/apps/shared/components/mail/ConversationList.js +9 -13
  112. package/dist/apps/shared/components/mail/ConversationThreadPane.d.ts +4 -1
  113. package/dist/apps/shared/components/mail/ConversationThreadPane.js +53 -28
  114. package/dist/apps/shared/components/mail/LazyReadingPane.d.ts +9 -0
  115. package/dist/apps/shared/components/mail/LazyReadingPane.js +82 -0
  116. package/dist/apps/shared/components/mail/MailAddress.d.ts +27 -0
  117. package/dist/apps/shared/components/mail/MailAddress.js +39 -0
  118. package/dist/apps/shared/components/mail/MailSelectionBar.d.ts +4 -1
  119. package/dist/apps/shared/components/mail/MailSelectionBar.js +11 -3
  120. package/dist/apps/shared/components/mail/MessageDetailPane.d.ts +7 -1
  121. package/dist/apps/shared/components/mail/MessageDetailPane.js +87 -36
  122. package/dist/apps/shared/components/mail/NewMailToasts.d.ts +19 -0
  123. package/dist/apps/shared/components/mail/NewMailToasts.js +55 -0
  124. package/dist/apps/shared/components/mail/compose/ComposeContext.d.ts +30 -0
  125. package/dist/apps/shared/components/mail/compose/ComposeContext.js +71 -5
  126. package/dist/apps/shared/components/mail/compose/ComposeToolbar.js +4 -3
  127. package/dist/apps/shared/components/mail/compose/ComposeWindow.d.ts +1 -1
  128. package/dist/apps/shared/components/mail/compose/ComposeWindow.js +122 -27
  129. package/dist/apps/shared/components/mail/compose/ComposeWindowPlaceholder.d.ts +19 -0
  130. package/dist/apps/shared/components/mail/compose/ComposeWindowPlaceholder.js +33 -0
  131. package/dist/apps/shared/components/mail/compose/RichTextEditor.d.ts +9 -1
  132. package/dist/apps/shared/components/mail/compose/RichTextEditor.js +20 -2
  133. package/dist/apps/shared/components/mail/compose/SendFailureAlert.d.ts +14 -0
  134. package/dist/apps/shared/components/mail/compose/SendFailureAlert.js +11 -0
  135. package/dist/apps/shared/components/mail/compose/composePerf.d.ts +16 -0
  136. package/dist/apps/shared/components/mail/compose/composePerf.js +41 -0
  137. package/dist/apps/shared/components/mail/compose/quotedBody.d.ts +13 -0
  138. package/dist/apps/shared/components/mail/compose/quotedBody.js +67 -11
  139. package/dist/apps/shared/components/mail/layout/MailShell.d.ts +19 -4
  140. package/dist/apps/shared/components/mail/layout/MailShell.js +85 -85
  141. package/dist/apps/shared/components/mail/unreadStyle.d.ts +45 -0
  142. package/dist/apps/shared/components/mail/unreadStyle.js +61 -0
  143. package/dist/apps/shared/components/settings/layout/SettingsShell.d.ts +1 -1
  144. package/dist/apps/shared/components/settings/layout/SettingsShell.js +15 -5
  145. package/dist/apps/shared/components/tasks/layout/TasksShell.d.ts +1 -1
  146. package/dist/apps/shared/components/tasks/layout/TasksShell.js +15 -5
  147. package/dist/apps/shared/keyboard/GlobalShortcuts.d.ts +14 -0
  148. package/dist/apps/shared/keyboard/GlobalShortcuts.js +37 -0
  149. package/dist/apps/shared/keyboard/ShortcutProvider.d.ts +20 -0
  150. package/dist/apps/shared/keyboard/ShortcutProvider.js +52 -0
  151. package/dist/apps/shared/keyboard/ShortcutsDialog.d.ts +14 -0
  152. package/dist/apps/shared/keyboard/ShortcutsDialog.js +42 -0
  153. package/dist/apps/shared/keyboard/dispatch.d.ts +16 -0
  154. package/dist/apps/shared/keyboard/dispatch.js +109 -0
  155. package/dist/apps/shared/keyboard/format.d.ts +12 -0
  156. package/dist/apps/shared/keyboard/format.js +74 -0
  157. package/dist/apps/shared/keyboard/keymap.d.ts +294 -0
  158. package/dist/apps/shared/keyboard/keymap.js +84 -0
  159. package/dist/apps/shared/keyboard/match.d.ts +20 -0
  160. package/dist/apps/shared/keyboard/match.js +31 -0
  161. package/dist/apps/shared/keyboard/parse.d.ts +31 -0
  162. package/dist/apps/shared/keyboard/parse.js +107 -0
  163. package/dist/apps/shared/keyboard/platform.d.ts +15 -0
  164. package/dist/apps/shared/keyboard/platform.js +22 -0
  165. package/dist/apps/shared/keyboard/registry.d.ts +39 -0
  166. package/dist/apps/shared/keyboard/registry.js +33 -0
  167. package/dist/apps/shared/keyboard/targets.d.ts +14 -0
  168. package/dist/apps/shared/keyboard/targets.js +67 -0
  169. package/dist/apps/shared/keyboard/useShortcut.d.ts +19 -0
  170. package/dist/apps/shared/keyboard/useShortcut.js +35 -0
  171. package/dist/apps/shared/keyboard/useShortcutProps.d.ts +10 -0
  172. package/dist/apps/shared/keyboard/useShortcutProps.js +15 -0
  173. package/dist/apps/shared/mail/folderCounts.d.ts +78 -0
  174. package/dist/apps/shared/mail/folderCounts.js +212 -0
  175. package/dist/apps/shared/mail/listSnapshots.d.ts +46 -0
  176. package/dist/apps/shared/mail/listSnapshots.js +43 -0
  177. package/dist/apps/shared/mail/mergeFirstPage.d.ts +23 -0
  178. package/dist/apps/shared/mail/mergeFirstPage.js +30 -0
  179. package/dist/apps/shared/mail/messageReadState.d.ts +32 -0
  180. package/dist/apps/shared/mail/messageReadState.js +63 -0
  181. package/dist/apps/shared/mail/newMailNotifications.d.ts +62 -0
  182. package/dist/apps/shared/mail/newMailNotifications.js +138 -0
  183. package/dist/apps/shared/mail/useMailConnection.d.ts +55 -0
  184. package/dist/apps/shared/mail/useMailConnection.js +96 -0
  185. package/dist/apps/shared/mail/useMailLiveUpdates.d.ts +55 -0
  186. package/dist/apps/shared/mail/useMailLiveUpdates.js +182 -0
  187. package/dist/apps/shared/mail/useMarkMessageRead.d.ts +12 -0
  188. package/dist/apps/shared/mail/useMarkMessageRead.js +44 -0
  189. package/dist/apps/shared/mail/useNewMailNotifications.d.ts +41 -0
  190. package/dist/apps/shared/mail/useNewMailNotifications.js +110 -0
  191. package/dist/apps/shared/mail/useUnreadTitle.d.ts +18 -0
  192. package/dist/apps/shared/mail/useUnreadTitle.js +31 -0
  193. package/dist/apps/shared/navigation/AppRouter.d.ts +52 -0
  194. package/dist/apps/shared/navigation/AppRouter.js +242 -0
  195. package/dist/apps/shared/navigation/appHrefs.d.ts +14 -0
  196. package/dist/apps/shared/navigation/appHrefs.js +20 -0
  197. package/dist/apps/shared/navigation/frameContext.d.ts +19 -0
  198. package/dist/apps/shared/navigation/frameContext.js +24 -0
  199. package/dist/apps/shared/navigation/idle.d.ts +14 -0
  200. package/dist/apps/shared/navigation/idle.js +43 -0
  201. package/dist/apps/shared/navigation/routerContext.d.ts +37 -0
  202. package/dist/apps/shared/navigation/routerContext.js +56 -0
  203. package/dist/apps/shared/navigation/routes.d.ts +32 -0
  204. package/dist/apps/shared/navigation/routes.js +37 -0
  205. package/dist/apps/shared/search/LocalIndexLifecycle.js +17 -3
  206. package/dist/apps/shared/styles/app.css +28 -10
  207. package/dist/apps/www/_routedPage.d.ts +12 -0
  208. package/dist/apps/www/_routedPage.js +19 -0
  209. package/dist/apps/www/_routes.d.ts +11 -0
  210. package/dist/apps/www/_routes.js +29 -0
  211. package/dist/apps/www/calendar/index.d.ts +2 -2
  212. package/dist/apps/www/calendar/index.js +39 -8
  213. package/dist/apps/www/contacts/[uid].d.ts +3 -9
  214. package/dist/apps/www/contacts/[uid].js +6 -2
  215. package/dist/apps/www/contacts/index.d.ts +2 -2
  216. package/dist/apps/www/contacts/index.js +17 -4
  217. package/dist/apps/www/index.d.ts +2 -2
  218. package/dist/apps/www/index.js +361 -34
  219. package/dist/apps/www/messages/[uid].d.ts +3 -7
  220. package/dist/apps/www/messages/[uid].js +7 -4
  221. package/dist/apps/www/settings/auto-reply/index.d.ts +2 -2
  222. package/dist/apps/www/settings/auto-reply/index.js +3 -1
  223. package/dist/apps/www/settings/encryption/index.d.ts +2 -2
  224. package/dist/apps/www/settings/encryption/index.js +3 -1
  225. package/dist/apps/www/settings/filters/[uid].d.ts +2 -2
  226. package/dist/apps/www/settings/filters/[uid].js +3 -1
  227. package/dist/apps/www/settings/filters/index.d.ts +2 -2
  228. package/dist/apps/www/settings/filters/index.js +3 -1
  229. package/dist/apps/www/settings/filters/new/index.d.ts +2 -2
  230. package/dist/apps/www/settings/filters/new/index.js +6 -2
  231. package/dist/apps/www/settings/labels/index.d.ts +2 -2
  232. package/dist/apps/www/settings/labels/index.js +3 -1
  233. package/dist/apps/www/settings/privacy/index.d.ts +2 -2
  234. package/dist/apps/www/settings/privacy/index.js +3 -1
  235. package/dist/apps/www/settings/read-receipts/index.d.ts +2 -2
  236. package/dist/apps/www/settings/read-receipts/index.js +3 -1
  237. package/dist/apps/www/settings/sharing/index.d.ts +2 -2
  238. package/dist/apps/www/settings/sharing/index.js +3 -1
  239. package/dist/apps/www/settings/signatures/[uid].d.ts +2 -2
  240. package/dist/apps/www/settings/signatures/[uid].js +3 -1
  241. package/dist/apps/www/settings/signatures/index.d.ts +2 -2
  242. package/dist/apps/www/settings/signatures/index.js +3 -1
  243. package/dist/apps/www/settings/signatures/new/index.d.ts +2 -2
  244. package/dist/apps/www/settings/signatures/new/index.js +6 -2
  245. package/dist/apps/www/tasks/index.d.ts +2 -2
  246. package/dist/apps/www/tasks/index.js +15 -3
  247. package/package.json +2 -2
@@ -0,0 +1,110 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
6
+ import { desktopPermission, getDesktopOfferDismissed, getNewMailPopupsEnabled, noticeFor, noticeSender, ownAddressesOf, requestDesktopPermission, setDesktopOfferDismissed, shouldAnnounce, } from "./newMailNotifications.js";
7
+ /** The most pop-ups on screen at once; a new one pushes the oldest off. */
8
+ export const MAX_TOASTS = 3;
9
+ /** How long a pop-up stays before it goes by itself (while it is neither hovered nor focused, and the tab is in view). */
10
+ export const TOAST_DURATION_MS = 8000;
11
+ /** At most this many desktop notifications in `DESKTOP_BURST_WINDOW_MS`: a flood of mail is a few notifications, not a hundred. */
12
+ export const DESKTOP_BURST_LIMIT = 5;
13
+ export const DESKTOP_BURST_WINDOW_MS = 30000;
14
+ /** How many announced message uids are remembered, so an event delivered twice is announced once. */
15
+ const ANNOUNCED_LIMIT = 500;
16
+ /** Whether the user is not looking at this tab: it is hidden, or another window has the focus. */
17
+ function tabIsInBackground() {
18
+ return document.visibilityState === "hidden" || !document.hasFocus();
19
+ }
20
+ function defaultOpen(href) {
21
+ window.location.href = href;
22
+ }
23
+ /**
24
+ * New-mail announcements: an in-app pop-up for each message that arrives (`MAX_TOASTS` at most on screen), and - while the tab
25
+ * is in the background and the user has allowed it - a desktop notification with the same content, one per message however
26
+ * many events name it. `announce()` is what `useMailLiveUpdates()`'s `onMessageCreated` calls; nothing else feeds it, so a
27
+ * page load, a list refetch and a reconnect never announce anything.
28
+ *
29
+ * Nothing is ever asked of the browser on load: permission is requested only by `enableDesktop()`, from the offer in the
30
+ * first pop-up, or from Settings. What the user answered is remembered in `localStorage` (see `newMailNotifications.ts`).
31
+ */
32
+ export function useNewMailNotifications({ mailboxes, mailboxFolders, open = defaultOpen }) {
33
+ const [toasts, setToasts] = useState([]);
34
+ const [permission, setPermission] = useState("unsupported");
35
+ const [offerDismissed, setOfferDismissed] = useState(true);
36
+ const folders = useMemo(() => mailboxFolders.flatMap((entry) => entry.folders), [mailboxFolders]);
37
+ const ownAddresses = useMemo(() => ownAddressesOf(mailboxes), [mailboxes]);
38
+ const latestRef = useRef({ folders, ownAddresses, open });
39
+ latestRef.current = { folders, ownAddresses, open };
40
+ const announcedRef = useRef(new Set());
41
+ const desktopShownRef = useRef([]);
42
+ // The browser's own state is only read on the client, after hydration, so the server and first client render agree.
43
+ useEffect(() => {
44
+ setPermission(desktopPermission());
45
+ setOfferDismissed(getDesktopOfferDismissed());
46
+ }, []);
47
+ const dismiss = useCallback((uid) => {
48
+ setToasts((previous) => previous.filter((toast) => toast.uid !== uid));
49
+ }, []);
50
+ const showDesktop = useCallback((notice) => {
51
+ const now = Date.now();
52
+ desktopShownRef.current = desktopShownRef.current.filter((at) => now - at < DESKTOP_BURST_WINDOW_MS);
53
+ if (desktopShownRef.current.length >= DESKTOP_BURST_LIMIT) {
54
+ return;
55
+ }
56
+ try {
57
+ const notification = new Notification(noticeSender(notice), {
58
+ body: [notice.subject, notice.preview].filter(Boolean).join("\n"),
59
+ // One per message: a duplicate (another tab of this app, a re-delivered event) replaces instead of stacking.
60
+ tag: notice.uid,
61
+ });
62
+ desktopShownRef.current.push(now);
63
+ notification.onclick = () => {
64
+ window.focus();
65
+ notification.close();
66
+ latestRef.current.open(notice.href);
67
+ };
68
+ }
69
+ catch {
70
+ // Some browsers refuse `new Notification()` outright (a mobile one wanting a service worker): the pop-up is enough.
71
+ }
72
+ }, []);
73
+ const announce = useCallback((message) => {
74
+ if (!getNewMailPopupsEnabled() || announcedRef.current.has(message.uid)) {
75
+ return;
76
+ }
77
+ const { folders: knownFolders, ownAddresses: own } = latestRef.current;
78
+ if (!shouldAnnounce(message, { folders: knownFolders, ownAddresses: own })) {
79
+ return;
80
+ }
81
+ announcedRef.current.add(message.uid);
82
+ if (announcedRef.current.size > ANNOUNCED_LIMIT) {
83
+ announcedRef.current.delete(announcedRef.current.values().next().value);
84
+ }
85
+ const notice = noticeFor(message);
86
+ setToasts((previous) => [...previous.filter((toast) => toast.uid !== notice.uid), notice].slice(-MAX_TOASTS));
87
+ if (desktopPermission() === "granted" && tabIsInBackground()) {
88
+ showDesktop(notice);
89
+ }
90
+ }, [showDesktop]);
91
+ const enableDesktop = useCallback(async () => {
92
+ const answer = await requestDesktopPermission();
93
+ setPermission(answer);
94
+ // Answered either way, so the offer goes: on granted it has done its job, on denied it can't be made again.
95
+ setDesktopOfferDismissed(true);
96
+ setOfferDismissed(true);
97
+ }, []);
98
+ const declineDesktop = useCallback(() => {
99
+ setDesktopOfferDismissed(true);
100
+ setOfferDismissed(true);
101
+ }, []);
102
+ return {
103
+ toasts,
104
+ dismiss,
105
+ offerDesktop: permission === "default" && !offerDismissed,
106
+ enableDesktop,
107
+ declineDesktop,
108
+ announce,
109
+ };
110
+ }
@@ -0,0 +1,18 @@
1
+ /** `title` with `count` unread in front - `(3) Acme: Mail` - or as it is when there are none. */
2
+ export declare function titleWithUnread(title: string, count: number): string;
3
+ export interface UseUnreadTitleOptions {
4
+ /** Does nothing while false - for the one of two owners that isn't. Default true. */
5
+ enabled?: boolean;
6
+ /** Changes whenever something else has rewritten the title (the app frame sets it on every page change), so the count is put in front of
7
+ * the new one. */
8
+ resetKey?: unknown;
9
+ }
10
+ /**
11
+ * Keeps the browser tab's title in step with the unread count, as Outlook on the web does - `(3) Acme: Mail` - so mail that
12
+ * arrives while the tab is in the background can be seen from the tab strip. The count is put in front of whatever the title is
13
+ * (the page sets its own, from the branding) and taken off again when it drops to zero or the component goes away.
14
+ *
15
+ * Order matters when the same component also sets the title: declare this hook after the effect that does, so that on a change of `resetKey`
16
+ * the count is taken off the old title, the new title is written, and the count goes back in front of it.
17
+ */
18
+ export declare function useUnreadTitle(count: number, { enabled, resetKey }?: UseUnreadTitleOptions): void;
@@ -0,0 +1,31 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ import { useEffect } from "react";
6
+ /** A count this hook put in front of the title, so it can be taken off again: `(3) `. */
7
+ const TITLE_PREFIX = /^\(\d+\) /;
8
+ /** `title` with `count` unread in front - `(3) Acme: Mail` - or as it is when there are none. */
9
+ export function titleWithUnread(title, count) {
10
+ const base = title.replace(TITLE_PREFIX, "");
11
+ return count > 0 ? `(${count > 99 ? "99+" : count}) ${base}` : base;
12
+ }
13
+ /**
14
+ * Keeps the browser tab's title in step with the unread count, as Outlook on the web does - `(3) Acme: Mail` - so mail that
15
+ * arrives while the tab is in the background can be seen from the tab strip. The count is put in front of whatever the title is
16
+ * (the page sets its own, from the branding) and taken off again when it drops to zero or the component goes away.
17
+ *
18
+ * Order matters when the same component also sets the title: declare this hook after the effect that does, so that on a change of `resetKey`
19
+ * the count is taken off the old title, the new title is written, and the count goes back in front of it.
20
+ */
21
+ export function useUnreadTitle(count, { enabled = true, resetKey } = {}) {
22
+ useEffect(() => {
23
+ if (!enabled) {
24
+ return;
25
+ }
26
+ document.title = titleWithUnread(document.title, count);
27
+ return () => {
28
+ document.title = titleWithUnread(document.title, 0);
29
+ };
30
+ }, [count, enabled, resetKey]);
31
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The web client's client-side router: what lets Mail, Calendar, Contacts, Tasks and Settings replace one another - and a
3
+ * folder replace a folder - without a page load.
4
+ *
5
+ * `@rapidrest/react` has no router of its own: every page is a server-rendered document with its own hydration entry, and
6
+ * hydrates only the page component (never `_layout.tsx`). So the router is a component the pages render themselves - each
7
+ * page's default export is `routedPage(path, Page)` (see `apps/www/_routedPage.tsx`), which renders `AppRouter` with the page
8
+ * as its first content. The first load is unchanged: the server renders the same tree, the browser hydrates it.
9
+ *
10
+ * `AppRouter` keeps ONE `AppChrome` (the icon rail, header, user menu, impersonation banner, compose windows, unlock prompt,
11
+ * idle-key timer, sign-out listener) mounted and swaps only what is inside it. A page's own shell (`MailShell`,
12
+ * `CalendarShell`, ...) still renders `AppShell` as it always did; inside the frame that renders nothing but its children.
13
+ *
14
+ * Navigation:
15
+ *
16
+ * Every `<a href>` to a same-origin path in the route table is intercepted (plain left clicks only - not with a modifier key, not
17
+ * `target`/`download`, not a hash-only link, not one marked `data-full-reload`), so pages just write ordinary links and
18
+ * middle-click, "open in new tab" and copy-link keep working. Anything not in the table - the admin and escrow consoles, plugin
19
+ * pages, other sites - is left to the browser. `useNavigate()` does the same for code (`navigate("/contacts")`).
20
+ *
21
+ * Back and forward work (`popstate`), and the URL is always the real, shareable one (`?mailboxUid=&folderUid=` and all).
22
+ *
23
+ * A route's code is loaded (`route.load()`) before the URL and the page change, so the old page stays up meanwhile (the frame is
24
+ * `aria-busy`); a route that fails to load falls back to a real navigation, which also picks up a new deploy.
25
+ *
26
+ * The next page's code is fetched on hover, focus and pointer-down of a link to it, and for the app rail's pages when the browser
27
+ * is idle.
28
+ *
29
+ * After a page change the frame moves keyboard focus to the content region, scrolls to the top, sets `document.title` and
30
+ * announces the new page to screen readers.
31
+ *
32
+ * The props of every `apps/www` page are the same for all of them (`WwwRoute.fetchProps()`: the user, auth-server URL,
33
+ * branding, plugin navigation, impersonation) except `params`, which the router recomputes from the URL - so the props of the
34
+ * page that was loaded are what every later page gets.
35
+ */
36
+ import React, { ComponentType } from "react";
37
+ import { RouteDefinition } from "./routes.js";
38
+ export { useLocation, useLocationSearch, useNavigate } from "./routerContext.js";
39
+ export type { NavigateFn, NavigateOptions, RouterLocation } from "./routerContext.js";
40
+ /** What `routedPage()` gives the router: the page component itself, without the router around it. */
41
+ export type RoutedPageComponent<P = any> = ComponentType<P> & {
42
+ page: ComponentType<P>;
43
+ };
44
+ export interface AppRouterProps {
45
+ routes: readonly RouteDefinition[];
46
+ /** The template of the route this document was rendered for. */
47
+ initialPath: string;
48
+ initialPage: ComponentType<any>;
49
+ /** The props the server rendered the page with. */
50
+ pageProps: Record<string, any>;
51
+ }
52
+ export default function AppRouter({ routes, initialPath, initialPage, pageProps }: AppRouterProps): React.JSX.Element;
@@ -0,0 +1,242 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ ///////////////////////////////////////////////////////////////////////////////
3
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
4
+ // SPDX-License-Identifier: MPL-2.0
5
+ ///////////////////////////////////////////////////////////////////////////////
6
+ /**
7
+ * The web client's client-side router: what lets Mail, Calendar, Contacts, Tasks and Settings replace one another - and a
8
+ * folder replace a folder - without a page load.
9
+ *
10
+ * `@rapidrest/react` has no router of its own: every page is a server-rendered document with its own hydration entry, and
11
+ * hydrates only the page component (never `_layout.tsx`). So the router is a component the pages render themselves - each
12
+ * page's default export is `routedPage(path, Page)` (see `apps/www/_routedPage.tsx`), which renders `AppRouter` with the page
13
+ * as its first content. The first load is unchanged: the server renders the same tree, the browser hydrates it.
14
+ *
15
+ * `AppRouter` keeps ONE `AppChrome` (the icon rail, header, user menu, impersonation banner, compose windows, unlock prompt,
16
+ * idle-key timer, sign-out listener) mounted and swaps only what is inside it. A page's own shell (`MailShell`,
17
+ * `CalendarShell`, ...) still renders `AppShell` as it always did; inside the frame that renders nothing but its children.
18
+ *
19
+ * Navigation:
20
+ *
21
+ * Every `<a href>` to a same-origin path in the route table is intercepted (plain left clicks only - not with a modifier key, not
22
+ * `target`/`download`, not a hash-only link, not one marked `data-full-reload`), so pages just write ordinary links and
23
+ * middle-click, "open in new tab" and copy-link keep working. Anything not in the table - the admin and escrow consoles, plugin
24
+ * pages, other sites - is left to the browser. `useNavigate()` does the same for code (`navigate("/contacts")`).
25
+ *
26
+ * Back and forward work (`popstate`), and the URL is always the real, shareable one (`?mailboxUid=&folderUid=` and all).
27
+ *
28
+ * A route's code is loaded (`route.load()`) before the URL and the page change, so the old page stays up meanwhile (the frame is
29
+ * `aria-busy`); a route that fails to load falls back to a real navigation, which also picks up a new deploy.
30
+ *
31
+ * The next page's code is fetched on hover, focus and pointer-down of a link to it, and for the app rail's pages when the browser
32
+ * is idle.
33
+ *
34
+ * After a page change the frame moves keyboard focus to the content region, scrolls to the top, sets `document.title` and
35
+ * announces the new page to screen readers.
36
+ *
37
+ * The props of every `apps/www` page are the same for all of them (`WwwRoute.fetchProps()`: the user, auth-server URL,
38
+ * branding, plugin navigation, impersonation) except `params`, which the router recomputes from the URL - so the props of the
39
+ * page that was loaded are what every later page gets.
40
+ */
41
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
42
+ import { AppChrome } from "../components/layout/AppShell.js";
43
+ import { AppFrameContext } from "./frameContext.js";
44
+ import { whenIdle, shouldSaveData } from "./idle.js";
45
+ import { matchRoute } from "./routes.js";
46
+ import { RouterContext, UNKNOWN_LOCATION, readWindowLocation } from "./routerContext.js";
47
+ export { useLocation, useLocationSearch, useNavigate } from "./routerContext.js";
48
+ function pickChromeProps(props) {
49
+ const { userUid, authServerUrl, impersonating, impersonationBaseUrl, trusted, trustedRoles, pluginNav } = props;
50
+ return { userUid, authServerUrl, impersonating, impersonationBaseUrl, trusted, trustedRoles, pluginNav };
51
+ }
52
+ /** The anchor a click/pointer event is on or in, if it is a link. */
53
+ function linkOf(event) {
54
+ const target = event.target;
55
+ return target instanceof Element ? target.closest("a[href]") : null;
56
+ }
57
+ /** The URL of a link the router may take over, `null` for one the browser must handle itself. */
58
+ function routerUrlOf(link) {
59
+ if ((link.target && link.target !== "_self") || link.hasAttribute("download") || link.hasAttribute("data-full-reload")) {
60
+ return null;
61
+ }
62
+ const url = new URL(link.href, window.location.href);
63
+ if (url.origin !== window.location.origin) {
64
+ return null;
65
+ }
66
+ // A link that only moves within the page (`#section`) is the browser's own.
67
+ if (url.hash !== "" && url.pathname === window.location.pathname && url.search === window.location.search) {
68
+ return null;
69
+ }
70
+ return url;
71
+ }
72
+ export default function AppRouter({ routes, initialPath, initialPage, pageProps }) {
73
+ const [current, setCurrent] = useState(() => ({
74
+ Page: initialPage,
75
+ routePath: initialPath,
76
+ active: routes.find((route) => route.path === initialPath)?.active ?? "mail",
77
+ params: pageProps.params ?? {},
78
+ key: "initial",
79
+ }));
80
+ const [location, setLocation] = useState(UNKNOWN_LOCATION);
81
+ const [pending, setPending] = useState(false);
82
+ const [takeovers, setTakeovers] = useState(0);
83
+ const currentRef = useRef(current);
84
+ currentRef.current = current;
85
+ /** Identifies the latest navigation; one that finds it changed while loading has been overtaken and does nothing. */
86
+ const navigationRef = useRef(0);
87
+ /** The page components loaded or loading, by route template - one request per page however many links are touched. */
88
+ const pagesRef = useRef(new Map([[initialPath, Promise.resolve(initialPage)]]));
89
+ const routesRef = useRef(routes);
90
+ routesRef.current = routes;
91
+ /** Loads a route's page component (once). Rejects if its code can't be loaded - and then a later attempt tries again. */
92
+ const pageFor = useCallback((route) => {
93
+ let loading = pagesRef.current.get(route.path);
94
+ if (!loading) {
95
+ loading = route.load().then((module) => {
96
+ const page = module.default.page;
97
+ if (!page) {
98
+ throw new Error(`The page for ${route.path} is not a routedPage().`);
99
+ }
100
+ return page;
101
+ });
102
+ pagesRef.current.set(route.path, loading);
103
+ loading.catch(() => pagesRef.current.delete(route.path));
104
+ }
105
+ return loading;
106
+ }, []);
107
+ /** The pathname of the page on screen (the address bar's, once a navigation has been committed). */
108
+ const shownPathRef = useRef(null);
109
+ /**
110
+ * Shows `match` (loading its page first if it is a different page from the one on screen), calling `commit` - which puts
111
+ * the URL in the address bar - at the moment the new page replaces the old one. Resolves `false` when the page's code
112
+ * couldn't be loaded, and `true` otherwise, including when a newer navigation overtook this one while it loaded.
113
+ */
114
+ const show = useCallback(async (match, target, sequence, commit) => {
115
+ const pathChanged = target.pathname !== shownPathRef.current;
116
+ let Page = currentRef.current.Page;
117
+ if (pathChanged && match.route.path !== currentRef.current.routePath) {
118
+ try {
119
+ Page = await pageFor(match.route);
120
+ }
121
+ catch {
122
+ return false;
123
+ }
124
+ }
125
+ if (sequence !== navigationRef.current) {
126
+ return true;
127
+ }
128
+ commit();
129
+ shownPathRef.current = target.pathname;
130
+ if (pathChanged) {
131
+ setCurrent({ Page, routePath: match.route.path, active: match.route.active, params: match.params, key: target.pathname });
132
+ }
133
+ setLocation(target);
134
+ return true;
135
+ }, [pageFor]);
136
+ const navigate = useCallback((href, options = {}) => {
137
+ const url = new URL(href, window.location.href);
138
+ const leave = () => (options.replace ? window.location.replace(url.href) : (window.location.href = url.href));
139
+ const match = url.origin === window.location.origin ? matchRoute(routesRef.current, url.pathname) : undefined;
140
+ if (!match) {
141
+ leave();
142
+ return;
143
+ }
144
+ const target = { pathname: url.pathname, search: url.search, hash: url.hash };
145
+ const now = readWindowLocation();
146
+ if (target.pathname === now.pathname && target.search === now.search && target.hash === now.hash) {
147
+ return;
148
+ }
149
+ const sequence = ++navigationRef.current;
150
+ setPending(true);
151
+ void show(match, target, sequence, () => {
152
+ window.history[options.replace ? "replaceState" : "pushState"](null, "", target.pathname + target.search + target.hash);
153
+ }).then((ok) => {
154
+ if (!ok) {
155
+ leave();
156
+ }
157
+ if (sequence === navigationRef.current) {
158
+ setPending(false);
159
+ }
160
+ });
161
+ }, [show]);
162
+ // The browser's own location is read after the first render, never during it: the server can't know the query string.
163
+ useEffect(() => {
164
+ const here = readWindowLocation();
165
+ shownPathRef.current = here.pathname;
166
+ setLocation(here);
167
+ }, []);
168
+ // Back and forward.
169
+ useEffect(() => {
170
+ function handlePopState() {
171
+ const here = readWindowLocation();
172
+ const match = matchRoute(routesRef.current, here.pathname);
173
+ if (!match) {
174
+ window.location.reload();
175
+ return;
176
+ }
177
+ const sequence = ++navigationRef.current;
178
+ setPending(true);
179
+ void show(match, here, sequence, () => undefined).then((ok) => {
180
+ if (!ok) {
181
+ window.location.reload();
182
+ }
183
+ if (sequence === navigationRef.current) {
184
+ setPending(false);
185
+ }
186
+ });
187
+ }
188
+ window.addEventListener("popstate", handlePopState);
189
+ return () => window.removeEventListener("popstate", handlePopState);
190
+ }, [show]);
191
+ // Plain links, and fetching what they lead to before they are clicked.
192
+ useEffect(() => {
193
+ function handleClick(event) {
194
+ if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
195
+ return;
196
+ }
197
+ const link = linkOf(event);
198
+ const url = link && routerUrlOf(link);
199
+ if (!url || !matchRoute(routesRef.current, url.pathname)) {
200
+ return;
201
+ }
202
+ event.preventDefault();
203
+ navigate(url.pathname + url.search + url.hash);
204
+ }
205
+ function handleIntent(event) {
206
+ const link = linkOf(event);
207
+ const url = link && routerUrlOf(link);
208
+ const match = url && matchRoute(routesRef.current, url.pathname);
209
+ if (match) {
210
+ pageFor(match.route).catch(() => undefined);
211
+ }
212
+ }
213
+ document.addEventListener("click", handleClick);
214
+ document.addEventListener("pointerover", handleIntent);
215
+ document.addEventListener("pointerdown", handleIntent);
216
+ document.addEventListener("focusin", handleIntent);
217
+ return () => {
218
+ document.removeEventListener("click", handleClick);
219
+ document.removeEventListener("pointerover", handleIntent);
220
+ document.removeEventListener("pointerdown", handleIntent);
221
+ document.removeEventListener("focusin", handleIntent);
222
+ };
223
+ }, [navigate, pageFor]);
224
+ // The app rail's other pages, once the browser has nothing else to do - so that the likeliest next click is instant.
225
+ useEffect(() => {
226
+ if (shouldSaveData()) {
227
+ return;
228
+ }
229
+ const cancels = routesRef.current
230
+ .filter((route) => route.idlePrefetch)
231
+ .map((route) => whenIdle(() => void pageFor(route).catch(() => undefined)));
232
+ return () => cancels.forEach((cancel) => cancel());
233
+ }, [pageFor]);
234
+ const enterTakeover = useCallback(() => {
235
+ setTakeovers((n) => n + 1);
236
+ return () => setTakeovers((n) => n - 1);
237
+ }, []);
238
+ const frame = useMemo(() => ({ enterTakeover }), [enterTakeover]);
239
+ const router = useMemo(() => ({ location, navigate }), [location, navigate]);
240
+ const Page = current.Page;
241
+ return (_jsx(RouterContext.Provider, { value: router, children: _jsx(AppFrameContext.Provider, { value: frame, children: _jsx(AppChrome, { active: current.active, routeKey: current.key, busy: pending, hideChrome: takeovers > 0, ...pickChromeProps(pageProps), children: _jsx(Page, { ...pageProps, params: current.params }, current.key) }) }) }));
242
+ }
@@ -0,0 +1,14 @@
1
+ /** Where each of the four rail apps lives - what the icon rail links to and what the keyboard's "Go to ..." shortcuts navigate to. */
2
+ export declare const APP_HREFS: {
3
+ readonly mail: "/";
4
+ readonly calendar: "/calendar";
5
+ readonly contacts: "/contacts";
6
+ readonly tasks: "/tasks";
7
+ };
8
+ /**
9
+ * Settings is reached from the account menu, not the rail. It links straight to the first settings section, the only one that always
10
+ * exists - repoint this at a real `/settings` landing page once there is one.
11
+ */
12
+ export declare const SETTINGS_HREF = "/settings/auto-reply";
13
+ /** Whether `pathname` is a page of Settings (any section), which is what "already there" means for it. */
14
+ export declare function isSettingsPath(pathname: string): boolean;
@@ -0,0 +1,20 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ /** Where each of the four rail apps lives - what the icon rail links to and what the keyboard's "Go to ..." shortcuts navigate to. */
6
+ export const APP_HREFS = {
7
+ mail: "/",
8
+ calendar: "/calendar",
9
+ contacts: "/contacts",
10
+ tasks: "/tasks",
11
+ };
12
+ /**
13
+ * Settings is reached from the account menu, not the rail. It links straight to the first settings section, the only one that always
14
+ * exists - repoint this at a real `/settings` landing page once there is one.
15
+ */
16
+ export const SETTINGS_HREF = "/settings/auto-reply";
17
+ /** Whether `pathname` is a page of Settings (any section), which is what "already there" means for it. */
18
+ export function isSettingsPath(pathname) {
19
+ return pathname === "/settings" || pathname.startsWith("/settings/");
20
+ }
@@ -0,0 +1,19 @@
1
+ import React, { PropsWithChildren } from "react";
2
+ /**
3
+ * What the persistent app frame (`AppRouter`'s one mounted `AppShell` chrome) offers to what is rendered inside it. `null`
4
+ * outside a frame - a page not shown by the router (an admin page, a plugin page, a test) renders its own chrome.
5
+ */
6
+ export interface AppFrameContextValue {
7
+ /** Hides the frame's chrome (icon rail, header, footer) until the returned function is called - see `FrameTakeover`. */
8
+ enterTakeover: () => () => void;
9
+ }
10
+ export declare const AppFrameContext: React.Context<AppFrameContextValue | null>;
11
+ /** `true` inside the persistent app frame. */
12
+ export declare function useInAppFrame(): boolean;
13
+ /**
14
+ * Wraps a screen that takes over the whole window - first-time key setup, "your mailbox is being set up" - which used to
15
+ * replace the page's shell entirely. Inside the persistent frame the shell can't be replaced (it stays mounted), so the frame
16
+ * hides its chrome for as long as this is mounted instead (and gives the screen the full width); outside a frame it renders its children as they are. A layout effect, so the chrome
17
+ * is gone before the screen is first painted.
18
+ */
19
+ export declare function FrameTakeover({ children }: PropsWithChildren): React.JSX.Element;
@@ -0,0 +1,24 @@
1
+ import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ ///////////////////////////////////////////////////////////////////////////////
3
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
4
+ // SPDX-License-Identifier: MPL-2.0
5
+ ///////////////////////////////////////////////////////////////////////////////
6
+ import { createContext, useContext, useLayoutEffect } from "react";
7
+ export const AppFrameContext = createContext(null);
8
+ /** `true` inside the persistent app frame. */
9
+ export function useInAppFrame() {
10
+ return useContext(AppFrameContext) !== null;
11
+ }
12
+ /**
13
+ * Wraps a screen that takes over the whole window - first-time key setup, "your mailbox is being set up" - which used to
14
+ * replace the page's shell entirely. Inside the persistent frame the shell can't be replaced (it stays mounted), so the frame
15
+ * hides its chrome for as long as this is mounted instead (and gives the screen the full width); outside a frame it renders its children as they are. A layout effect, so the chrome
16
+ * is gone before the screen is first painted.
17
+ */
18
+ export function FrameTakeover({ children }) {
19
+ const frame = useContext(AppFrameContext);
20
+ useLayoutEffect(() => frame?.enterTakeover(), [frame]);
21
+ // The frame puts a page in `#app-content`, a flex row: a full-window screen there would shrink to its content and sit at the left
22
+ // edge instead of centring, so inside the frame it is given the whole row, as a block its own root fills.
23
+ return frame ? _jsx("div", { className: "flex-1 min-w-0", children: children }) : _jsx(_Fragment, { children: children });
24
+ }
@@ -0,0 +1,14 @@
1
+ /** How long an idle callback may be held back on a busy page before it runs anyway. */
2
+ export declare const IDLE_TIMEOUT_MS = 3000;
3
+ /** The delay used where the browser has no `requestIdleCallback` (Safari). */
4
+ export declare const IDLE_FALLBACK_DELAY_MS = 200;
5
+ /**
6
+ * Runs `callback` once the page has finished loading (the window `load` event, which waits for every script and image the page
7
+ * asked for) and the browser has nothing better to do (`requestIdleCallback`), or shortly after where there is no such thing.
8
+ * For work that only makes later things faster - fetching a code chunk, warming a cache - and must never compete with what
9
+ * the user is waiting for. Returns a function that cancels it if it hasn't run yet.
10
+ */
11
+ export declare function whenIdle(callback: () => void): () => void;
12
+ /** `true` when the user has asked the browser to save data (or is on a very slow connection): work that only speculates
13
+ * about what will be needed - prefetching - should not run then. */
14
+ export declare function shouldSaveData(): boolean;
@@ -0,0 +1,43 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ /** How long an idle callback may be held back on a busy page before it runs anyway. */
6
+ export const IDLE_TIMEOUT_MS = 3000;
7
+ /** The delay used where the browser has no `requestIdleCallback` (Safari). */
8
+ export const IDLE_FALLBACK_DELAY_MS = 200;
9
+ /**
10
+ * Runs `callback` once the page has finished loading (the window `load` event, which waits for every script and image the page
11
+ * asked for) and the browser has nothing better to do (`requestIdleCallback`), or shortly after where there is no such thing.
12
+ * For work that only makes later things faster - fetching a code chunk, warming a cache - and must never compete with what
13
+ * the user is waiting for. Returns a function that cancels it if it hasn't run yet.
14
+ */
15
+ export function whenIdle(callback) {
16
+ let cancelIdle = () => undefined;
17
+ function schedule() {
18
+ if (typeof window.requestIdleCallback === "function") {
19
+ const handle = window.requestIdleCallback(callback, { timeout: IDLE_TIMEOUT_MS });
20
+ cancelIdle = () => window.cancelIdleCallback(handle);
21
+ }
22
+ else {
23
+ const timer = setTimeout(callback, IDLE_FALLBACK_DELAY_MS);
24
+ cancelIdle = () => clearTimeout(timer);
25
+ }
26
+ }
27
+ if (document.readyState === "complete") {
28
+ schedule();
29
+ }
30
+ else {
31
+ window.addEventListener("load", schedule, { once: true });
32
+ }
33
+ return () => {
34
+ window.removeEventListener("load", schedule);
35
+ cancelIdle();
36
+ };
37
+ }
38
+ /** `true` when the user has asked the browser to save data (or is on a very slow connection): work that only speculates
39
+ * about what will be needed - prefetching - should not run then. */
40
+ export function shouldSaveData() {
41
+ const connection = navigator.connection;
42
+ return connection?.saveData === true || connection?.effectiveType === "slow-2g" || connection?.effectiveType === "2g";
43
+ }
@@ -0,0 +1,37 @@
1
+ export interface RouterLocation {
2
+ pathname: string;
3
+ /** With the leading `?`, or `""`. */
4
+ search: string;
5
+ /** With the leading `#`, or `""`. */
6
+ hash: string;
7
+ }
8
+ export interface NavigateOptions {
9
+ /** Replaces the current history entry instead of adding one (a redirect, or a state change that isn't a "place"). */
10
+ replace?: boolean;
11
+ }
12
+ /** Goes to `href`: without a page load when it is a route of the app, else as an ordinary navigation. */
13
+ export type NavigateFn = (href: string, options?: NavigateOptions) => void;
14
+ export interface RouterContextValue {
15
+ location: RouterLocation;
16
+ navigate: NavigateFn;
17
+ }
18
+ export declare const RouterContext: import("react").Context<RouterContextValue | null>;
19
+ /** Before the browser's own location has been read (the server render and the first client render, which must match). */
20
+ export declare const UNKNOWN_LOCATION: RouterLocation;
21
+ export declare function readWindowLocation(): RouterLocation;
22
+ /**
23
+ * The function to go somewhere with. `navigate("/calendar")` and `navigate("/?mailboxUid=a&folderUid=b")` change the page and
24
+ * the URL without a page load when the router knows the route (Mail, Calendar, Contacts, Tasks, Settings and their subpages),
25
+ * and load the page normally otherwise; outside the router it is always a normal navigation. Prefer a plain `<a href>` for
26
+ * anything the user clicks - the router intercepts it, and it stays a real link - and this for what code decides (after a
27
+ * save, a delete, a select). The returned function is stable across renders.
28
+ */
29
+ export declare function useNavigate(): NavigateFn;
30
+ /**
31
+ * Where the app is now, as `{ pathname, search, hash }`, updating as the router navigates (folder changes are `search`
32
+ * changes, not page changes). Empty until the browser's location has been read in an effect after the first render, so that
33
+ * the server render and the first client render agree - read query parameters from it in an effect, not during render.
34
+ */
35
+ export declare function useLocation(): RouterLocation;
36
+ /** `useLocation().search` - what the shells read `?mailboxUid=` and the like from. */
37
+ export declare function useLocationSearch(): string;