From b7482f2a6ad5a9e1d6973037ae8517207efe24a1 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 10 Aug 2025 11:38:12 +0300 Subject: [PATCH] refactor(react/dialogs): use shown everywhere --- apps/client/src/components/app_context.ts | 4 + apps/client/src/services/note_create.ts | 2 - apps/client/src/widgets/dialogs/confirm.tsx | 63 +++++++-------- .../src/widgets/dialogs/delete_notes.tsx | 45 ++++++----- apps/client/src/widgets/dialogs/export.tsx | 67 ++++++++-------- apps/client/src/widgets/dialogs/import.tsx | 42 +++++----- .../src/widgets/dialogs/include_note.tsx | 36 ++++----- apps/client/src/widgets/dialogs/info.tsx | 44 ++++------- .../src/widgets/dialogs/jump_to_note.tsx | 78 ++++++++----------- .../src/widgets/dialogs/note_type_chooser.tsx | 39 +++++----- apps/client/src/widgets/dialogs/prompt.tsx | 37 ++++----- 11 files changed, 215 insertions(+), 242 deletions(-) diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts index 55aac553e..4c750a544 100644 --- a/apps/client/src/components/app_context.ts +++ b/apps/client/src/components/app_context.ts @@ -30,6 +30,7 @@ import type CodeMirror from "@triliumnext/codemirror"; import { StartupChecks } from "./startup_checks.js"; import type { CreateNoteOpts } from "../services/note_create.js"; import { ColumnComponent } from "tabulator-tables"; +import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx"; interface Layout { getRootWidget: (appContext: AppContext) => RootWidget; @@ -370,6 +371,9 @@ export type CommandMappings = { }; refreshTouchBar: CommandData; reloadTextEditor: CommandData; + chooseNoteType: CommandData & { + callback: ChooseNoteTypeCallback + } }; type EventMappings = { diff --git a/apps/client/src/services/note_create.ts b/apps/client/src/services/note_create.ts index 3fb597930..00ae717d2 100644 --- a/apps/client/src/services/note_create.ts +++ b/apps/client/src/services/note_create.ts @@ -109,8 +109,6 @@ async function createNote(parentNotePath: string | undefined, options: CreateNot async function chooseNoteType() { return new Promise((res) => { - // TODO: Remove ignore after callback for chooseNoteType is defined in app_context.ts - //@ts-ignore appContext.triggerCommand("chooseNoteType", { callback: res }); }); } diff --git a/apps/client/src/widgets/dialogs/confirm.tsx b/apps/client/src/widgets/dialogs/confirm.tsx index 2b731414a..bbdf36c03 100644 --- a/apps/client/src/widgets/dialogs/confirm.tsx +++ b/apps/client/src/widgets/dialogs/confirm.tsx @@ -1,36 +1,51 @@ import ReactBasicWidget from "../react/ReactBasicWidget"; import Modal from "../react/Modal"; import Button from "../react/Button"; -import { closeActiveDialog, openDialog } from "../../services/dialog"; +import { closeActiveDialog } from "../../services/dialog"; import { t } from "../../services/i18n"; import { useState } from "react"; import FormCheckbox from "../react/FormCheckbox"; +import useTriliumEvent from "../react/hooks"; interface ConfirmDialogProps { title?: string; message?: string | HTMLElement; callback?: ConfirmDialogCallback; - lastElementToFocus?: HTMLElement | null; isConfirmDeleteNoteBox?: boolean; } -function ConfirmDialogComponent({ title, message, callback, lastElementToFocus, isConfirmDeleteNoteBox }: ConfirmDialogProps) { +function ConfirmDialogComponent() { + const [ opts, setOpts ] = useState(); const [ confirmed, setConfirmed ] = useState(false); - const [ isDeleteNoteChecked, setIsDeleteNoteChecked ] = useState(false); + const [ isDeleteNoteChecked, setIsDeleteNoteChecked ] = useState(false); + const [ shown, setShown ] = useState(false); + + function showDialog(title: string | null, message: MessageType, callback: ConfirmDialogCallback, isConfirmDeleteNoteBox: boolean) { + setOpts({ + title: title ?? undefined, + message: (typeof message === "object" && "length" in message ? message[0] : message), + callback, + isConfirmDeleteNoteBox + }); + setShown(true); + } + + useTriliumEvent("showConfirmDialog", ({ message, callback }) => showDialog(null, message, callback, false)); + useTriliumEvent("showConfirmDeleteNoteBoxWithNoteDialog", ({ title, callback }) => showDialog(title, t("confirm.are_you_sure_remove_note", { title: title }), callback, true)); return ( { - callback?.({ + opts?.callback?.({ confirmed, isDeleteNoteChecked }); - lastElementToFocus?.focus(); + setShown(false); }} footer={<>