From 1e15585a24c57c369b3ee8d5a312cf69c2d3f42b Mon Sep 17 00:00:00 2001 From: Jakob Schlanstedt Date: Wed, 22 Oct 2025 00:53:36 +0200 Subject: [PATCH] refactor(typeerror): resolve typeerrors by refactoring code --- apps/client/src/components/entrypoints.ts | 4 +- .../src/components/main_tree_executors.ts | 4 +- .../src/components/root_command_executor.ts | 2 +- apps/client/src/menus/tree_context_menu.ts | 5 +- apps/client/src/services/note_autocomplete.ts | 21 +++++---- apps/client/src/services/note_create.ts | 47 ++++++++++++------- .../src/widgets/collections/board/api.ts | 9 ++-- .../widgets/collections/board/context_menu.ts | 11 ++++- .../widgets/collections/table/context_menu.ts | 7 +-- .../widgets/collections/table/row_editing.ts | 1 - .../mobile_widgets/mobile_detail_menu.tsx | 2 +- apps/client/src/widgets/note_tree.ts | 4 +- .../ribbon/components/AttributeEditor.tsx | 8 ++-- 13 files changed, 73 insertions(+), 52 deletions(-) diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts index 96721e26c..65d9af7e7 100644 --- a/apps/client/src/components/entrypoints.ts +++ b/apps/client/src/components/entrypoints.ts @@ -11,7 +11,7 @@ import froca from "../services/froca.js"; import linkService from "../services/link.js"; import { t } from "../services/i18n.js"; import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons"; -import noteCreateService, { CreateNoteTarget } from "../services/note_create.js"; +import noteCreateService, { CreateNoteIntoInboxURLOpts, CreateNoteTarget } from "../services/note_create.js"; export default class Entrypoints extends Component { constructor() { @@ -26,7 +26,7 @@ export default class Entrypoints extends Component { async createNoteIntoInboxCommand() { await noteCreateService.createNote( - CreateNoteTarget.IntoInbox + { target: CreateNoteTarget.IntoInbox } as CreateNoteIntoInboxURLOpts ); } diff --git a/apps/client/src/components/main_tree_executors.ts b/apps/client/src/components/main_tree_executors.ts index d8ced149c..81068785c 100644 --- a/apps/client/src/components/main_tree_executors.ts +++ b/apps/client/src/components/main_tree_executors.ts @@ -49,8 +49,8 @@ export default class MainTreeExecutors extends Component { } await noteCreateService.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: activeNoteContext.notePath, isProtected: activeNoteContext.note.isProtected, saveSelection: false, @@ -78,8 +78,8 @@ export default class MainTreeExecutors extends Component { } await noteCreateService.createNote( - CreateNoteTarget.AfterNoteURL, { + target: CreateNoteTarget.AfterNoteURL, parentNoteUrl: parentNotePath, targetBranchId: node.data.branchId, isProtected: isProtected, diff --git a/apps/client/src/components/root_command_executor.ts b/apps/client/src/components/root_command_executor.ts index b86ea10cc..192458287 100644 --- a/apps/client/src/components/root_command_executor.ts +++ b/apps/client/src/components/root_command_executor.ts @@ -241,8 +241,8 @@ export default class RootCommandExecutor extends Component { const rootNoteId = "root"; const result = await noteCreateService.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, title: "New AI Chat", type: "aiChat", content: JSON.stringify({ diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts index 03ba5f898..e3177ea8e 100644 --- a/apps/client/src/menus/tree_context_menu.ts +++ b/apps/client/src/menus/tree_context_menu.ts @@ -285,8 +285,8 @@ export default class TreeContextMenu implements SelectMenuItemEventListener, options?: Options) { // --- CREATE NOTE INTO INBOX --- case SuggestionAction.CreateNoteIntoInbox: { const { note } = await noteCreateService.createNote( - CreateNoteTarget.IntoInbox, { + target: CreateNoteTarget.IntoInbox, title: suggestion.noteTitle, activate: true, promptForType: true, - } as InboxNoteOpts + } as CreateNoteIntoInboxURLOpts ); if (!note) return; @@ -503,12 +503,12 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { // --- CREATE AND LINK NOTE INTO INBOX --- case SuggestionAction.CreateAndLinkNoteIntoInbox: { const { note } = await noteCreateService.createNote( - CreateNoteTarget.IntoInbox, { + target: CreateNoteTarget.IntoInbox, title: suggestion.noteTitle, activate: false, promptForType: true, - } as InboxNoteOpts, + } as CreateNoteIntoInboxURLOpts, ); if (!note) return; @@ -524,14 +524,14 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { // --- CREATE NOTE INTO PATH --- case SuggestionAction.CreateNoteIntoPath: { const { note } = await noteCreateService.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: suggestion.parentNoteId, title: suggestion.noteTitle, activate: true, promptForType: true, - } as CreateNoteIntoURLOpts - ) + } as CreateNoteIntoURLOpts, + ); if (!note) return; @@ -546,15 +546,16 @@ function initNoteAutocomplete($el: JQuery, options?: Options) { // --- CREATE AND LINK NOTE INTO PATH --- case SuggestionAction.CreateAndLinkNoteIntoPath: { const { note } = await noteCreateService.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, + parentNoteUrl: suggestion.parentNoteId, title: suggestion.noteTitle, activate: false, promptForType: true, } as CreateNoteIntoURLOpts ); - if (!note) return + if (!note) return; const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId; suggestion.notePath = note?.getBestNotePathString(hoistedNoteId); diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts index ff22e6bf9..dc235df4c 100644 --- a/apps/client/src/services/note_create.ts +++ b/apps/client/src/services/note_create.ts @@ -23,6 +23,7 @@ import { CreateChildrenResponse } from "@triliumnext/commons"; export enum CreateNoteTarget { IntoNoteURL, AfterNoteURL, + BeforeNoteURL, IntoInbox, } @@ -37,6 +38,7 @@ export type BaseCreateNoteOpts = } & BaseCreateNoteSharedOpts); export interface BaseCreateNoteSharedOpts { + target: CreateNoteTarget; isProtected?: boolean; saveSelection?: boolean; title?: string | null; @@ -57,15 +59,18 @@ type CreateNoteAtURLOpts = BaseCreateNoteSharedOpts & { } export type CreateNoteIntoURLOpts = CreateNoteAtURLOpts; -export type CreateNoteAfterURLOpts = Omit & { - // targetBranchId disambiguates the position for cloned notes, thus it must - // only be specified for a sibling - // This is also specified in the backend + +// targetBranchId disambiguates the position for cloned notes, thus it must +// only be specified for a sibling +// This is also specified in the backend +type CreateNoteSiblingURLOpts = Omit & { targetBranchId: string; }; +export type CreateNoteBeforeURLOpts = CreateNoteSiblingURLOpts; +export type CreateNoteAfterURLOpts = CreateNoteSiblingURLOpts; // For creating *in the inbox* -export type InboxNoteOpts = BaseCreateNoteSharedOpts & { +export type CreateNoteIntoInboxURLOpts = BaseCreateNoteSharedOpts & { // disallowed parentNoteUrl?: never; } @@ -170,6 +175,12 @@ async function createNoteIntoNote( return createNoteAtNote("into", {...options} as CreateNoteAtURLOpts); } +async function createNoteBeforeNote( + options: CreateNoteBeforeURLOpts +): Promise<{ note: FNote | null; branch: FBranch | undefined }> { + return createNoteAtNote("before", {...options} as CreateNoteAtURLOpts); +} + async function createNoteAfterNote( options: CreateNoteAfterURLOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }> { @@ -184,7 +195,7 @@ async function createNoteAfterNote( * Resolves with the created note and its branch, or `{ note: null, branch: undefined }` if the inbox is missing. */ async function createNoteIntoInbox( - options: InboxNoteOpts + options: CreateNoteIntoInboxURLOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }> { const inboxNote = await dateNoteService.getInboxNote(); if (!inboxNote) { @@ -215,27 +226,26 @@ async function chooseNoteType() { } async function createNote( - target: CreateNoteTarget.IntoNoteURL, options: CreateNoteIntoURLOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }>; async function createNote( - target: CreateNoteTarget.AfterNoteURL, options: CreateNoteAfterURLOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }>; async function createNote( - target: CreateNoteTarget.IntoInbox, - options?: InboxNoteOpts + options: CreateNoteBeforeURLOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }>; async function createNote( - target: CreateNoteTarget, - options: BaseCreateNoteOpts = {promptForType: true} + options: CreateNoteIntoInboxURLOpts +): Promise<{ note: FNote | null; branch: FBranch | undefined }>; + +async function createNote( + options: BaseCreateNoteOpts ): Promise<{ note: FNote | null; branch: FBranch | undefined }> { let resolvedOptions = { ...options }; - let resolvedTarget = target; // handle prompts centrally to write once fix for all if (options.promptForType) { @@ -256,24 +266,27 @@ async function createNote( resolvedOptions = resolvedOptions as CreateNoteIntoURLOpts; resolvedOptions = { ...resolvedOptions, + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: notePath, } as CreateNoteIntoURLOpts; - resolvedTarget = CreateNoteTarget.IntoNoteURL; } } - switch (resolvedTarget) { + switch (resolvedOptions.target) { case CreateNoteTarget.IntoNoteURL: return await createNoteIntoNote(resolvedOptions as CreateNoteIntoURLOpts); + case CreateNoteTarget.BeforeNoteURL: + return await createNoteBeforeNote(resolvedOptions as CreateNoteBeforeURLOpts); + case CreateNoteTarget.AfterNoteURL: return await createNoteAfterNote(resolvedOptions as CreateNoteAfterURLOpts); case CreateNoteTarget.IntoInbox: - return await createNoteIntoInbox(resolvedOptions as InboxNoteOpts); + return await createNoteIntoInbox(resolvedOptions as CreateNoteIntoInboxURLOpts); default: { - console.warn("[createNote] Unknown target:", target, resolvedOptions); + console.warn("[createNote] Unknown target:", options.target, resolvedOptions); toastService.showMessage("Unknown note creation target."); // optional return { note: null, branch: undefined }; } diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts index e169e8609..562594a51 100644 --- a/apps/client/src/widgets/collections/board/api.ts +++ b/apps/client/src/widgets/collections/board/api.ts @@ -39,7 +39,8 @@ export default class BoardApi { const parentNotePath = this.parentNote.noteId; // Create a new note as a child of the parent note - const { note: newNote, branch: newBranch } = await note_create.createNote(CreateNoteTarget.IntoNoteURL, { + const { note: newNote, branch: newBranch } = await note_create.createNote({ + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: parentNotePath, activate: false, title, @@ -140,14 +141,14 @@ export default class BoardApi { async insertRowAtPosition( column: string, relativeToBranchId: string, - direction: "before" | "after") { + direction: CreateNoteTarget.BeforeNoteURL | CreateNoteTarget.AfterNoteURL + ) { const { note, branch } = await note_create.createNote( - CreateNoteTarget.IntoNoteURL, { + target: direction, parentNoteUrl: this.parentNote.noteId, activate: false, targetBranchId: relativeToBranchId, - target: direction, title: t("board_view.new-item"), } as CreateNoteIntoURLOpts ); diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts index c834b4c8d..23f4cf6b1 100644 --- a/apps/client/src/widgets/collections/board/context_menu.ts +++ b/apps/client/src/widgets/collections/board/context_menu.ts @@ -6,6 +6,7 @@ import attributes from "../../../services/attributes"; import branches from "../../../services/branches"; import dialog from "../../../services/dialog"; import { t } from "../../../services/i18n"; +import { CreateNoteTarget } from "../../../services/note_create"; import Api from "./api"; export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column: string) { @@ -57,12 +58,18 @@ export function openNoteContextMenu(api: Api, event: ContextMenuEvent, note: FNo { title: t("board_view.insert-above"), uiIcon: "bx bx-list-plus", - handler: () => api.insertRowAtPosition(column, branchId, "before") + handler: () => api.insertRowAtPosition( + column, + branchId, + CreateNoteTarget.BeforeNoteURL) }, { title: t("board_view.insert-below"), uiIcon: "bx bx-empty", - handler: () => api.insertRowAtPosition(column, branchId, "after") + handler: () => api.insertRowAtPosition( + column, + branchId, + CreateNoteTarget.AfterNoteURL) }, { kind: "separator" }, { diff --git a/apps/client/src/widgets/collections/table/context_menu.ts b/apps/client/src/widgets/collections/table/context_menu.ts index 80b1c93c2..2898eb098 100644 --- a/apps/client/src/widgets/collections/table/context_menu.ts +++ b/apps/client/src/widgets/collections/table/context_menu.ts @@ -9,6 +9,7 @@ import branches from "../../../services/branches.js"; import Component from "../../../components/component.js"; import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker.jsx"; import { RefObject } from "preact"; +import { CreateNoteTarget } from "../../../services/note_create.js"; export function useContextMenu(parentNote: FNote, parentComponent: Component | null | undefined, tabulator: RefObject): Partial { const events: Partial = {}; @@ -183,7 +184,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro handler: () => parentComponent?.triggerCommand("addNewRow", { parentNotePath: parentNoteId, customOpts: { - target: "before", + target: CreateNoteTarget.BeforeNoteURL, targetBranchId: rowData.branchId, } }) @@ -197,7 +198,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro parentComponent?.triggerCommand("addNewRow", { parentNotePath: note?.noteId, customOpts: { - target: "after", + target: CreateNoteTarget.AfterNoteURL, targetBranchId: branchId, } }); @@ -210,7 +211,7 @@ export function showRowContextMenu(parentComponent: Component, e: MouseEvent, ro handler: () => parentComponent?.triggerCommand("addNewRow", { parentNotePath: parentNoteId, customOpts: { - target: "after", + target: CreateNoteTarget.AfterNoteURL, targetBranchId: rowData.branchId, } }) diff --git a/apps/client/src/widgets/collections/table/row_editing.ts b/apps/client/src/widgets/collections/table/row_editing.ts index e36f25dfb..914b8c4d0 100644 --- a/apps/client/src/widgets/collections/table/row_editing.ts +++ b/apps/client/src/widgets/collections/table/row_editing.ts @@ -20,7 +20,6 @@ export default function useRowTableEditing(api: RefObject, attributeD ...customOpts } note_create.createNote( - CreateNoteTarget.IntoNoteURL, { parentNoteUrl: notePath, ...opts diff --git a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx index b73a7b0ba..782d54850 100644 --- a/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx +++ b/apps/client/src/widgets/mobile_widgets/mobile_detail_menu.tsx @@ -30,8 +30,8 @@ export default function MobileDetailMenu() { selectMenuItemHandler: async ({ command }) => { if (command === "insertChildNote") { note_create.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: appContext.tabManager.getActiveContextNotePath() ?? undefined } as CreateNoteIntoURLOpts ); diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.ts index e1eab8532..7a4521194 100644 --- a/apps/client/src/widgets/note_tree.ts +++ b/apps/client/src/widgets/note_tree.ts @@ -225,8 +225,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { const node = $.ui.fancytree.getNode(e as unknown as Event); const parentNotePath = treeService.getNotePath(node); noteCreateService.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: parentNotePath, isProtected: node.data.isProtected } as CreateNoteIntoURLOpts, @@ -1843,8 +1843,8 @@ export default class NoteTreeWidget extends NoteContextAwareWidget { if (!node) return; const notePath = treeService.getNotePath(node); noteCreateService.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: notePath, isProtected: node.data.isProtected } as CreateNoteIntoURLOpts diff --git a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx index a5166f688..44bf5b5d7 100644 --- a/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx +++ b/apps/client/src/widgets/ribbon/components/AttributeEditor.tsx @@ -19,7 +19,7 @@ import contextMenu from "../../../menus/context_menu"; import type { CommandData, FilteredCommandNames } from "../../../components/app_context"; import { AttributeType } from "@triliumnext/commons"; import attributes from "../../../services/attributes"; -import note_create, { CreateNoteAfterURLOpts, CreateNoteIntoURLOpts, CreateNoteTarget, InboxNoteOpts } from "../../../services/note_create"; +import note_create, { CreateNoteAfterURLOpts, CreateNoteIntoURLOpts, CreateNoteTarget, CreateNoteIntoInboxURLOpts } from "../../../services/note_create"; import { CreateNoteAction } from "@triliumnext/commons"; type AttributeCommandNames = FilteredCommandNames; @@ -262,11 +262,11 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI case CreateNoteAction.CreateNoteIntoInbox: case CreateNoteAction.CreateAndLinkNoteIntoInbox: { const { note } = await note_create.createNote( - CreateNoteTarget.IntoInbox, { + target: CreateNoteTarget.IntoInbox, title, activate: false - } as InboxNoteOpts + } as CreateNoteIntoInboxURLOpts ); return note?.getBestNotePathString() ?? ""; } @@ -274,8 +274,8 @@ export default function AttributeEditor({ api, note, componentId, notePath, ntxI case CreateNoteAction.CreateNoteIntoPath: case CreateNoteAction.CreateAndLinkNoteIntoPath: { const resp = await note_create.createNote( - CreateNoteTarget.IntoNoteURL, { + target: CreateNoteTarget.IntoNoteURL, parentNoteUrl: parentNotePath, title, activate: false,