Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): keep the chat's draft and reply when closing the panel

Elian Doranceb06b8b3c2a
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx
new file mode 100644
index 0000000000..13b60ed460
--- /dev/null
+++ b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.spec.tsx
@@ -0,0 +1,111 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import type FNote from "../../entities/fnote";
+
+const shownNote = vi.hoisted(() => ({ current: null as FNote | null }));
+vi.mock("../react/hooks", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("../react/hooks")>()),
+ useActiveNoteContext: () => ({ note: shownNote.current }),
+ useNoteContext: () => ({ note: shownNote.current, noteContext: null }),
+ useStaticTooltip: vi.fn()
+}));
+
+// Renders its children by `Modal`'s own rule (`show || keepInDom`) and hands out `onHidden`, so that
+// closing the panel needs no Bootstrap.
+const modal = vi.hoisted(() => ({ onHidden: null as (() => void) | null }));
+vi.mock("../react/master_detail", async () => {
+ const { h } = await import("preact");
+ return {
+ MasterDetailModal: ({ show, keepInDom, onHidden, children }: {
+ show: boolean, keepInDom?: boolean, onHidden: () => void, children: preact.ComponentChildren
+ }) => {
+ modal.onHidden = onHidden;
+ return (show || keepInDom) ? h("div", { className: "modal-stub" }, children) : null;
+ }
+ };
+});
+
+vi.mock("../sidebar/AttributeList", () => stubWidget("attribute-list-stub"));
+vi.mock("../sidebar/TableOfContents", () => stubWidget("toc-stub"));
+vi.mock("../sidebar/HighlightsList", () => stubWidget("highlights-stub"));
+vi.mock("../sidebar/NoteMap", () => stubWidget("note-map-stub"));
+vi.mock("../sidebar/NotePaths", () => stubWidget("note-paths-stub"));
+vi.mock("../sidebar/Backlinks", () => stubWidget("backlinks-stub"));
+vi.mock("../sidebar/BoardColumns", () => stubWidget("board-columns-stub"));
+vi.mock("../sidebar/ChatHighlightsList", () => stubWidget("chat-highlights-stub"));
+vi.mock("../sidebar/pdf/PdfPages", () => stubWidget("pdf-pages-stub"));
+vi.mock("../sidebar/pdf/PdfAttachments", () => stubWidget("pdf-attachments-stub"));
+vi.mock("../sidebar/pdf/PdfLayers", () => stubWidget("pdf-layers-stub"));
+vi.mock("../sidebar/pdf/PdfAnnotations", () => stubWidget("pdf-annotations-stub"));
+
+async function stubWidget(className: string) {
+ const { h } = await import("preact");
+ return { default: () => h("div", { className }) };
+}
+
+import appContext from "../../components/app_context";
+import type Component from "../../components/component";
+import options from "../../services/options";
+import { buildNote } from "../../test/easy-froca";
+import { ParentComponent } from "../react/react_utils";
+import NoteMap from "../sidebar/NoteMap";
+import PdfPages from "../sidebar/pdf/PdfPages";
+import type { RightPaneTabContents } from "../sidebar/RightPanelContainer";
+import MobileNotePanel, { withFixedHeightWidgetsLast } from "./MobileNotePanel";
+
+describe("withFixedHeightWidgetsLast", () => {
+ it("moves the note map and the PDF pages last, keeping the order within each group", () => {
+ const first = <div key="first" />;
+ const second = <div key="second" />;
+ const map = <NoteMap key="map" />;
+ const pages = <PdfPages key="pages" />;
+ const tab = (items: RightPaneTabContents["items"]) => ({ id: "outline", items } as RightPaneTabContents);
+
+ expect(withFixedHeightWidgetsLast(tab([ pages, first, map, second ])).items)
+ .toEqual([ first, second, pages, map ]);
+ expect(withFixedHeightWidgetsLast(tab([ map ])).items).toEqual([ map ]);
+ expect(withFixedHeightWidgetsLast(tab([])).items).toEqual([]);
+ });
+});
+
+describe("MobileNotePanel", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ options.set("highlightsList", "[]");
+ options.set("aiEnabled", "false");
+ appContext.tabManager = { getActiveContext: () => null } as typeof appContext.tabManager;
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ act(() => render(null, container));
+ container.remove();
+ for (const stub of document.body.querySelectorAll(".modal-stub")) stub.remove();
+ });
+
+ it("mounts the tabs on the first open and keeps them mounted once closed", () => {
+ shownNote.current = buildNote({ title: "Read" });
+ const parent = { componentId: "cid", registerHandler: () => {}, removeHandler: () => {} } as unknown as Component;
+ const widgetsByParent = { getLegacyWidgets: () => [], getPreactWidgets: () => [] };
+ act(() => {
+ render((
+ <ParentComponent.Provider value={parent}>
+ <MobileNotePanel widgetsByParent={widgetsByParent as never} />
+ </ParentComponent.Provider>
+ ), container);
+ });
+ expect(document.body.querySelector(".toc-stub")).toBeNull();
+
+ const button = container.querySelector("button");
+ expect(button).not.toBeNull();
+ act(() => button?.click());
+ expect(document.body.querySelector(".toc-stub")).not.toBeNull();
+
+ act(() => modal.onHidden?.());
+ expect(document.body.querySelector(".toc-stub")).not.toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx
index daa8d50b5c..f451cd97ed 100644
--- a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx
+++ b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx
@@ -54,7 +54,11 @@ function MobileNotePanelModal({ shown, onHidden, widgetsByParent, selectedTabId,
selectedTabId: RightPaneTabId | undefined;
setSelectedTabId: (tabId: RightPaneTabId) => void;
}) {
- const tabs = useRightPaneTabs(shown, widgetsByParent).map(withFixedHeightWidgetsLast);
+ // The tabs stay mounted once opened, so that closing the panel to read the note keeps the chat's
+ // draft and the reply it is receiving.
+ const opened = useRef(false);
+ opened.current ||= shown;
+ const tabs = useRightPaneTabs(opened.current, widgetsByParent).map(withFixedHeightWidgetsLast);
const activeTab = tabs.find((tab) => tab.id === selectedTabId) ?? tabs[0];
const [ headerSlots, setHeaderSlotRef ] = useHeaderSlots();
// One slot per tab, only the active one shown; a tab that fills its slot replaces the title.
@@ -85,6 +89,7 @@ function MobileNotePanelModal({ shown, onHidden, widgetsByParent, selectedTabId,
backTitle={t("attribute_list_panel.back")}
ariaLabel={t("mobile_detail_menu.note_panel")}
show={shown}
+ keepInDom={opened.current}
onHidden={onHidden}
>
{/* The widgets' card, TOC and highlight styles are scoped to `#right-pane`, which the
@@ -119,7 +124,7 @@ function useHeaderSlots() {
* Moves `NoteMap` and `PdfPages` to the end of the tab: both are drawn to a fixed height, which in a
* tab that scrolls as a whole would sit between the sections around them.
*/
-function withFixedHeightWidgetsLast(tab: RightPaneTabContents): RightPaneTabContents {
+export function withFixedHeightWidgetsLast(tab: RightPaneTabContents): RightPaneTabContents {
const isFixedHeight = (item: RightPaneTabContents["items"][number]) => item.type === NoteMap || item.type === PdfPages;
return {
...tab,