Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(notes): keep the empty note switcher away while a save is pending
Elian Doran7627cc091de3
diff --git a/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsx b/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsxindex 1fde3b7492..6eb2063d61 100644--- a/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsx+++ b/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsx@@ -3,15 +3,21 @@ import { act } from "preact/test-utils";import { afterEach, describe, expect, it, vi } from "vitest";import Component from "../../components/component.js";+import NoteContext, { type SaveState } from "../../components/note_context.js";import type { MenuCommandItem, MenuItem } from "../../menus/context_menu.js";import type { TreeCommandNames } from "../../menus/tree_context_menu.js";import attributes from "../../services/attributes.js";import type { NoteTypeData } from "../../services/note_types.js";import server from "../../services/server.js";import { buildNote } from "../../test/easy-froca.js";-import { ParentComponent } from "../react/react_utils.js";+import { noteSavedDataStore } from "../react/NoteStore.js";+import { NoteContextContext, ParentComponent } from "../react/react_utils.js";import NoteTypeSwitcher, { NoteTypeBadges, toSwitcherItems, useNoteTypeData } from "./NoteTypeSwitcher.js";+vi.mock("../../services/i18n.js", () => ({+ t: (key: string) => key+}));+let container: HTMLDivElement | undefined;afterEach(() => {@@ -32,6 +38,32 @@ describe("NoteTypeSwitcher", () => {.querySelector(".note-type-switcher")).toBeNull();});+ it("steps back while the note has changes not yet saved, which a switch would drop", async () => {+ vi.spyOn(server, "get").mockResolvedValue({ templateNoteIds: [], newTemplateNoteIds: [] });+ buildNote({ id: "_templates", title: "Templates" });+ const note = buildNote({ id: "pendingNote", title: "Note", type: "text" });+ noteSavedDataStore.set("pendingNote", "");+ const noteContext = new NoteContext("pending-ntx");+ const host = new Component();+ const intro = () => container?.querySelector(".note-type-switcher .intro");+ const deliver = (state: SaveState) => act(() => {+ host.handleEvent("contextDataChanged", { noteContext, key: "saveState", value: { state } });+ });++ mount(<NoteContextContext.Provider value={noteContext}><NoteTypeSwitcher note={note} /></NoteContextContext.Provider>, host);+ await flush();+ expect(intro()).not.toBeNull();++ for (const state of [ "unsaved", "saving", "error" ] as const) {+ deliver(state);+ expect(intro(), state).toBeNull();+ }++ // Saved and still empty, the note can be switched again.+ deliver("saved");+ expect(intro()).not.toBeNull();+ });+it("offers Markdown and Canvas as pills, and the code languages, collections, templates and the rest from dropdowns", async () => {buildNote({id: "_templates",diff --git a/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx b/apps/client/src/widgets/layout/NoteTypeSwitcher.tsxindex 128b0679b1..427150d697 100644--- a/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx+++ b/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx@@ -12,7 +12,9 @@ import { applyNotePreset } from "../../services/note_presets";import note_types, { MARKDOWN_NOTE_TYPE_MIME, NOTE_TYPES, type NoteTypeData } from "../../services/note_types";import server from "../../services/server";import { Badge, BadgeWithDropdown } from "../react/Badge";-import { useNoteProperty, useNoteSavedData, useTriliumEvent } from "../react/hooks";+import {+ useGetContextDataFrom, useNoteContext, useNoteProperty, useNoteSavedData, useTriliumEvent+} from "../react/hooks";import { onWheelHorizontalScroll } from "../widget_utils";/** The note types offered as a pill of their own, each a single click away. */@@ -23,6 +25,10 @@ const PINNED_NOTE_TYPES: { type: NoteType, mime?: string }[] = [export default function NoteTypeSwitcher({ note }: { note?: FNote | null }) {const blob = useNoteSavedData(note?.noteId);+ const { noteContext } = useNoteContext();+ // A switch drops the editor's pending save, so the switcher waits for it to land.+ const saveState = useGetContextDataFrom(noteContext, "saveState")?.state;+ const isSaved = !saveState || saveState === "saved";const currentNoteType = useNoteProperty(note, "type");const currentNoteTypeData = NOTE_TYPES.find(t => t.type === currentNoteType);@@ -33,7 +39,7 @@ export default function NoteTypeSwitcher({ note }: { note?: FNote | null }) {className="note-type-switcher"onWheel={onWheelHorizontalScroll}>- {note && blob?.length === 0 && (+ {note && blob?.length === 0 && isSaved && (<><div className="intro">{t("note_title.note_type_switcher_label", { type: currentNoteTypeData?.title.toLocaleLowerCase() })}</div><NoteTypeBadges noteId={note.noteId} />