@rapidmx/web-client 0.3.1 → 0.5.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.
- package/apps/admin/audit-log/index.tsx +35 -8
- package/apps/admin/branding/index.tsx +39 -404
- package/apps/admin/data-requests/index.tsx +480 -485
- package/apps/admin/distribution-lists/[uid].tsx +19 -1
- package/apps/admin/domains/[uid].tsx +92 -259
- package/apps/admin/encryption-policy/index.tsx +19 -0
- package/apps/admin/escrow-scopes/[uid].tsx +348 -237
- package/apps/admin/escrow-scopes/new/index.tsx +8 -2
- package/apps/admin/index.tsx +118 -82
- package/apps/admin/ingest-queue/index.tsx +44 -5
- package/apps/admin/mailbox-policy/index.tsx +19 -0
- package/apps/admin/mailboxes/[uid].tsx +211 -184
- package/apps/admin/mailboxes/new/index.tsx +28 -291
- package/apps/admin/plugins/index.tsx +15 -0
- package/apps/admin/quarantine/index.tsx +46 -14
- package/apps/admin/retention-policy/index.tsx +39 -132
- package/apps/admin/setup/index.tsx +15 -0
- package/apps/admin/transport-rules/[uid].tsx +30 -1
- package/apps/admin/transport-rules/_transportRuleConfig.tsx +38 -2
- package/apps/admin/transport-rules/index.tsx +24 -6
- package/apps/admin/transport-rules/new/index.tsx +30 -1
- package/apps/escrow/_layout.tsx +2 -3
- package/apps/escrow/audit-log/index.tsx +196 -168
- package/apps/escrow/matters/[uid].tsx +621 -521
- package/apps/shared/components/admin/escrowScopes/EscrowScopeKeyAndHoldersFields.tsx +22 -1
- package/apps/shared/components/admin/layout/AdminShell.tsx +251 -210
- package/apps/shared/components/admin/mailboxes/EscrowScopeCard.tsx +152 -0
- package/apps/shared/components/admin/mailboxes/ResourceSettingsCard.tsx +3 -3
- package/apps/shared/components/admin/mailboxes/ShareAccessCard.tsx +55 -11
- package/apps/shared/components/admin/settings/BrandingForm.tsx +385 -0
- package/apps/shared/components/admin/settings/DomainDnsSetup.tsx +187 -0
- package/apps/shared/components/admin/settings/EncryptionPolicyForm.tsx +112 -0
- package/apps/shared/components/admin/settings/LoadedSettingsForm.tsx +34 -0
- package/apps/shared/components/admin/settings/MailboxCreateForm.tsx +308 -0
- package/apps/shared/components/admin/settings/MailboxPolicyForm.tsx +152 -0
- package/apps/shared/components/admin/settings/PluginsManager.tsx +1235 -0
- package/apps/shared/components/admin/settings/RetentionPolicyForm.tsx +174 -0
- package/apps/shared/components/admin/setup/EscrowSetupStep.tsx +290 -0
- package/apps/shared/components/admin/setup/SetupWizard.tsx +416 -0
- package/apps/shared/components/admin/signOut.ts +79 -0
- package/apps/shared/components/admin/usePagedList.tsx +129 -0
- package/apps/shared/components/calendar/CalendarListSidebar.tsx +120 -76
- package/apps/shared/components/calendar/EventModal.tsx +717 -513
- package/apps/shared/components/calendar/MonthView.tsx +173 -139
- package/apps/shared/components/calendar/RecurrenceEditor.tsx +222 -180
- package/apps/shared/components/calendar/SplitDayView.tsx +143 -137
- package/apps/shared/components/calendar/TimeGridView.tsx +244 -198
- package/apps/shared/components/calendar/allDay.ts +124 -0
- package/apps/shared/components/calendar/layout/CalendarShell.tsx +80 -99
- package/apps/shared/components/contacts/ContactDetailPane.tsx +100 -21
- package/apps/shared/components/contacts/ContactForm.tsx +377 -314
- package/apps/shared/components/contacts/ContactsSidebar.tsx +6 -2
- package/apps/shared/components/contacts/KeyChangeReview.tsx +192 -0
- package/apps/shared/components/contacts/contactKeys.ts +78 -0
- package/apps/shared/components/escrow/layout/EscrowShell.tsx +148 -134
- package/apps/shared/components/layout/AppShell.tsx +270 -169
- package/apps/shared/components/layout/BrandingChrome.tsx +48 -10
- package/apps/shared/components/layout/KeyEnrollmentGate.tsx +150 -26
- package/apps/shared/components/layout/MailboxProvisioning.tsx +19 -3
- package/apps/shared/components/layout/RecoveryCodeUnlock.tsx +350 -0
- package/apps/shared/components/layout/UnlockPromptProvider.tsx +257 -0
- package/apps/shared/components/mail/ConversationThreadPane.tsx +216 -181
- package/apps/shared/components/mail/MessageDetailPane.tsx +799 -76
- package/apps/shared/components/mail/compose/ComposeContext.tsx +26 -16
- package/apps/shared/components/mail/compose/ComposeWindow.tsx +1652 -765
- package/apps/shared/components/mail/compose/composeFlushRegistry.ts +60 -0
- package/apps/shared/components/mail/layout/MailShell.tsx +418 -302
- package/apps/shared/components/mail/pinnedSigners.ts +124 -0
- package/apps/shared/components/mail/verificationSeals.ts +125 -0
- package/apps/shared/components/mail/writableMailboxes.ts +101 -0
- package/apps/shared/components/rules/RuleBuilder.tsx +311 -276
- package/apps/shared/components/settings/layout/SettingsShell.tsx +1 -0
- package/apps/shared/mail/findWellKnownFolderUid.ts +13 -0
- package/apps/shared/mail/listAllPages.ts +39 -0
- package/apps/shared/search/LocalIndexLifecycle.tsx +98 -0
- package/apps/shared/search/localIndexBlockCipher.ts +83 -0
- package/apps/shared/search/localIndexBuilder.ts +481 -0
- package/apps/shared/search/localIndexKey.ts +27 -0
- package/apps/shared/search/localIndexRpcClient.ts +316 -0
- package/apps/shared/search/localIndexSchema.ts +236 -0
- package/apps/shared/search/localIndexSizePreference.ts +88 -0
- package/apps/shared/search/localIndexStorage.ts +98 -0
- package/apps/shared/search/localIndexVFS.ts +266 -0
- package/apps/shared/search/localIndexWorker.ts +926 -0
- package/apps/shared/search/searchTier2.ts +79 -0
- package/apps/shared/search/wa-sqlite-shims.d.ts +44 -0
- package/apps/www/calendar/index.tsx +438 -407
- package/apps/www/contacts/[uid].tsx +21 -2
- package/apps/www/contacts/index.tsx +148 -17
- package/apps/www/index.tsx +1314 -486
- package/apps/www/messages/[uid].tsx +23 -7
- package/apps/www/settings/auto-reply/index.tsx +134 -129
- package/apps/www/settings/booking-types/[uid].tsx +306 -300
- package/apps/www/settings/encryption/index.tsx +800 -215
- package/apps/www/settings/filters/[uid].tsx +171 -155
- package/apps/www/settings/filters/new/index.tsx +8 -1
- package/apps/www/settings/focused-inbox/index.tsx +150 -148
- package/apps/www/settings/privacy/index.tsx +494 -436
- package/apps/www/settings/read-receipts/index.tsx +148 -125
- package/apps/www/settings/sharing/index.tsx +274 -0
- package/apps/www/tasks/index.tsx +638 -530
- package/dist/apps/admin/audit-log/index.js +38 -8
- package/dist/apps/admin/branding/index.js +4 -98
- package/dist/apps/admin/data-requests/index.js +52 -72
- package/dist/apps/admin/distribution-lists/[uid].js +1 -1
- package/dist/apps/admin/domains/[uid].js +5 -68
- package/dist/apps/admin/encryption-policy/index.js +8 -0
- package/dist/apps/admin/escrow-scopes/[uid].js +91 -12
- package/dist/apps/admin/escrow-scopes/new/index.js +8 -3
- package/dist/apps/admin/index.js +17 -1
- package/dist/apps/admin/ingest-queue/index.js +24 -6
- package/dist/apps/admin/mailbox-policy/index.js +8 -0
- package/dist/apps/admin/mailboxes/[uid].js +20 -4
- package/dist/apps/admin/mailboxes/new/index.js +4 -89
- package/dist/apps/admin/plugins/index.js +6 -0
- package/dist/apps/admin/quarantine/index.js +20 -11
- package/dist/apps/admin/retention-policy/index.js +3 -36
- package/dist/apps/admin/setup/index.js +6 -0
- package/dist/apps/admin/transport-rules/[uid].js +18 -2
- package/dist/apps/admin/transport-rules/_transportRuleConfig.js +19 -0
- package/dist/apps/admin/transport-rules/index.js +22 -6
- package/dist/apps/admin/transport-rules/new/index.js +18 -2
- package/dist/apps/escrow/_layout.js +3 -3
- package/dist/apps/escrow/audit-log/index.js +26 -1
- package/dist/apps/escrow/matters/[uid].js +77 -42
- package/dist/apps/shared/components/admin/escrowScopes/EscrowScopeKeyAndHoldersFields.js +11 -2
- package/dist/apps/shared/components/admin/layout/AdminShell.js +37 -4
- package/dist/apps/shared/components/admin/mailboxes/EscrowScopeCard.js +72 -0
- package/dist/apps/shared/components/admin/mailboxes/ResourceSettingsCard.js +3 -3
- package/dist/apps/shared/components/admin/mailboxes/ShareAccessCard.js +22 -4
- package/dist/apps/shared/components/admin/settings/BrandingForm.js +116 -0
- package/dist/apps/shared/components/admin/settings/DomainDnsSetup.js +83 -0
- package/dist/apps/shared/components/admin/settings/EncryptionPolicyForm.js +62 -0
- package/dist/apps/shared/components/admin/settings/LoadedSettingsForm.js +25 -0
- package/dist/apps/shared/components/admin/settings/MailboxCreateForm.js +112 -0
- package/dist/apps/shared/components/admin/settings/MailboxPolicyForm.js +74 -0
- package/dist/apps/shared/components/admin/settings/PluginsManager.js +572 -0
- package/dist/apps/shared/components/admin/settings/RetentionPolicyForm.js +73 -0
- package/dist/apps/shared/components/admin/setup/EscrowSetupStep.js +147 -0
- package/dist/apps/shared/components/admin/setup/SetupWizard.js +208 -0
- package/dist/apps/shared/components/admin/signOut.js +75 -0
- package/dist/apps/shared/components/admin/usePagedList.js +99 -0
- package/dist/apps/shared/components/calendar/CalendarListSidebar.js +24 -13
- package/dist/apps/shared/components/calendar/EventModal.js +154 -21
- package/dist/apps/shared/components/calendar/MonthView.js +12 -5
- package/dist/apps/shared/components/calendar/RecurrenceEditor.js +40 -5
- package/dist/apps/shared/components/calendar/SplitDayView.js +10 -4
- package/dist/apps/shared/components/calendar/TimeGridView.js +23 -7
- package/dist/apps/shared/components/calendar/allDay.js +111 -0
- package/dist/apps/shared/components/calendar/layout/CalendarShell.js +47 -39
- package/dist/apps/shared/components/contacts/ContactDetailPane.js +43 -5
- package/dist/apps/shared/components/contacts/ContactForm.js +45 -12
- package/dist/apps/shared/components/contacts/ContactsSidebar.js +2 -2
- package/dist/apps/shared/components/contacts/KeyChangeReview.js +63 -0
- package/dist/apps/shared/components/contacts/contactKeys.js +64 -0
- package/dist/apps/shared/components/escrow/layout/EscrowShell.js +24 -5
- package/dist/apps/shared/components/layout/AppShell.js +100 -8
- package/dist/apps/shared/components/layout/BrandingChrome.js +45 -10
- package/dist/apps/shared/components/layout/KeyEnrollmentGate.js +63 -14
- package/dist/apps/shared/components/layout/MailboxProvisioning.js +10 -2
- package/dist/apps/shared/components/layout/RecoveryCodeUnlock.js +174 -0
- package/dist/apps/shared/components/layout/UnlockPromptProvider.js +136 -0
- package/dist/apps/shared/components/mail/ConversationThreadPane.js +50 -12
- package/dist/apps/shared/components/mail/MessageDetailPane.js +464 -34
- package/dist/apps/shared/components/mail/compose/ComposeContext.js +9 -6
- package/dist/apps/shared/components/mail/compose/ComposeWindow.js +807 -116
- package/dist/apps/shared/components/mail/compose/composeFlushRegistry.js +50 -0
- package/dist/apps/shared/components/mail/layout/MailShell.js +116 -41
- package/dist/apps/shared/components/mail/pinnedSigners.js +98 -0
- package/dist/apps/shared/components/mail/verificationSeals.js +112 -0
- package/dist/apps/shared/components/mail/writableMailboxes.js +87 -0
- package/dist/apps/shared/components/rules/RuleBuilder.js +34 -6
- package/dist/apps/shared/components/settings/layout/SettingsShell.js +1 -0
- package/dist/apps/shared/mail/findWellKnownFolderUid.js +12 -0
- package/dist/apps/shared/mail/listAllPages.js +25 -0
- package/dist/apps/shared/search/LocalIndexLifecycle.js +80 -0
- package/dist/apps/shared/search/localIndexBlockCipher.js +63 -0
- package/dist/apps/shared/search/localIndexBuilder.js +408 -0
- package/dist/apps/shared/search/localIndexKey.js +25 -0
- package/dist/apps/shared/search/localIndexRpcClient.js +241 -0
- package/dist/apps/shared/search/localIndexSchema.js +185 -0
- package/dist/apps/shared/search/localIndexSizePreference.js +78 -0
- package/dist/apps/shared/search/localIndexStorage.js +86 -0
- package/dist/apps/shared/search/localIndexVFS.js +258 -0
- package/dist/apps/shared/search/localIndexWorker.js +687 -0
- package/dist/apps/shared/search/searchTier2.js +38 -0
- package/dist/apps/www/calendar/index.js +38 -18
- package/dist/apps/www/contacts/[uid].js +13 -2
- package/dist/apps/www/contacts/index.js +94 -15
- package/dist/apps/www/index.js +770 -137
- package/dist/apps/www/messages/[uid].js +23 -7
- package/dist/apps/www/settings/auto-reply/index.js +9 -4
- package/dist/apps/www/settings/booking-types/[uid].js +17 -12
- package/dist/apps/www/settings/encryption/index.js +541 -96
- package/dist/apps/www/settings/filters/[uid].js +22 -7
- package/dist/apps/www/settings/filters/new/index.js +8 -2
- package/dist/apps/www/settings/focused-inbox/index.js +3 -1
- package/dist/apps/www/settings/privacy/index.js +57 -22
- package/dist/apps/www/settings/read-receipts/index.js +10 -3
- package/dist/apps/www/settings/sharing/index.js +124 -0
- package/dist/apps/www/tasks/index.js +76 -14
- package/package.json +3 -2
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import React, { FormEvent, useEffect, useRef, useState } from "react";
|
|
6
|
+
import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
|
|
7
|
+
import { getRetentionPolicy } from "@rapidmx/react-shared/admin/retentionPolicyApi.js";
|
|
8
|
+
import { getMailboxPolicy } from "@rapidmx/react-shared/admin/mailboxPolicyApi.js";
|
|
9
|
+
import { createDomain, Domain, listDomains } from "@rapidmx/react-shared/admin/domainsApi.js";
|
|
10
|
+
import { completeSetup, getSetupStatus, saveSetupStep, SetupStatus } from "@rapidmx/react-shared/admin/setupApi.js";
|
|
11
|
+
import { getBranding } from "@rapidmx/react-shared/branding/brandingApi.js";
|
|
12
|
+
import { EncryptionPolicy, getEncryptionPolicy } from "@rapidmx/react-shared/crypto/keyvaultApi.js";
|
|
13
|
+
import { listMailboxes, Mailbox } from "@rapidmx/react-shared/mail/mailApi.js";
|
|
14
|
+
import Alert from "@rapidmx/react-shared/components/feedback/Alert.js";
|
|
15
|
+
import Button from "@rapidmx/react-shared/components/buttons/Button.js";
|
|
16
|
+
import FormField from "@rapidmx/react-shared/components/forms/FormField.js";
|
|
17
|
+
import Modal from "@rapidmx/react-shared/components/overlays/Modal.js";
|
|
18
|
+
import BrandingForm from "../settings/BrandingForm.js";
|
|
19
|
+
import DomainDnsSetup from "../settings/DomainDnsSetup.js";
|
|
20
|
+
import EncryptionPolicyForm, { isEncryptionEnabled } from "../settings/EncryptionPolicyForm.js";
|
|
21
|
+
import LoadedSettingsForm from "../settings/LoadedSettingsForm.js";
|
|
22
|
+
import MailboxCreateForm from "../settings/MailboxCreateForm.js";
|
|
23
|
+
import MailboxPolicyForm from "../settings/MailboxPolicyForm.js";
|
|
24
|
+
import PluginsManager from "../settings/PluginsManager.js";
|
|
25
|
+
import RetentionPolicyForm from "../settings/RetentionPolicyForm.js";
|
|
26
|
+
import EscrowSetupStep from "./EscrowSetupStep.js";
|
|
27
|
+
|
|
28
|
+
const INPUT_CLASS =
|
|
29
|
+
"w-full text-sm py-2.5 px-3 border border-border rounded-sm bg-surface text-text focus:outline-none focus:border-primary";
|
|
30
|
+
|
|
31
|
+
export const SETUP_STEPS = [
|
|
32
|
+
{ id: "plugins", title: "Plugins", intro: "Choose which protocols and features this server runs. The recommended plugins are already installed." },
|
|
33
|
+
{ id: "domain", title: "Domain", intro: "Add the domain this server receives mail for, then publish the DNS records below." },
|
|
34
|
+
{ id: "settings", title: "Server settings", intro: "Set the rules for encryption, how long mail is kept, and new mailboxes." },
|
|
35
|
+
{ id: "escrow", title: "Escrow", intro: "Decide whether encrypted mail can be recovered by a trusted group of holders." },
|
|
36
|
+
{ id: "branding", title: "Branding", intro: "Make the sign-in page and apps look like your organization." },
|
|
37
|
+
{ id: "mailboxes", title: "Mailboxes", intro: "Create the first mailboxes, starting with your own." },
|
|
38
|
+
] as const;
|
|
39
|
+
|
|
40
|
+
export type SetupStepId = (typeof SETUP_STEPS)[number]["id"];
|
|
41
|
+
|
|
42
|
+
function isStepId(value: string | undefined): value is SetupStepId {
|
|
43
|
+
return SETUP_STEPS.some((step) => step.id === value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function errorMessage(err: unknown, fallback: string): string {
|
|
47
|
+
return err instanceof ApiRequestError ? err.message : fallback;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SetupWizardProps {
|
|
51
|
+
userUid: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The first-run setup wizard. Each step saves as the administrator works through it; the wizard itself only records
|
|
56
|
+
* which step they're on (so leaving and coming back resumes there) and, at the end, that setup is finished. Every step
|
|
57
|
+
* except the domain can be passed over and configured later from the admin console.
|
|
58
|
+
*/
|
|
59
|
+
export default function SetupWizard({ userUid }: SetupWizardProps) {
|
|
60
|
+
const [step, setStep] = useState<SetupStepId | null>(null);
|
|
61
|
+
const [status, setStatus] = useState<SetupStatus | undefined>();
|
|
62
|
+
const [domains, setDomains] = useState<Domain[]>([]);
|
|
63
|
+
const [domainsError, setDomainsError] = useState<string | null>(null);
|
|
64
|
+
const [encryption, setEncryption] = useState<EncryptionPolicy | null>(null);
|
|
65
|
+
const [finishing, setFinishing] = useState(false);
|
|
66
|
+
const [error, setError] = useState<string | null>(null);
|
|
67
|
+
/** Which of the settings step's forms have edits that haven't been saved. */
|
|
68
|
+
const [unsaved, setUnsaved] = useState<Record<string, boolean>>({});
|
|
69
|
+
/** The step the administrator asked to go to while the settings step had unsaved edits. */
|
|
70
|
+
const [leavingTo, setLeavingTo] = useState<SetupStepId | null>(null);
|
|
71
|
+
const [progressFailed, setProgressFailed] = useState(false);
|
|
72
|
+
/** Step saves run one at a time, and only the latest step still waiting is saved. */
|
|
73
|
+
const progress = useRef<{ running: Promise<void> | null; next: SetupStepId | null }>({ running: null, next: null });
|
|
74
|
+
|
|
75
|
+
function loadDomains(): Promise<void> {
|
|
76
|
+
return listDomains({ limit: 25 })
|
|
77
|
+
.then((list) => {
|
|
78
|
+
setDomains(list);
|
|
79
|
+
setDomainsError(null);
|
|
80
|
+
})
|
|
81
|
+
.catch((err) => setDomainsError(errorMessage(err, "Could not load your domains.")));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
void Promise.all([getSetupStatus().catch(() => undefined), loadDomains()]).then(([loaded]) => {
|
|
86
|
+
setStatus(loaded);
|
|
87
|
+
setStep(isStepId(loaded?.currentStep) ? loaded.currentStep : "plugins");
|
|
88
|
+
});
|
|
89
|
+
}, []);
|
|
90
|
+
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (step === "escrow" || step === "settings") {
|
|
93
|
+
getEncryptionPolicy()
|
|
94
|
+
.then(setEncryption)
|
|
95
|
+
.catch(() => setEncryption(null));
|
|
96
|
+
}
|
|
97
|
+
}, [step]);
|
|
98
|
+
|
|
99
|
+
if (step === null) {
|
|
100
|
+
return <p className="text-sm text-text-muted">Loading…</p>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const index = SETUP_STEPS.findIndex((s) => s.id === step);
|
|
104
|
+
const current = SETUP_STEPS[index];
|
|
105
|
+
const last = index === SETUP_STEPS.length - 1;
|
|
106
|
+
|
|
107
|
+
/** Records the step the administrator is on. Progress is a convenience for resuming, so a failure is only noted. */
|
|
108
|
+
function saveProgress(next: SetupStepId) {
|
|
109
|
+
const queue = progress.current;
|
|
110
|
+
queue.next = next;
|
|
111
|
+
if (queue.running) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
queue.running = (async () => {
|
|
115
|
+
while (queue.next) {
|
|
116
|
+
const target: SetupStepId = queue.next;
|
|
117
|
+
queue.next = null;
|
|
118
|
+
try {
|
|
119
|
+
await saveSetupStep(target);
|
|
120
|
+
setProgressFailed(false);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
console.warn("Could not save setup progress", err);
|
|
123
|
+
setProgressFailed(true);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
queue.running = null;
|
|
127
|
+
})();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function goTo(next: SetupStepId) {
|
|
131
|
+
setError(null);
|
|
132
|
+
setLeavingTo(null);
|
|
133
|
+
setUnsaved({});
|
|
134
|
+
setStep(next);
|
|
135
|
+
saveProgress(next);
|
|
136
|
+
window.scrollTo?.(0, 0);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Goes to `next`, first asking whether to discard any unsaved server settings. */
|
|
140
|
+
function requestGoTo(next: SetupStepId) {
|
|
141
|
+
// Going to the step already shown would clear the unsaved-edit tracking while the forms keep their edits.
|
|
142
|
+
if (next === step) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (step === "settings" && Object.values(unsaved).some(Boolean)) {
|
|
146
|
+
setLeavingTo(next);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
goTo(next);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const trackUnsaved = (form: string) => (dirty: boolean) => setUnsaved((prev) => ({ ...prev, [form]: dirty }));
|
|
153
|
+
|
|
154
|
+
async function finish() {
|
|
155
|
+
setFinishing(true);
|
|
156
|
+
setError(null);
|
|
157
|
+
try {
|
|
158
|
+
// Let a step still being recorded land first, so it can't arrive after setup is marked finished.
|
|
159
|
+
await progress.current.running;
|
|
160
|
+
await completeSetup();
|
|
161
|
+
window.location.href = "/admin";
|
|
162
|
+
} catch (err) {
|
|
163
|
+
setError(errorMessage(err, "Could not finish setup."));
|
|
164
|
+
setFinishing(false);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const noDomain: boolean = domains.length === 0;
|
|
169
|
+
const blocked = (step === "domain" || last) && noDomain;
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<div className="flex flex-col gap-6">
|
|
173
|
+
<div className="flex items-start justify-between gap-4">
|
|
174
|
+
<div>
|
|
175
|
+
<h1 className="text-xl font-bold uppercase tracking-wide mb-1">Set up your server</h1>
|
|
176
|
+
<p className="text-sm text-text-muted">You can change any of these settings later from the admin console.</p>
|
|
177
|
+
</div>
|
|
178
|
+
{status && !status.required && (
|
|
179
|
+
<a href="/admin" className="text-sm text-primary-dark underline whitespace-nowrap">
|
|
180
|
+
Exit setup
|
|
181
|
+
</a>
|
|
182
|
+
)}
|
|
183
|
+
</div>
|
|
184
|
+
|
|
185
|
+
{domainsError && (
|
|
186
|
+
<Alert>
|
|
187
|
+
<span className="flex flex-wrap items-center gap-2">
|
|
188
|
+
{domainsError}
|
|
189
|
+
<Button type="button" variant="secondary" className="!w-auto !py-1 !px-2 !text-xs" onClick={() => void loadDomains()}>
|
|
190
|
+
Retry
|
|
191
|
+
</Button>
|
|
192
|
+
</span>
|
|
193
|
+
</Alert>
|
|
194
|
+
)}
|
|
195
|
+
|
|
196
|
+
<ol aria-label="Setup steps" className="flex flex-wrap gap-2">
|
|
197
|
+
{SETUP_STEPS.map((s, i) => (
|
|
198
|
+
<li key={s.id}>
|
|
199
|
+
<button
|
|
200
|
+
type="button"
|
|
201
|
+
aria-current={s.id === step ? "step" : undefined}
|
|
202
|
+
// Steps after the domain need somewhere to put mail first.
|
|
203
|
+
disabled={i > 1 && noDomain}
|
|
204
|
+
onClick={() => requestGoTo(s.id)}
|
|
205
|
+
className={[
|
|
206
|
+
"text-sm py-1.5 px-3 rounded-pill border disabled:opacity-50",
|
|
207
|
+
s.id === step
|
|
208
|
+
? "bg-primary/10 border-primary text-primary-dark font-semibold"
|
|
209
|
+
: i < index
|
|
210
|
+
? "border-border text-text"
|
|
211
|
+
: "border-border text-text-muted",
|
|
212
|
+
].join(" ")}
|
|
213
|
+
>
|
|
214
|
+
{i + 1}. {s.title}
|
|
215
|
+
{i < index && <span className="sr-only"> (done)</span>}
|
|
216
|
+
</button>
|
|
217
|
+
</li>
|
|
218
|
+
))}
|
|
219
|
+
</ol>
|
|
220
|
+
|
|
221
|
+
<section aria-labelledby="setup-step-title" className="flex flex-col gap-5">
|
|
222
|
+
<div>
|
|
223
|
+
<h2 id="setup-step-title" className="text-lg font-bold">
|
|
224
|
+
Step {index + 1} of {SETUP_STEPS.length}: {current.title}
|
|
225
|
+
</h2>
|
|
226
|
+
<p className="text-sm text-text-muted">{current.intro}</p>
|
|
227
|
+
</div>
|
|
228
|
+
|
|
229
|
+
{step === "plugins" && <PluginsManager />}
|
|
230
|
+
{step === "domain" && <DomainStep domains={domains} onCreated={(domain) => setDomains((prev) => [...prev, domain])} />}
|
|
231
|
+
{step === "settings" && (
|
|
232
|
+
<div className="flex flex-col gap-10">
|
|
233
|
+
<LoadedSettingsForm load={getEncryptionPolicy} loadErrorMessage="Could not load the encryption policy.">
|
|
234
|
+
{(policy, onChange) => (
|
|
235
|
+
<EncryptionPolicyForm
|
|
236
|
+
policy={policy}
|
|
237
|
+
onDirtyChange={trackUnsaved("encryption")}
|
|
238
|
+
onChange={(updated) => {
|
|
239
|
+
onChange(updated);
|
|
240
|
+
setEncryption(updated);
|
|
241
|
+
}}
|
|
242
|
+
/>
|
|
243
|
+
)}
|
|
244
|
+
</LoadedSettingsForm>
|
|
245
|
+
<LoadedSettingsForm load={getRetentionPolicy} loadErrorMessage="Could not load the retention policy.">
|
|
246
|
+
{(policy, onChange) => <RetentionPolicyForm policy={policy} onChange={onChange} onDirtyChange={trackUnsaved("retention")} />}
|
|
247
|
+
</LoadedSettingsForm>
|
|
248
|
+
<LoadedSettingsForm load={getMailboxPolicy} loadErrorMessage="Could not load the mailbox policy.">
|
|
249
|
+
{(policy, onChange) => <MailboxPolicyForm policy={policy} onChange={onChange} onDirtyChange={trackUnsaved("mailbox")} />}
|
|
250
|
+
</LoadedSettingsForm>
|
|
251
|
+
</div>
|
|
252
|
+
)}
|
|
253
|
+
{step === "escrow" &&
|
|
254
|
+
(encryption && !isEncryptionEnabled(encryption) ? (
|
|
255
|
+
<p role="status" className="text-sm py-2 px-3 rounded-sm bg-surface-alt text-text">
|
|
256
|
+
End-to-end encryption is turned off, so escrow isn’t needed. You can set it up later if you
|
|
257
|
+
turn encryption on.
|
|
258
|
+
</p>
|
|
259
|
+
) : (
|
|
260
|
+
<EscrowSetupStep adminUid={userUid} />
|
|
261
|
+
))}
|
|
262
|
+
{step === "branding" && (
|
|
263
|
+
<LoadedSettingsForm load={getBranding} loadErrorMessage="Could not load branding.">
|
|
264
|
+
{(branding, onChange) => <BrandingForm branding={branding} onChange={onChange} />}
|
|
265
|
+
</LoadedSettingsForm>
|
|
266
|
+
)}
|
|
267
|
+
{step === "mailboxes" && <MailboxesStep userUid={userUid} domain={domains.find((d) => d.verified)?.name ?? domains[0]?.name} />}
|
|
268
|
+
</section>
|
|
269
|
+
|
|
270
|
+
{error && <Alert>{error}</Alert>}
|
|
271
|
+
|
|
272
|
+
<div className="flex items-center gap-3 border-t border-border pt-5">
|
|
273
|
+
{index > 0 && (
|
|
274
|
+
<Button type="button" variant="secondary" className="!w-auto" onClick={() => requestGoTo(SETUP_STEPS[index - 1].id)}>
|
|
275
|
+
Back
|
|
276
|
+
</Button>
|
|
277
|
+
)}
|
|
278
|
+
<div className="flex-1" />
|
|
279
|
+
{progressFailed && <span className="text-xs text-text-muted">Your place in setup couldn’t be saved.</span>}
|
|
280
|
+
{blocked && <span className="text-sm text-text-muted">{last ? "Add a domain before finishing setup." : "Add a domain to continue."}</span>}
|
|
281
|
+
{last ? (
|
|
282
|
+
<Button type="button" className="!w-auto" loading={finishing} disabled={finishing || blocked} onClick={() => void finish()}>
|
|
283
|
+
Finish setup
|
|
284
|
+
</Button>
|
|
285
|
+
) : (
|
|
286
|
+
<Button type="button" className="!w-auto" disabled={blocked} onClick={() => requestGoTo(SETUP_STEPS[index + 1].id)}>
|
|
287
|
+
Continue
|
|
288
|
+
</Button>
|
|
289
|
+
)}
|
|
290
|
+
</div>
|
|
291
|
+
|
|
292
|
+
{leavingTo && (
|
|
293
|
+
<Modal open onClose={() => setLeavingTo(null)} title="Discard unsaved changes?">
|
|
294
|
+
<p className="text-sm mb-4">Some server settings on this step have changes that haven’t been saved.</p>
|
|
295
|
+
<div className="flex gap-2 justify-end">
|
|
296
|
+
<Button type="button" variant="secondary" className="!w-auto" onClick={() => setLeavingTo(null)}>
|
|
297
|
+
Keep editing
|
|
298
|
+
</Button>
|
|
299
|
+
<Button type="button" className="!w-auto" onClick={() => goTo(leavingTo)}>
|
|
300
|
+
Discard changes
|
|
301
|
+
</Button>
|
|
302
|
+
</div>
|
|
303
|
+
</Modal>
|
|
304
|
+
)}
|
|
305
|
+
</div>
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function DomainStep({ domains, onCreated }: { domains: Domain[]; onCreated: (domain: Domain) => void }) {
|
|
310
|
+
const [name, setName] = useState("");
|
|
311
|
+
const [error, setError] = useState<string | null>(null);
|
|
312
|
+
const [saving, setSaving] = useState(false);
|
|
313
|
+
|
|
314
|
+
async function handleSubmit(e: FormEvent) {
|
|
315
|
+
e.preventDefault();
|
|
316
|
+
setError(null);
|
|
317
|
+
if (!name.trim()) {
|
|
318
|
+
setError("A domain name is required.");
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
setSaving(true);
|
|
322
|
+
try {
|
|
323
|
+
onCreated(await createDomain({ name: name.trim() }));
|
|
324
|
+
setName("");
|
|
325
|
+
} catch (err) {
|
|
326
|
+
setError(errorMessage(err, "Could not add the domain."));
|
|
327
|
+
} finally {
|
|
328
|
+
setSaving(false);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return (
|
|
333
|
+
<div className="flex flex-col gap-5 max-w-3xl">
|
|
334
|
+
{domains.map((domain) => (
|
|
335
|
+
<div key={domain.uid} className="flex flex-col gap-3">
|
|
336
|
+
<h3 className="text-base font-bold">{domain.name}</h3>
|
|
337
|
+
<DomainDnsSetup uid={domain.uid} />
|
|
338
|
+
</div>
|
|
339
|
+
))}
|
|
340
|
+
{error && <Alert>{error}</Alert>}
|
|
341
|
+
<form onSubmit={handleSubmit} className="bg-surface border border-border rounded-md p-6">
|
|
342
|
+
<FormField label={domains.length === 0 ? "Domain name" : "Add another domain"} htmlFor="setupDomainName">
|
|
343
|
+
<input
|
|
344
|
+
id="setupDomainName"
|
|
345
|
+
type="text"
|
|
346
|
+
className={INPUT_CLASS}
|
|
347
|
+
value={name}
|
|
348
|
+
onChange={(e) => setName(e.target.value)}
|
|
349
|
+
placeholder="example.com"
|
|
350
|
+
/>
|
|
351
|
+
</FormField>
|
|
352
|
+
<Button type="submit" loading={saving} disabled={saving} className="!w-auto">
|
|
353
|
+
Add domain
|
|
354
|
+
</Button>
|
|
355
|
+
</form>
|
|
356
|
+
</div>
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function MailboxesStep({ userUid, domain }: { userUid: string; domain?: string }) {
|
|
361
|
+
const [mailboxes, setMailboxes] = useState<Mailbox[]>([]);
|
|
362
|
+
const [loaded, setLoaded] = useState(false);
|
|
363
|
+
const [error, setError] = useState<string | null>(null);
|
|
364
|
+
const [formKey, setFormKey] = useState(0);
|
|
365
|
+
|
|
366
|
+
useEffect(() => {
|
|
367
|
+
listMailboxes({ limit: 100 })
|
|
368
|
+
.then((list) =>
|
|
369
|
+
// Keep any mailbox created here while the list was loading.
|
|
370
|
+
setMailboxes((prev) => [...list, ...prev.filter((mine) => !list.some((mailbox) => mailbox.uid === mine.uid))]),
|
|
371
|
+
)
|
|
372
|
+
.catch((err) =>
|
|
373
|
+
setError(
|
|
374
|
+
`${errorMessage(err, "Could not load the existing mailboxes.")} Check the Mailboxes page before creating your own, so you don't create it twice.`,
|
|
375
|
+
),
|
|
376
|
+
)
|
|
377
|
+
.finally(() => setLoaded(true));
|
|
378
|
+
}, []);
|
|
379
|
+
|
|
380
|
+
const hasOwn = mailboxes.some((mailbox) => mailbox.ownerUserUid === userUid);
|
|
381
|
+
|
|
382
|
+
return (
|
|
383
|
+
<div className="flex flex-col gap-5 max-w-xl">
|
|
384
|
+
{error && <Alert>{error}</Alert>}
|
|
385
|
+
{mailboxes.length > 0 && (
|
|
386
|
+
<div>
|
|
387
|
+
<h3 className="text-sm font-bold uppercase tracking-wide mb-2">Mailboxes created</h3>
|
|
388
|
+
<ul className="text-sm flex flex-col gap-1">
|
|
389
|
+
{mailboxes.map((mailbox) => (
|
|
390
|
+
<li key={mailbox.uid}>
|
|
391
|
+
{mailbox.displayName} <{mailbox.primarySmtpAddress}>
|
|
392
|
+
{mailbox.ownerUserUid === userUid && <span className="text-text-muted"> (yours)</span>}
|
|
393
|
+
</li>
|
|
394
|
+
))}
|
|
395
|
+
</ul>
|
|
396
|
+
</div>
|
|
397
|
+
)}
|
|
398
|
+
{!loaded ? (
|
|
399
|
+
<p className="text-sm text-text-muted">Loading…</p>
|
|
400
|
+
) : (
|
|
401
|
+
<>
|
|
402
|
+
<h3 className="text-base font-bold">{hasOwn ? "Add another mailbox" : "Your mailbox"}</h3>
|
|
403
|
+
<MailboxCreateForm
|
|
404
|
+
key={formKey}
|
|
405
|
+
submitLabel="Create mailbox"
|
|
406
|
+
defaults={hasOwn ? { domain } : { localPart: "admin", domain, displayName: "Administrator", ownerUserUid: userUid }}
|
|
407
|
+
onCreated={(mailbox) => {
|
|
408
|
+
setMailboxes((prev) => [...prev, mailbox]);
|
|
409
|
+
setFormKey((k) => k + 1);
|
|
410
|
+
}}
|
|
411
|
+
/>
|
|
412
|
+
</>
|
|
413
|
+
)}
|
|
414
|
+
</div>
|
|
415
|
+
);
|
|
416
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import { authApiFetch } from "@rapidmx/react-shared/util/api.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The cross-tab sign-out channel - must stay identical to `SIGN_OUT_CHANNEL` in
|
|
9
|
+
* `apps/shared/search/localIndexRpcClient.ts` (the channel `AppShell` listens on and `destroyAllLocalIndexes()`
|
|
10
|
+
* announces on). Duplicated rather than imported so the admin/escrow consoles don't pull the local-search Worker
|
|
11
|
+
* client into their bundles; `test/apps/admin/_components/signOut.test.ts` fails if the two ever drift apart.
|
|
12
|
+
*/
|
|
13
|
+
export const CONSOLE_SIGN_OUT_CHANNEL = "rapidmx-localsearch";
|
|
14
|
+
|
|
15
|
+
/** How long sign-out waits for auth-server's logout before navigating anyway (same bound as `AppShell`). */
|
|
16
|
+
export const CONSOLE_LOGOUT_TIMEOUT_MS = 3_000;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The local search index's pending-deletions `localStorage` key - must stay identical to `PENDING_DELETIONS_KEY`
|
|
20
|
+
* in `apps/shared/search/localIndexRpcClient.ts`, duplicated for the same bundle reason as the channel above
|
|
21
|
+
* (the test checks it). `"*"` is that module's "every index on this device" entry.
|
|
22
|
+
*/
|
|
23
|
+
export const CONSOLE_PENDING_DELETIONS_KEY = "rapidmx-localsearch-pending-deletions";
|
|
24
|
+
const ALL_LOCAL_INDEXES = "*";
|
|
25
|
+
|
|
26
|
+
/** Records that every local search index on this device must be deleted. The consoles can't delete them
|
|
27
|
+
* themselves (no local-index client here); the next mail page load retries recorded deletions before opening
|
|
28
|
+
* any index. With storage blocked nothing is recorded, and only an open mail tab hearing the broadcast deletes
|
|
29
|
+
* them. */
|
|
30
|
+
function markLocalIndexesForDeletion(): void {
|
|
31
|
+
try {
|
|
32
|
+
localStorage.setItem(CONSOLE_PENDING_DELETIONS_KEY, JSON.stringify([ALL_LOCAL_INDEXES]));
|
|
33
|
+
} catch {
|
|
34
|
+
// See the doc comment.
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Tells every other tab of this origin that the session ended (`{ type: "sign-out" }`, `AppShell`'s message).
|
|
39
|
+
* An open mail tab then destroys its unlocked keys and every local search index on the device, and leaves too.
|
|
40
|
+
* A no-op where BroadcastChannel is unavailable. */
|
|
41
|
+
function broadcastSignOut(): void {
|
|
42
|
+
if (typeof BroadcastChannel === "undefined") {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const channel = new BroadcastChannel(CONSOLE_SIGN_OUT_CHANNEL);
|
|
46
|
+
channel.postMessage({ type: "sign-out" });
|
|
47
|
+
channel.close();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Calls auth-server's logout (`POST ${authServerUrl}/api/auth/logout`, credentials included), bounded by
|
|
51
|
+
* `CONSOLE_LOGOUT_TIMEOUT_MS`. Never rejects - a failure must not keep the user from leaving. */
|
|
52
|
+
async function logOutOfAuthServer(authServerUrl: string | undefined): Promise<void> {
|
|
53
|
+
if (!authServerUrl) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const controller = new AbortController();
|
|
57
|
+
const timer = setTimeout(() => controller.abort(), CONSOLE_LOGOUT_TIMEOUT_MS);
|
|
58
|
+
try {
|
|
59
|
+
await authApiFetch(authServerUrl, "/auth/logout", { method: "POST", signal: controller.signal });
|
|
60
|
+
} catch {
|
|
61
|
+
// Navigate anyway.
|
|
62
|
+
} finally {
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Sign-out for `AdminShell`/`EscrowShell`: announces the sign-out to other tabs, ends the auth-server session
|
|
69
|
+
* (clears the auth cookie and invalidates the refresh token - navigating alone would leave the session valid),
|
|
70
|
+
* then navigates to auth-server (or `/`). Mirrors `AppShell.handleSignOut`, except that the consoles hold no
|
|
71
|
+
* unlocked keys and can't delete local search indexes in their own tab: they record every index for deletion
|
|
72
|
+
* (finished by the next mail page load) and rely on an open mail tab hearing the broadcast to delete them now.
|
|
73
|
+
*/
|
|
74
|
+
export async function signOutOfConsole(authServerUrl: string | undefined): Promise<void> {
|
|
75
|
+
markLocalIndexesForDeletion();
|
|
76
|
+
broadcastSignOut();
|
|
77
|
+
await logOutOfAuthServer(authServerUrl);
|
|
78
|
+
window.location.href = authServerUrl ?? "/";
|
|
79
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
2
|
+
// Copyright (C) 2026 Jean-Philippe Steinmetz
|
|
3
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5
|
+
import React, { useRef, useState } from "react";
|
|
6
|
+
import { ApiRequestError } from "@rapidmx/react-shared/util/api.js";
|
|
7
|
+
import Button from "@rapidmx/react-shared/components/buttons/Button.js";
|
|
8
|
+
|
|
9
|
+
/** Page size for the async-request lists (the server returns them newest first and caps `limit` at 500). */
|
|
10
|
+
export const REQUEST_LIST_PAGE_SIZE = 50;
|
|
11
|
+
|
|
12
|
+
export interface PagedList<T> {
|
|
13
|
+
items: T[];
|
|
14
|
+
/** `true` until the first page has loaded (or failed). */
|
|
15
|
+
loading: boolean;
|
|
16
|
+
loadError: string | null;
|
|
17
|
+
/** Whether the last page fetched was full, so another may follow. */
|
|
18
|
+
hasMore: boolean;
|
|
19
|
+
loadingMore: boolean;
|
|
20
|
+
/** Re-fetches from the first page, dropping anything loaded beyond it. */
|
|
21
|
+
reload: () => Promise<void>;
|
|
22
|
+
/** Fetches the next page and appends what's new in it. */
|
|
23
|
+
loadMore: () => Promise<void>;
|
|
24
|
+
/** Replaces one already-loaded item in place (e.g. with an action's response). */
|
|
25
|
+
replaceItem: (item: T) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A newest-first list fetched a page at a time with "Load more". Only the most recently started `reload()`'s
|
|
30
|
+
* responses are applied, so a slow earlier fetch (e.g. the mount fetch racing a reload after a create) can't
|
|
31
|
+
* overwrite a newer list, and a "Load more" that was in flight during a reload is dropped.
|
|
32
|
+
*
|
|
33
|
+
* Paging is by offset, so "Load more" re-reads the previous page alongside the next one and continues after the last
|
|
34
|
+
* row already shown: rows created since shift older rows onto later pages (repeats are skipped), and rows deleted
|
|
35
|
+
* since shift them onto earlier pages (which a plain next-page fetch would silently skip). If that row can't be found
|
|
36
|
+
* in the two pages at all (it was deleted, or more than a page of rows came or went), the list reloads from the start.
|
|
37
|
+
*/
|
|
38
|
+
export function usePagedList<T extends { uid: string }>(
|
|
39
|
+
fetchPage: (params: { limit: number; page: number }) => Promise<T[]>,
|
|
40
|
+
errorMessage: string,
|
|
41
|
+
): PagedList<T> {
|
|
42
|
+
const pageSize = REQUEST_LIST_PAGE_SIZE;
|
|
43
|
+
const [items, setItems] = useState<T[]>([]);
|
|
44
|
+
const [loading, setLoading] = useState(true);
|
|
45
|
+
const [loadError, setLoadError] = useState<string | null>(null);
|
|
46
|
+
const [hasMore, setHasMore] = useState(false);
|
|
47
|
+
const [loadingMore, setLoadingMore] = useState(false);
|
|
48
|
+
const generation = useRef(0);
|
|
49
|
+
const nextPage = useRef(0);
|
|
50
|
+
const loaded = useRef<T[]>(items);
|
|
51
|
+
loaded.current = items;
|
|
52
|
+
|
|
53
|
+
function describe(err: unknown): string {
|
|
54
|
+
return err instanceof ApiRequestError ? err.message : errorMessage;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function reload() {
|
|
58
|
+
const current = ++generation.current;
|
|
59
|
+
try {
|
|
60
|
+
const data = await fetchPage({ limit: pageSize, page: 0 });
|
|
61
|
+
if (current !== generation.current) return;
|
|
62
|
+
setItems(data);
|
|
63
|
+
setHasMore(data.length === pageSize);
|
|
64
|
+
setLoadError(null);
|
|
65
|
+
nextPage.current = 1;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
if (current === generation.current) setLoadError(describe(err));
|
|
68
|
+
} finally {
|
|
69
|
+
if (current === generation.current) setLoading(false);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function loadMore() {
|
|
74
|
+
const current = generation.current;
|
|
75
|
+
const page = nextPage.current;
|
|
76
|
+
setLoadingMore(true);
|
|
77
|
+
try {
|
|
78
|
+
const [previous, data] = await Promise.all([
|
|
79
|
+
fetchPage({ limit: pageSize, page: page - 1 }),
|
|
80
|
+
fetchPage({ limit: pageSize, page }),
|
|
81
|
+
]);
|
|
82
|
+
if (current !== generation.current) return;
|
|
83
|
+
const window: T[] = [...previous, ...data];
|
|
84
|
+
// "Load more" is only offered after a full page, so at least one row is shown.
|
|
85
|
+
const lastShown: string = loaded.current[loaded.current.length - 1].uid;
|
|
86
|
+
const boundary: number = window.map((item) => item.uid).lastIndexOf(lastShown);
|
|
87
|
+
if (boundary === -1) {
|
|
88
|
+
await reload();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const after: T[] = window.slice(boundary + 1);
|
|
92
|
+
setItems((prev) => [...prev, ...after.filter((item) => !prev.some((existing) => existing.uid === item.uid))]);
|
|
93
|
+
setHasMore(data.length === pageSize);
|
|
94
|
+
setLoadError(null);
|
|
95
|
+
nextPage.current = page + 1;
|
|
96
|
+
} catch (err) {
|
|
97
|
+
if (current === generation.current) setLoadError(describe(err));
|
|
98
|
+
} finally {
|
|
99
|
+
setLoadingMore(false);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function replaceItem(item: T) {
|
|
104
|
+
setItems((prev) => prev.map((existing) => (existing.uid === item.uid ? item : existing)));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return { items, loading, loadError, hasMore, loadingMore, reload, loadMore, replaceItem };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The "Load more" button for a `usePagedList()` list - renders nothing once there's nothing more to load. */
|
|
111
|
+
export function LoadMoreButton({ list, label }: { list: Pick<PagedList<unknown>, "hasMore" | "loadingMore" | "loadMore">; label: string }) {
|
|
112
|
+
if (!list.hasMore) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
return (
|
|
116
|
+
<div className="mt-3">
|
|
117
|
+
<Button
|
|
118
|
+
type="button"
|
|
119
|
+
variant="secondary"
|
|
120
|
+
className="!w-auto"
|
|
121
|
+
loading={list.loadingMore}
|
|
122
|
+
disabled={list.loadingMore}
|
|
123
|
+
onClick={() => void list.loadMore()}
|
|
124
|
+
>
|
|
125
|
+
{label}
|
|
126
|
+
</Button>
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
}
|