Load the composer, previews, dialogs and other sidebars on demand
The main chunk carried everything the mail view might open: the file preview and its Markdown renderer, the composer and its editor, the contact editor, the filter and share dialogs, and the calendar, contacts and files sidebars. Each is now loaded when first shown. The composer is also fetched when the browser is idle after startup, so the first Compose does not wait on the network. Import the notification helpers statically where they already were: the dynamic imports beside those static ones split nothing.
This commit is contained in:
@@ -1,7 +1,20 @@
|
||||
import { lazy, Suspense } from "react";
|
||||
import { useCompose } from "@/store/compose";
|
||||
import { Composer } from "./Composer";
|
||||
import { useIsMobile } from "@/ui/misc";
|
||||
|
||||
/*
|
||||
* The composer -- the rich-text editor, the recipient and file pickers -- is
|
||||
* loaded apart from the mail view, and fetched while the browser is idle
|
||||
* after startup so the first Compose does not wait on the network.
|
||||
*/
|
||||
const loadComposer = () => import("./Composer");
|
||||
const Composer = lazy(() => loadComposer().then((m) => ({ default: m.Composer })));
|
||||
if (typeof window !== "undefined") {
|
||||
const warm = () => void loadComposer().catch(() => {});
|
||||
if ("requestIdleCallback" in window) window.requestIdleCallback(warm, { timeout: 5000 });
|
||||
else setTimeout(warm, 2000);
|
||||
}
|
||||
|
||||
export function ComposerDock() {
|
||||
const drafts = useCompose((s) => s.drafts);
|
||||
const activeKey = useCompose((s) => s.activeKey);
|
||||
@@ -13,9 +26,11 @@ export function ComposerDock() {
|
||||
const hasMaximized = !isMobile && drafts.some((d) => d.maximized && !d.minimized);
|
||||
return (
|
||||
<div className={`composer-dock${hasMaximized ? " has-maximized" : ""}`}>
|
||||
{visible.map((d) => (
|
||||
<Composer key={d.key} draft={isMobile && d.key !== activeKey ? { ...d, minimized: true } : d} />
|
||||
))}
|
||||
<Suspense fallback={null}>
|
||||
{visible.map((d) => (
|
||||
<Composer key={d.key} draft={isMobile && d.key !== activeKey ? { ...d, minimized: true } : d} />
|
||||
))}
|
||||
</Suspense>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -42,35 +42,39 @@ describe("ComposerDock with a full-screen composer", () => {
|
||||
useCompose.setState({ drafts: [], activeKey: null });
|
||||
});
|
||||
|
||||
const render = (drafts: Draft[], activeKey: string) => {
|
||||
// The composer is loaded on demand, so rendering waits for it to arrive.
|
||||
const render = async (drafts: Draft[], activeKey: string) => {
|
||||
useCompose.setState({ drafts, activeKey });
|
||||
act(() => root.render(<ComposerDock />));
|
||||
await act(async () => {
|
||||
root.render(<ComposerDock />);
|
||||
await import("../Composer");
|
||||
});
|
||||
};
|
||||
const dock = () => host.querySelector(".composer-dock")!;
|
||||
|
||||
it("marks the dock so the other composers are hidden behind it", () => {
|
||||
it("marks the dock so the other composers are hidden behind it", async () => {
|
||||
setWidth(1300);
|
||||
render([draft("a"), draft("b", { maximized: true }), draft("c")], "b");
|
||||
await render([draft("a"), draft("b", { maximized: true }), draft("c")], "b");
|
||||
expect(dock().classList.contains("has-maximized")).toBe(true);
|
||||
// Every composer stays mounted: the hiding is the stylesheet's, so nothing being typed elsewhere is lost.
|
||||
expect(host.querySelectorAll(".composer").length).toBe(3);
|
||||
});
|
||||
|
||||
it("leaves the dock alone while nobody is full screen", () => {
|
||||
it("leaves the dock alone while nobody is full screen", async () => {
|
||||
setWidth(1300);
|
||||
render([draft("a"), draft("b")], "b");
|
||||
await render([draft("a"), draft("b")], "b");
|
||||
expect(dock().classList.contains("has-maximized")).toBe(false);
|
||||
});
|
||||
|
||||
it("does not count a full-screen composer that has since been minimized", () => {
|
||||
it("does not count a full-screen composer that has since been minimized", async () => {
|
||||
setWidth(1300);
|
||||
render([draft("a"), draft("b", { maximized: true, minimized: true })], "a");
|
||||
await render([draft("a"), draft("b", { maximized: true, minimized: true })], "a");
|
||||
expect(dock().classList.contains("has-maximized")).toBe(false);
|
||||
});
|
||||
|
||||
it("is not a phone concern: there the active composer is already the only one open", () => {
|
||||
it("is not a phone concern: there the active composer is already the only one open", async () => {
|
||||
setWidth(400);
|
||||
render([draft("a"), draft("b", { maximized: true })], "b");
|
||||
await render([draft("a"), draft("b", { maximized: true })], "b");
|
||||
expect(dock().classList.contains("has-maximized")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user