@rapidmx/web-client 0.14.1 → 0.15.1
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.
- package/apps/admin/domains/[uid].tsx +3 -2
- package/apps/shared/appearance/AppearanceForm.tsx +9 -1
- package/apps/shared/calendar/bookingPlugin.ts +100 -0
- package/apps/shared/calendar/calendarLiveUpdates.ts +27 -0
- package/apps/shared/components/admin/layout/AdminShell.tsx +37 -5
- package/apps/shared/components/admin/settings/MailboxCreateForm.tsx +3 -1
- package/apps/shared/components/admin/settings/PluginsManager.tsx +1545 -1491
- package/apps/shared/components/calendar/AppointmentQuickForm.tsx +327 -0
- package/apps/shared/components/calendar/DescriptionEditor.tsx +274 -0
- package/apps/shared/components/calendar/EventDescriptionView.tsx +70 -0
- package/apps/shared/components/calendar/EventDetails.tsx +401 -0
- package/apps/shared/components/calendar/EventEditor.tsx +640 -0
- package/apps/shared/components/calendar/EventExpandedForm.tsx +210 -0
- package/apps/shared/components/calendar/EventFormParts.tsx +622 -0
- package/apps/shared/components/calendar/EventModal.tsx +126 -866
- package/apps/shared/components/calendar/EventQuickForm.tsx +116 -0
- package/apps/shared/components/calendar/EventShell.tsx +202 -0
- package/apps/shared/components/calendar/FindATime.tsx +379 -0
- package/apps/shared/components/calendar/LazyDescriptionEditor.tsx +70 -0
- package/apps/shared/components/calendar/MonthView.tsx +15 -3
- package/apps/shared/components/calendar/QuickCreateFaces.tsx +87 -0
- package/apps/shared/components/calendar/QuickCreateTabs.tsx +81 -0
- package/apps/shared/components/calendar/QuickFaceFrame.tsx +81 -0
- package/apps/shared/components/calendar/RecurrenceEditor.tsx +10 -5
- package/apps/shared/components/calendar/RequestChangeForm.tsx +271 -0
- package/apps/shared/components/calendar/SplitDayView.tsx +6 -5
- package/apps/shared/components/calendar/TaskQuickForm.tsx +289 -0
- package/apps/shared/components/calendar/TimeGridView.tsx +8 -6
- package/apps/shared/components/calendar/eventDialogFields.ts +86 -0
- package/apps/shared/components/calendar/eventForm.ts +108 -0
- package/apps/shared/components/calendar/eventFormat.ts +183 -0
- package/apps/shared/components/calendar/findTimeGrid.ts +90 -0
- package/apps/shared/components/calendar/swipeNavigation.ts +14 -0
- package/apps/shared/components/layout/UserMenu.tsx +80 -28
- package/apps/shared/components/mail/ConversationList.tsx +100 -48
- package/apps/shared/components/mail/ConversationThreadPane.tsx +490 -486
- package/apps/shared/components/mail/InviteCard.tsx +387 -0
- package/apps/shared/components/mail/MessageDetailPane.tsx +24 -2
- package/apps/shared/components/mail/SwipeRow.tsx +120 -0
- package/apps/shared/components/mail/compose/ComposeWindow.tsx +7 -3
- package/apps/shared/components/mail/invite/InviteRowChip.tsx +104 -0
- package/apps/shared/components/mail/invite/InviteRsvpPopover.tsx +271 -0
- package/apps/shared/components/mail/invite/InviteTimeline.tsx +200 -0
- package/apps/shared/components/mail/invite/ProposeTimeForm.tsx +109 -0
- package/apps/shared/components/mail/invite/inviteFormat.ts +110 -0
- package/apps/shared/components/mail/invite/inviteStore.ts +114 -0
- package/apps/shared/components/mail/layout/MailShell.tsx +522 -490
- package/apps/shared/components/settings/layout/SettingsShell.tsx +1 -0
- package/apps/shared/gestures/useEnterSlide.ts +54 -0
- package/apps/shared/gestures/useSwipe.ts +114 -0
- package/apps/shared/gestures/useSwipeSlide.ts +113 -0
- package/apps/shared/gestures/useWheelPaging.ts +107 -0
- package/apps/shared/mail/newMailNotifications.ts +171 -183
- package/apps/shared/mail/useNewMailNotifications.ts +178 -178
- package/apps/shared/notifications/preferences.ts +36 -0
- package/apps/shared/notifications/store.ts +560 -551
- package/apps/www/_routes.ts +1 -0
- package/apps/www/calendar/index.tsx +139 -37
- package/apps/www/index.tsx +273 -126
- package/apps/www/messages/[uid].tsx +31 -4
- package/apps/www/settings/profile/index.tsx +248 -0
- package/dist/apps/admin/domains/[uid].js +4 -2
- package/dist/apps/shared/appearance/AppearanceForm.js +9 -1
- package/dist/apps/shared/calendar/bookingPlugin.d.ts +50 -0
- package/dist/apps/shared/calendar/bookingPlugin.js +57 -0
- package/dist/apps/shared/calendar/calendarLiveUpdates.d.ts +11 -0
- package/dist/apps/shared/calendar/calendarLiveUpdates.js +23 -0
- package/dist/apps/shared/components/admin/layout/AdminShell.d.ts +1 -1
- package/dist/apps/shared/components/admin/layout/AdminShell.js +7 -3
- package/dist/apps/shared/components/admin/settings/MailboxCreateForm.js +3 -1
- package/dist/apps/shared/components/admin/settings/PluginsManager.js +40 -6
- package/dist/apps/shared/components/calendar/AppointmentQuickForm.d.ts +47 -0
- package/dist/apps/shared/components/calendar/AppointmentQuickForm.js +135 -0
- package/dist/apps/shared/components/calendar/DescriptionEditor.d.ts +26 -0
- package/dist/apps/shared/components/calendar/DescriptionEditor.js +192 -0
- package/dist/apps/shared/components/calendar/EventDescriptionView.d.ts +14 -0
- package/dist/apps/shared/components/calendar/EventDescriptionView.js +39 -0
- package/dist/apps/shared/components/calendar/EventDetails.d.ts +33 -0
- package/dist/apps/shared/components/calendar/EventDetails.js +137 -0
- package/dist/apps/shared/components/calendar/EventEditor.d.ts +59 -0
- package/dist/apps/shared/components/calendar/EventEditor.js +549 -0
- package/dist/apps/shared/components/calendar/EventExpandedForm.d.ts +14 -0
- package/dist/apps/shared/components/calendar/EventExpandedForm.js +44 -0
- package/dist/apps/shared/components/calendar/EventFormParts.d.ts +69 -0
- package/dist/apps/shared/components/calendar/EventFormParts.js +202 -0
- package/dist/apps/shared/components/calendar/EventModal.d.ts +26 -13
- package/dist/apps/shared/components/calendar/EventModal.js +57 -459
- package/dist/apps/shared/components/calendar/EventQuickForm.d.ts +18 -0
- package/dist/apps/shared/components/calendar/EventQuickForm.js +25 -0
- package/dist/apps/shared/components/calendar/EventShell.d.ts +64 -0
- package/dist/apps/shared/components/calendar/EventShell.js +134 -0
- package/dist/apps/shared/components/calendar/FindATime.d.ts +18 -0
- package/dist/apps/shared/components/calendar/FindATime.js +190 -0
- package/dist/apps/shared/components/calendar/LazyDescriptionEditor.d.ts +8 -0
- package/dist/apps/shared/components/calendar/LazyDescriptionEditor.js +44 -0
- package/dist/apps/shared/components/calendar/MonthView.d.ts +5 -1
- package/dist/apps/shared/components/calendar/MonthView.js +11 -5
- package/dist/apps/shared/components/calendar/QuickCreateFaces.d.ts +30 -0
- package/dist/apps/shared/components/calendar/QuickCreateFaces.js +44 -0
- package/dist/apps/shared/components/calendar/QuickCreateTabs.d.ts +24 -0
- package/dist/apps/shared/components/calendar/QuickCreateTabs.js +46 -0
- package/dist/apps/shared/components/calendar/QuickFaceFrame.d.ts +25 -0
- package/dist/apps/shared/components/calendar/QuickFaceFrame.js +19 -0
- package/dist/apps/shared/components/calendar/RecurrenceEditor.d.ts +4 -1
- package/dist/apps/shared/components/calendar/RecurrenceEditor.js +2 -2
- package/dist/apps/shared/components/calendar/RequestChangeForm.d.ts +36 -0
- package/dist/apps/shared/components/calendar/RequestChangeForm.js +138 -0
- package/dist/apps/shared/components/calendar/SplitDayView.d.ts +3 -2
- package/dist/apps/shared/components/calendar/SplitDayView.js +3 -2
- package/dist/apps/shared/components/calendar/TaskQuickForm.d.ts +40 -0
- package/dist/apps/shared/components/calendar/TaskQuickForm.js +101 -0
- package/dist/apps/shared/components/calendar/TimeGridView.d.ts +4 -2
- package/dist/apps/shared/components/calendar/TimeGridView.js +3 -2
- package/dist/apps/shared/components/calendar/eventDialogFields.d.ts +28 -0
- package/dist/apps/shared/components/calendar/eventDialogFields.js +59 -0
- package/dist/apps/shared/components/calendar/eventForm.d.ts +98 -0
- package/dist/apps/shared/components/calendar/eventForm.js +1 -0
- package/dist/apps/shared/components/calendar/eventFormat.d.ts +67 -0
- package/dist/apps/shared/components/calendar/eventFormat.js +163 -0
- package/dist/apps/shared/components/calendar/findTimeGrid.d.ts +30 -0
- package/dist/apps/shared/components/calendar/findTimeGrid.js +83 -0
- package/dist/apps/shared/components/calendar/swipeNavigation.d.ts +9 -0
- package/dist/apps/shared/components/calendar/swipeNavigation.js +8 -0
- package/dist/apps/shared/components/layout/UserMenu.d.ts +10 -8
- package/dist/apps/shared/components/layout/UserMenu.js +70 -23
- package/dist/apps/shared/components/mail/ConversationList.d.ts +12 -1
- package/dist/apps/shared/components/mail/ConversationList.js +21 -4
- package/dist/apps/shared/components/mail/ConversationThreadPane.d.ts +4 -1
- package/dist/apps/shared/components/mail/InviteCard.d.ts +20 -0
- package/dist/apps/shared/components/mail/InviteCard.js +150 -0
- package/dist/apps/shared/components/mail/MessageDetailPane.js +13 -2
- package/dist/apps/shared/components/mail/SwipeRow.d.ts +22 -0
- package/dist/apps/shared/components/mail/SwipeRow.js +71 -0
- package/dist/apps/shared/components/mail/compose/ComposeWindow.js +8 -3
- package/dist/apps/shared/components/mail/invite/InviteRowChip.d.ts +23 -0
- package/dist/apps/shared/components/mail/invite/InviteRowChip.js +46 -0
- package/dist/apps/shared/components/mail/invite/InviteRsvpPopover.d.ts +20 -0
- package/dist/apps/shared/components/mail/invite/InviteRsvpPopover.js +141 -0
- package/dist/apps/shared/components/mail/invite/InviteTimeline.d.ts +27 -0
- package/dist/apps/shared/components/mail/invite/InviteTimeline.js +120 -0
- package/dist/apps/shared/components/mail/invite/ProposeTimeForm.d.ts +17 -0
- package/dist/apps/shared/components/mail/invite/ProposeTimeForm.js +45 -0
- package/dist/apps/shared/components/mail/invite/inviteFormat.d.ts +28 -0
- package/dist/apps/shared/components/mail/invite/inviteFormat.js +94 -0
- package/dist/apps/shared/components/mail/invite/inviteStore.d.ts +28 -0
- package/dist/apps/shared/components/mail/invite/inviteStore.js +95 -0
- package/dist/apps/shared/components/mail/layout/MailShell.d.ts +5 -0
- package/dist/apps/shared/components/mail/layout/MailShell.js +14 -3
- package/dist/apps/shared/components/settings/layout/SettingsShell.js +1 -0
- package/dist/apps/shared/gestures/useEnterSlide.d.ts +14 -0
- package/dist/apps/shared/gestures/useEnterSlide.js +47 -0
- package/dist/apps/shared/gestures/useSwipe.d.ts +45 -0
- package/dist/apps/shared/gestures/useSwipe.js +90 -0
- package/dist/apps/shared/gestures/useSwipeSlide.d.ts +40 -0
- package/dist/apps/shared/gestures/useSwipeSlide.js +95 -0
- package/dist/apps/shared/gestures/useWheelPaging.d.ts +32 -0
- package/dist/apps/shared/gestures/useWheelPaging.js +85 -0
- package/dist/apps/shared/mail/newMailNotifications.d.ts +3 -8
- package/dist/apps/shared/mail/newMailNotifications.js +3 -12
- package/dist/apps/shared/mail/useNewMailNotifications.js +3 -2
- package/dist/apps/shared/notifications/preferences.d.ts +13 -0
- package/dist/apps/shared/notifications/preferences.js +36 -0
- package/dist/apps/shared/notifications/store.d.ts +1 -0
- package/dist/apps/shared/notifications/store.js +7 -23
- package/dist/apps/www/_routes.js +1 -0
- package/dist/apps/www/calendar/index.js +76 -6
- package/dist/apps/www/index.js +140 -39
- package/dist/apps/www/messages/[uid].js +13 -5
- package/dist/apps/www/settings/profile/index.d.ts +4 -0
- package/dist/apps/www/settings/profile/index.js +124 -0
- package/package.json +2 -2
|
@@ -1,178 +1,178 @@
|
|
|
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 type { Mailbox, Message } from "@rapidmx/react-shared/mail/mailApi.js";
|
|
7
|
-
import type { MailboxFolders } from "../components/mail/layout/MailShell.js";
|
|
8
|
-
import {
|
|
9
|
-
DesktopPermission,
|
|
10
|
-
NewMailNotice,
|
|
11
|
-
desktopPermission,
|
|
12
|
-
getDesktopOfferDismissed,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from "
|
|
21
|
-
import { getNotificationsSnapshot, notify } from "../notifications/store.js";
|
|
22
|
-
|
|
23
|
-
/** How long a new-mail pop-up stays before it goes by itself (while it is neither hovered nor focused, and the tab is in view). */
|
|
24
|
-
export const TOAST_DURATION_MS = 8_000;
|
|
25
|
-
|
|
26
|
-
/** At most this many desktop notifications in `DESKTOP_BURST_WINDOW_MS`: a flood of mail is a few notifications, not a hundred. */
|
|
27
|
-
export const DESKTOP_BURST_LIMIT = 5;
|
|
28
|
-
export const DESKTOP_BURST_WINDOW_MS = 30_000;
|
|
29
|
-
|
|
30
|
-
/** How many announced message uids are remembered, so an event delivered twice is announced once. */
|
|
31
|
-
const ANNOUNCED_LIMIT = 500;
|
|
32
|
-
|
|
33
|
-
export interface NewMailNotifications {
|
|
34
|
-
/** Whether the next pop-up should also offer to turn on desktop notifications: the browser can, has not been asked,
|
|
35
|
-
* and the user has not said "Not now". */
|
|
36
|
-
offerDesktop: boolean;
|
|
37
|
-
/** The offer's "Turn on desktop notifications": asks the browser. Must be called from a click. */
|
|
38
|
-
enableDesktop(): Promise<void>;
|
|
39
|
-
/** The offer's "Not now": remembered, so it is not made again. */
|
|
40
|
-
declineDesktop(): void;
|
|
41
|
-
/** Announces a message that just arrived - if it is worth it (see `shouldAnnounce()`) and pop-ups are on. Stable. */
|
|
42
|
-
announce(message: Message): void;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Whether the user is not looking at this tab: it is hidden, or another window has the focus. */
|
|
46
|
-
function tabIsInBackground(): boolean {
|
|
47
|
-
return document.visibilityState === "hidden" || !document.hasFocus();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface UseNewMailNotificationsOptions {
|
|
51
|
-
mailboxes: Mailbox[];
|
|
52
|
-
mailboxFolders: MailboxFolders[];
|
|
53
|
-
/** Opens a message from a desktop notification's click. Defaults to a plain navigation. */
|
|
54
|
-
open?: (href: string) => void;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function defaultOpen(href: string): void {
|
|
58
|
-
window.location.href = href;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* New-mail announcements: an in-app pop-up for each message that arrives (a `mail` notification - see `notifications/store.ts`, which owns the
|
|
63
|
-
* stack, its limit of three and the clock), and - while the tab
|
|
64
|
-
* is in the background and the user has allowed it - a desktop notification with the same content, one per message however
|
|
65
|
-
* many events name it. `announce()` is what `useMailLiveUpdates()`'s `onMessageCreated` calls; nothing else feeds it, so a
|
|
66
|
-
* page load, a list refetch and a reconnect never announce anything.
|
|
67
|
-
*
|
|
68
|
-
* Nothing is ever asked of the browser on load: permission is requested only by `enableDesktop()`, from the offer in the
|
|
69
|
-
* first pop-up, or from Settings. What the user answered is remembered in `localStorage` (see `newMailNotifications.ts`).
|
|
70
|
-
*/
|
|
71
|
-
export function useNewMailNotifications({ mailboxes, mailboxFolders, open = defaultOpen }: UseNewMailNotificationsOptions): NewMailNotifications {
|
|
72
|
-
const [permission, setPermission] = useState<DesktopPermission>("unsupported");
|
|
73
|
-
const [offerDismissed, setOfferDismissed] = useState(true);
|
|
74
|
-
const folders = useMemo(() => mailboxFolders.flatMap((entry) => entry.folders), [mailboxFolders]);
|
|
75
|
-
const ownAddresses = useMemo(() => ownAddressesOf(mailboxes), [mailboxes]);
|
|
76
|
-
const latestRef = useRef({ folders, ownAddresses, open });
|
|
77
|
-
latestRef.current = { folders, ownAddresses, open };
|
|
78
|
-
const announcedRef = useRef<Set<string>>(new Set());
|
|
79
|
-
const desktopShownRef = useRef<number[]>([]);
|
|
80
|
-
|
|
81
|
-
// The browser's own state is only read on the client, after hydration, so the server and first client render agree.
|
|
82
|
-
useEffect(() => {
|
|
83
|
-
setPermission(desktopPermission());
|
|
84
|
-
setOfferDismissed(getDesktopOfferDismissed());
|
|
85
|
-
}, []);
|
|
86
|
-
|
|
87
|
-
const showDesktop = useCallback((notice: NewMailNotice) => {
|
|
88
|
-
const now = Date.now();
|
|
89
|
-
desktopShownRef.current = desktopShownRef.current.filter((at) => now - at < DESKTOP_BURST_WINDOW_MS);
|
|
90
|
-
if (desktopShownRef.current.length >= DESKTOP_BURST_LIMIT) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
try {
|
|
94
|
-
const notification = new Notification(noticeSender(notice), {
|
|
95
|
-
body: [notice.subject, notice.preview].filter(Boolean).join("\n"),
|
|
96
|
-
// One per message: a duplicate (another tab of this app, a re-delivered event) replaces instead of stacking.
|
|
97
|
-
tag: notice.uid,
|
|
98
|
-
});
|
|
99
|
-
desktopShownRef.current.push(now);
|
|
100
|
-
notification.onclick = () => {
|
|
101
|
-
window.focus();
|
|
102
|
-
notification.close();
|
|
103
|
-
latestRef.current.open(notice.href);
|
|
104
|
-
};
|
|
105
|
-
} catch {
|
|
106
|
-
// Some browsers refuse `new Notification()` outright (a mobile one wanting a service worker): the pop-up is enough.
|
|
107
|
-
}
|
|
108
|
-
}, []);
|
|
109
|
-
|
|
110
|
-
const enableDesktop = useCallback(async () => {
|
|
111
|
-
const answer = await requestDesktopPermission();
|
|
112
|
-
setPermission(answer);
|
|
113
|
-
// Answered either way, so the offer goes: on granted it has done its job, on denied it can't be made again.
|
|
114
|
-
setDesktopOfferDismissed(true);
|
|
115
|
-
setOfferDismissed(true);
|
|
116
|
-
}, []);
|
|
117
|
-
|
|
118
|
-
const declineDesktop = useCallback(() => {
|
|
119
|
-
setDesktopOfferDismissed(true);
|
|
120
|
-
setOfferDismissed(true);
|
|
121
|
-
}, []);
|
|
122
|
-
|
|
123
|
-
// The offer's current state and answers, for the stable `announce` below.
|
|
124
|
-
const offerDesktop = permission === "default" && !offerDismissed;
|
|
125
|
-
const offerRef = useRef({ offered: offerDesktop, enable: enableDesktop, decline: declineDesktop });
|
|
126
|
-
offerRef.current = { offered: offerDesktop, enable: enableDesktop, decline: declineDesktop };
|
|
127
|
-
|
|
128
|
-
const announce = useCallback(
|
|
129
|
-
(message: Message) => {
|
|
130
|
-
if (!
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const { folders: knownFolders, ownAddresses: own } = latestRef.current;
|
|
134
|
-
if (!shouldAnnounce(message, { folders: knownFolders, ownAddresses: own })) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
announcedRef.current.add(message.uid);
|
|
138
|
-
if (announcedRef.current.size > ANNOUNCED_LIMIT) {
|
|
139
|
-
announcedRef.current.delete(announcedRef.current.values().next().value!);
|
|
140
|
-
}
|
|
141
|
-
const notice = noticeFor(message);
|
|
142
|
-
const offer = offerRef.current;
|
|
143
|
-
notify({
|
|
144
|
-
id: `mail:${notice.uid}`,
|
|
145
|
-
kind: "mail",
|
|
146
|
-
title: notice.senderName || notice.senderAddress,
|
|
147
|
-
subtitle: notice.senderName ? `<${notice.senderAddress}>` : undefined,
|
|
148
|
-
message: notice.subject,
|
|
149
|
-
preview: notice.preview || undefined,
|
|
150
|
-
href: notice.href,
|
|
151
|
-
timeoutMs: TOAST_DURATION_MS,
|
|
152
|
-
// Actions make a notification sticky; a new-mail pop-up with the offer still goes by itself.
|
|
153
|
-
sticky: false,
|
|
154
|
-
// The offer is made once at a time: on a pop-up only while none of the pop-ups still showing carries it.
|
|
155
|
-
...(offer.offered && !getNotificationsSnapshot().visible.some((item) => item.kind === "mail" && item.actions.length > 0)
|
|
156
|
-
? {
|
|
157
|
-
hint: "Get a desktop notification when mail arrives while this tab is in the background.",
|
|
158
|
-
actions: [
|
|
159
|
-
{ label: "Turn on desktop notifications", onClick: () => void offer.enable() },
|
|
160
|
-
{ label: "Not now", onClick: offer.decline },
|
|
161
|
-
],
|
|
162
|
-
}
|
|
163
|
-
: {}),
|
|
164
|
-
});
|
|
165
|
-
if (desktopPermission() === "granted" && tabIsInBackground()) {
|
|
166
|
-
showDesktop(notice);
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
[showDesktop],
|
|
170
|
-
);
|
|
171
|
-
|
|
172
|
-
return {
|
|
173
|
-
offerDesktop,
|
|
174
|
-
enableDesktop,
|
|
175
|
-
declineDesktop,
|
|
176
|
-
announce,
|
|
177
|
-
};
|
|
178
|
-
}
|
|
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 type { Mailbox, Message } from "@rapidmx/react-shared/mail/mailApi.js";
|
|
7
|
+
import type { MailboxFolders } from "../components/mail/layout/MailShell.js";
|
|
8
|
+
import {
|
|
9
|
+
DesktopPermission,
|
|
10
|
+
NewMailNotice,
|
|
11
|
+
desktopPermission,
|
|
12
|
+
getDesktopOfferDismissed,
|
|
13
|
+
noticeFor,
|
|
14
|
+
noticeSender,
|
|
15
|
+
ownAddressesOf,
|
|
16
|
+
requestDesktopPermission,
|
|
17
|
+
setDesktopOfferDismissed,
|
|
18
|
+
shouldAnnounce,
|
|
19
|
+
} from "./newMailNotifications.js";
|
|
20
|
+
import { getNotificationsEnabled } from "../notifications/preferences.js";
|
|
21
|
+
import { getNotificationsSnapshot, notify } from "../notifications/store.js";
|
|
22
|
+
|
|
23
|
+
/** How long a new-mail pop-up stays before it goes by itself (while it is neither hovered nor focused, and the tab is in view). */
|
|
24
|
+
export const TOAST_DURATION_MS = 8_000;
|
|
25
|
+
|
|
26
|
+
/** At most this many desktop notifications in `DESKTOP_BURST_WINDOW_MS`: a flood of mail is a few notifications, not a hundred. */
|
|
27
|
+
export const DESKTOP_BURST_LIMIT = 5;
|
|
28
|
+
export const DESKTOP_BURST_WINDOW_MS = 30_000;
|
|
29
|
+
|
|
30
|
+
/** How many announced message uids are remembered, so an event delivered twice is announced once. */
|
|
31
|
+
const ANNOUNCED_LIMIT = 500;
|
|
32
|
+
|
|
33
|
+
export interface NewMailNotifications {
|
|
34
|
+
/** Whether the next pop-up should also offer to turn on desktop notifications: the browser can, has not been asked,
|
|
35
|
+
* and the user has not said "Not now". */
|
|
36
|
+
offerDesktop: boolean;
|
|
37
|
+
/** The offer's "Turn on desktop notifications": asks the browser. Must be called from a click. */
|
|
38
|
+
enableDesktop(): Promise<void>;
|
|
39
|
+
/** The offer's "Not now": remembered, so it is not made again. */
|
|
40
|
+
declineDesktop(): void;
|
|
41
|
+
/** Announces a message that just arrived - if it is worth it (see `shouldAnnounce()`) and pop-ups are on. Stable. */
|
|
42
|
+
announce(message: Message): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether the user is not looking at this tab: it is hidden, or another window has the focus. */
|
|
46
|
+
function tabIsInBackground(): boolean {
|
|
47
|
+
return document.visibilityState === "hidden" || !document.hasFocus();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UseNewMailNotificationsOptions {
|
|
51
|
+
mailboxes: Mailbox[];
|
|
52
|
+
mailboxFolders: MailboxFolders[];
|
|
53
|
+
/** Opens a message from a desktop notification's click. Defaults to a plain navigation. */
|
|
54
|
+
open?: (href: string) => void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function defaultOpen(href: string): void {
|
|
58
|
+
window.location.href = href;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* New-mail announcements: an in-app pop-up for each message that arrives (a `mail` notification - see `notifications/store.ts`, which owns the
|
|
63
|
+
* stack, its limit of three and the clock), and - while the tab
|
|
64
|
+
* is in the background and the user has allowed it - a desktop notification with the same content, one per message however
|
|
65
|
+
* many events name it. `announce()` is what `useMailLiveUpdates()`'s `onMessageCreated` calls; nothing else feeds it, so a
|
|
66
|
+
* page load, a list refetch and a reconnect never announce anything.
|
|
67
|
+
*
|
|
68
|
+
* Nothing is ever asked of the browser on load: permission is requested only by `enableDesktop()`, from the offer in the
|
|
69
|
+
* first pop-up, or from Settings. What the user answered is remembered in `localStorage` (see `newMailNotifications.ts`).
|
|
70
|
+
*/
|
|
71
|
+
export function useNewMailNotifications({ mailboxes, mailboxFolders, open = defaultOpen }: UseNewMailNotificationsOptions): NewMailNotifications {
|
|
72
|
+
const [permission, setPermission] = useState<DesktopPermission>("unsupported");
|
|
73
|
+
const [offerDismissed, setOfferDismissed] = useState(true);
|
|
74
|
+
const folders = useMemo(() => mailboxFolders.flatMap((entry) => entry.folders), [mailboxFolders]);
|
|
75
|
+
const ownAddresses = useMemo(() => ownAddressesOf(mailboxes), [mailboxes]);
|
|
76
|
+
const latestRef = useRef({ folders, ownAddresses, open });
|
|
77
|
+
latestRef.current = { folders, ownAddresses, open };
|
|
78
|
+
const announcedRef = useRef<Set<string>>(new Set());
|
|
79
|
+
const desktopShownRef = useRef<number[]>([]);
|
|
80
|
+
|
|
81
|
+
// The browser's own state is only read on the client, after hydration, so the server and first client render agree.
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
setPermission(desktopPermission());
|
|
84
|
+
setOfferDismissed(getDesktopOfferDismissed());
|
|
85
|
+
}, []);
|
|
86
|
+
|
|
87
|
+
const showDesktop = useCallback((notice: NewMailNotice) => {
|
|
88
|
+
const now = Date.now();
|
|
89
|
+
desktopShownRef.current = desktopShownRef.current.filter((at) => now - at < DESKTOP_BURST_WINDOW_MS);
|
|
90
|
+
if (desktopShownRef.current.length >= DESKTOP_BURST_LIMIT) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const notification = new Notification(noticeSender(notice), {
|
|
95
|
+
body: [notice.subject, notice.preview].filter(Boolean).join("\n"),
|
|
96
|
+
// One per message: a duplicate (another tab of this app, a re-delivered event) replaces instead of stacking.
|
|
97
|
+
tag: notice.uid,
|
|
98
|
+
});
|
|
99
|
+
desktopShownRef.current.push(now);
|
|
100
|
+
notification.onclick = () => {
|
|
101
|
+
window.focus();
|
|
102
|
+
notification.close();
|
|
103
|
+
latestRef.current.open(notice.href);
|
|
104
|
+
};
|
|
105
|
+
} catch {
|
|
106
|
+
// Some browsers refuse `new Notification()` outright (a mobile one wanting a service worker): the pop-up is enough.
|
|
107
|
+
}
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
const enableDesktop = useCallback(async () => {
|
|
111
|
+
const answer = await requestDesktopPermission();
|
|
112
|
+
setPermission(answer);
|
|
113
|
+
// Answered either way, so the offer goes: on granted it has done its job, on denied it can't be made again.
|
|
114
|
+
setDesktopOfferDismissed(true);
|
|
115
|
+
setOfferDismissed(true);
|
|
116
|
+
}, []);
|
|
117
|
+
|
|
118
|
+
const declineDesktop = useCallback(() => {
|
|
119
|
+
setDesktopOfferDismissed(true);
|
|
120
|
+
setOfferDismissed(true);
|
|
121
|
+
}, []);
|
|
122
|
+
|
|
123
|
+
// The offer's current state and answers, for the stable `announce` below.
|
|
124
|
+
const offerDesktop = permission === "default" && !offerDismissed;
|
|
125
|
+
const offerRef = useRef({ offered: offerDesktop, enable: enableDesktop, decline: declineDesktop });
|
|
126
|
+
offerRef.current = { offered: offerDesktop, enable: enableDesktop, decline: declineDesktop };
|
|
127
|
+
|
|
128
|
+
const announce = useCallback(
|
|
129
|
+
(message: Message) => {
|
|
130
|
+
if (!getNotificationsEnabled() || announcedRef.current.has(message.uid)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const { folders: knownFolders, ownAddresses: own } = latestRef.current;
|
|
134
|
+
if (!shouldAnnounce(message, { folders: knownFolders, ownAddresses: own })) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
announcedRef.current.add(message.uid);
|
|
138
|
+
if (announcedRef.current.size > ANNOUNCED_LIMIT) {
|
|
139
|
+
announcedRef.current.delete(announcedRef.current.values().next().value!);
|
|
140
|
+
}
|
|
141
|
+
const notice = noticeFor(message);
|
|
142
|
+
const offer = offerRef.current;
|
|
143
|
+
notify({
|
|
144
|
+
id: `mail:${notice.uid}`,
|
|
145
|
+
kind: "mail",
|
|
146
|
+
title: notice.senderName || notice.senderAddress,
|
|
147
|
+
subtitle: notice.senderName ? `<${notice.senderAddress}>` : undefined,
|
|
148
|
+
message: notice.subject,
|
|
149
|
+
preview: notice.preview || undefined,
|
|
150
|
+
href: notice.href,
|
|
151
|
+
timeoutMs: TOAST_DURATION_MS,
|
|
152
|
+
// Actions make a notification sticky; a new-mail pop-up with the offer still goes by itself.
|
|
153
|
+
sticky: false,
|
|
154
|
+
// The offer is made once at a time: on a pop-up only while none of the pop-ups still showing carries it.
|
|
155
|
+
...(offer.offered && !getNotificationsSnapshot().visible.some((item) => item.kind === "mail" && item.actions.length > 0)
|
|
156
|
+
? {
|
|
157
|
+
hint: "Get a desktop notification when mail arrives while this tab is in the background.",
|
|
158
|
+
actions: [
|
|
159
|
+
{ label: "Turn on desktop notifications", onClick: () => void offer.enable() },
|
|
160
|
+
{ label: "Not now", onClick: offer.decline },
|
|
161
|
+
],
|
|
162
|
+
}
|
|
163
|
+
: {}),
|
|
164
|
+
});
|
|
165
|
+
if (desktopPermission() === "granted" && tabIsInBackground()) {
|
|
166
|
+
showDesktop(notice);
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
[showDesktop],
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
offerDesktop,
|
|
174
|
+
enableDesktop,
|
|
175
|
+
declineDesktop,
|
|
176
|
+
announce,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz. All rights reserved.
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
/**
|
|
6
|
+
* The user's one switch for every pop-up (new mail, a failed send, an API error, a reminder), per browser in `localStorage`.
|
|
7
|
+
* Framework-free like the store, which reads it: `notify()` keeps a notification out of sight while it is off (it is still
|
|
8
|
+
* listed in "Recent notifications").
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `localStorage` key of the switch. Absent means on; `"off"` means the user turned pop-ups off. The name is from when the switch
|
|
13
|
+
* only covered new mail, and is kept so a browser where that was turned off stays off.
|
|
14
|
+
*/
|
|
15
|
+
export const NOTIFICATIONS_ENABLED_KEY = "rapidmx-new-mail-popups";
|
|
16
|
+
|
|
17
|
+
/** Whether pop-up notifications are on (the default). */
|
|
18
|
+
export function getNotificationsEnabled(): boolean {
|
|
19
|
+
try {
|
|
20
|
+
return localStorage.getItem(NOTIFICATIONS_ENABLED_KEY) !== "off";
|
|
21
|
+
} catch {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function setNotificationsEnabled(enabled: boolean): void {
|
|
27
|
+
try {
|
|
28
|
+
if (enabled) {
|
|
29
|
+
localStorage.removeItem(NOTIFICATIONS_ENABLED_KEY);
|
|
30
|
+
} else {
|
|
31
|
+
localStorage.setItem(NOTIFICATIONS_ENABLED_KEY, "off");
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
// Storage blocked or full: the choice lasts until the page is reloaded, no longer.
|
|
35
|
+
}
|
|
36
|
+
}
|