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
client/options: add a related actions section
Adorian Doranb44a87585084
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 06c892d527..03db613b98 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3117,7 +3117,12 @@"dismiss": "Dismiss"},"settings": {- "related_settings": "Related settings"+ "related_settings": "Related settings",+ "related_actions": "Related actions"+ },+ "settings_other": {+ "related_space_usage": "Analyze the space usage within the database",+ "related_space_usage_description": "See which notes, revisions and attachments take up the most space, and reclaim it."},"settings_appearance": {"related_code_blocks": "Color scheme for code blocks in text notes",diff --git a/apps/client/src/widgets/type_widgets/options/components/RelatedSettings.tsx b/apps/client/src/widgets/type_widgets/options/components/RelatedSettings.tsxindex b0762ca34d..83c9079f5b 100644--- a/apps/client/src/widgets/type_widgets/options/components/RelatedSettings.tsx+++ b/apps/client/src/widgets/type_widgets/options/components/RelatedSettings.tsx@@ -11,14 +11,23 @@ interface RelatedSettingsItem {targetPage?: OptionPages;/** Link to an arbitrary hidden-subtree note (e.g. `_taskStates`). */targetNoteId?: string;+ /**+ * Navigates by itself, for an entry that has something to do before or instead of following its+ * link. It has to stop the event, since the global link handler would otherwise navigate as+ * well — and for the same reason the entry opts out of the dialog's contained navigation, which+ * would swallow the click before it ever arrives here.+ */+ onClick?: (e: MouseEvent) => void;enabled?: boolean;}interface RelatedSettingsProps {items: RelatedSettingsItem[];+ /** Heading of the section, when these entries are something other than related settings. */+ title?: string;}-export default function RelatedSettings({ items }: RelatedSettingsProps) {+export default function RelatedSettings({ items, title }: RelatedSettingsProps) {const filteredItems = items.filter(item => item.enabled !== false);if (filteredItems.length === 0) {@@ -26,9 +35,9 @@ export default function RelatedSettings({ items }: RelatedSettingsProps) {}return (- <OptionsSection title={t("settings.related_settings")}>+ <OptionsSection title={title ?? t("settings.related_settings")}>{filteredItems.map((item) => {- const { targetPage, targetNoteId } = item;+ const { targetPage, targetNoteId, onClick } = item;return (<OptionsRowLinkkey={targetPage ?? targetNoteId}@@ -37,8 +46,8 @@ export default function RelatedSettings({ items }: RelatedSettingsProps) {href={targetNoteId? `#root/_hidden/${targetNoteId}`: `#root/_hidden/_options/${targetPage}`}- noContainedNavigation={!!targetNoteId}- onClick={targetNoteId+ noContainedNavigation={!!targetNoteId || !!onClick}+ onClick={onClick ?? (targetNoteId? (e) => {// Hidden-subtree config notes open hoisted, in a tree-sidebar popup.// stopPropagation keeps the global `a` click handler@@ -47,7 +56,7 @@ export default function RelatedSettings({ items }: RelatedSettingsProps) {e.stopPropagation();void appContext.triggerCommand("openInTreePopup", { noteIdOrPath: targetNoteId, hoistedNoteId: targetNoteId });}- : undefined}+ : undefined)}/>);})}diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/index.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/index.spec.tsxnew file mode 100644index 0000000000..4c6c87e8c4--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/content_manager/index.spec.tsx@@ -0,0 +1,51 @@+import { render } from "preact";+import { afterEach, describe, expect, it, vi } from "vitest";++// The sections themselves pull in the whole listing/treemap machinery, and this is about which one+// the shell picks — stubs are enough to tell them apart.+vi.mock("./active_content", () => ({+ default: () => <div className="active-content-stub" />+}));++vi.mock("./space_usage", () => ({+ default: () => <div className="space-usage-stub" />+}));++// Import AFTER the mocks (vi.mock is hoisted, but the component import must resolve the mocked deps).+import type { TypeWidgetProps } from "../../type_widget";+import ContentManagerSettings, { requestContentManagerSection } from "./index";++let container: HTMLDivElement | undefined;++/** Opens the page as navigating to it would, and answers which section it landed on. */+function openPage() {+ container = document.createElement("div");+ document.body.appendChild(container);+ render(<ContentManagerSettings {...({} as TypeWidgetProps)} />, container);++ return container.querySelector(".space-usage-stub") ? "spaceUsage" : "activeContent";+}++afterEach(() => {+ if (container) {+ render(null, container);+ container.remove();+ container = undefined;+ }+});++describe("Content Manager section choice", () => {+ it("opens on Active Content, and on the requested section once asked", () => {+ expect(openPage()).toBe("activeContent");++ requestContentManagerSection("spaceUsage");+ expect(openPage()).toBe("spaceUsage");+ });++ it("forgets the request once taken, so opening the page again starts over", () => {+ requestContentManagerSection("spaceUsage");+ expect(openPage()).toBe("spaceUsage");++ expect(openPage()).toBe("activeContent");+ });+});diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/index.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/index.tsxindex f1d8bbde80..16550cc0f1 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager/index.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager/index.tsx@@ -9,7 +9,7 @@ import type { TypeWidgetProps } from "../../type_widget";import ActiveContent from "./active_content";import SpaceUsage from "./space_usage";-type ContentManagerSection = "activeContent" | "spaceUsage";+export type ContentManagerSection = "activeContent" | "spaceUsage";/** What the shell hands each section: the page's own props plus the switcher between sections. */export interface ContentManagerSectionProps extends TypeWidgetProps {@@ -23,10 +23,11 @@ export interface ContentManagerSectionProps extends TypeWidgetProps {/*** The Content Manager page: a shell hosting the Active Content listing and the Space Usage tooling,* switched by a segmented button. Each section owns its header and toolbar — the shell holds nothing- * but the choice, which starts over on Active Content each time the page opens.+ * but the choice, which starts over on Active Content each time the page opens, unless the page was+ * opened by a link asking for a particular section.*/export default function ContentManagerSettings(props: TypeWidgetProps) {- const [ section, setSection ] = useState<ContentManagerSection>("activeContent");+ const [ section, setSection ] = useState<ContentManagerSection>(takeRequestedSection);const sectionSwitcher = (<div className="content-manager-section-switcher"><SegmentedChoice options={SECTIONS} currentValue={section} onChange={setSection} />@@ -42,3 +43,22 @@ const SECTIONS: { value: ContentManagerSection, label: string }[] = [{ value: "activeContent", label: t("content_manager.section_active_content") },{ value: "spaceUsage", label: t("content_manager.section_space_usage") }];++/** The section a link asked for, until the page renders and takes it. */+let requestedSection: ContentManagerSection | null = null;++/**+ * Asks the page to open on a given section, for links pointing at a section rather than at the page+ * itself (the "Related actions" entry in Options → Other). The request is taken by the next render+ * and forgotten, so opening the page any other way still starts on Active Content.+ */+export function requestContentManagerSection(section: ContentManagerSection) {+ requestedSection = section;+}++function takeRequestedSection(): ContentManagerSection {+ const requested = requestedSection ?? "activeContent";++ requestedSection = null;+ return requested;+}diff --git a/apps/client/src/widgets/type_widgets/options/other.tsx b/apps/client/src/widgets/type_widgets/options/other.tsxindex be2e687818..e44bed4c8c 100644--- a/apps/client/src/widgets/type_widgets/options/other.tsx+++ b/apps/client/src/widgets/type_widgets/options/other.tsx@@ -8,11 +8,13 @@ import toast from "../../../services/toast";import Button from "../../react/Button";import FormText from "../../react/FormText";import { FormTextBoxWithUnit } from "../../react/FormTextBox";-import { useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";+import { useNoteContext, useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";import OptionsPageHeader from "./components/OptionsPageHeader";import OptionsRow, { OptionsRowWithButton, OptionsRowWithToggle } from "./components/OptionsRow";import OptionsSection from "./components/OptionsSection";+import RelatedSettings from "./components/RelatedSettings";import TimeSelector from "./components/TimeSelector";+import { requestContentManagerSection } from "./content_manager";export default function OtherSettings() {return (@@ -25,10 +27,38 @@ export default function OtherSettings() {<HtmlImportTags /><ShareSettings /><NetworkSettings />+ <RelatedActions /></>);}+function RelatedActions() {+ const { noteContext } = useNoteContext();++ return (+ <RelatedSettings+ title={t("settings.related_actions")}+ items={[+ {+ title: t("settings_other.related_space_usage"),+ description: t("settings_other.related_space_usage_description"),+ targetPage: "_optionsContentManager",+ onClick: (e) => {+ // The Content Manager opens on Active Content unless asked otherwise, and+ // navigating here rather than by the href is what lets us ask first.+ e.preventDefault();+ e.stopPropagation();+ requestContentManagerSection("spaceUsage");+ // Kept in whichever context this page is shown in — the settings dialog+ // holds a note context of its own, outside the tab manager.+ void noteContext?.setNote("_optionsContentManager", { keepActiveDialog: true });+ }+ }+ ]}+ />+ );+}+function SearchSettings() {const [ fuzzyEnabled, setFuzzyEnabled ] = useTriliumOptionBool("searchEnableFuzzyMatching");const [ autocompleteFuzzy, setAutocompleteFuzzy ] = useTriliumOptionBool("searchAutocompleteFuzzy");