From ab48a286359939ddd8f264d5db2ccd0d545c4630 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Fri, 29 Aug 2025 19:29:15 +0300 Subject: [PATCH] refactor(react/widgets): typings for dynamic require + solve type errors --- apps/client/src/components/app_context.ts | 3 ++- apps/client/src/components/entrypoints.ts | 7 +++---- apps/client/src/services/server.ts | 2 +- apps/client/src/services/utils.ts | 8 +++++++- apps/client/src/widgets/basic_widget.ts | 2 +- .../src/widgets/buttons/history_navigation.ts | 16 ++++------------ apps/client/src/widgets/react/hooks.tsx | 2 +- 7 files changed, 19 insertions(+), 21 deletions(-) diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts index 42f270176..d888eba6f 100644 --- a/apps/client/src/components/app_context.ts +++ b/apps/client/src/components/app_context.ts @@ -1,6 +1,6 @@ import froca from "../services/froca.js"; import RootCommandExecutor from "./root_command_executor.js"; -import Entrypoints, { type SqlExecuteResults } from "./entrypoints.js"; +import Entrypoints from "./entrypoints.js"; import options from "../services/options.js"; import utils, { hasTouchBar } from "../services/utils.js"; import zoomComponent from "./zoom.js"; @@ -32,6 +32,7 @@ import type { CreateNoteOpts } from "../services/note_create.js"; import { ColumnComponent } from "tabulator-tables"; import { ChooseNoteTypeCallback } from "../widgets/dialogs/note_type_chooser.jsx"; import type RootContainer from "../widgets/containers/root_container.js"; +import { SqlExecuteResults } from "@triliumnext/commons"; interface Layout { getRootWidget: (appContext: AppContext) => RootContainer; diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts index 6f8aefc24..7989960a6 100644 --- a/apps/client/src/components/entrypoints.ts +++ b/apps/client/src/components/entrypoints.ts @@ -10,7 +10,6 @@ import bundleService from "../services/bundle.js"; import froca from "../services/froca.js"; import linkService from "../services/link.js"; import { t } from "../services/i18n.js"; -import type FNote from "../entities/fnote.js"; import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons"; export default class Entrypoints extends Component { @@ -20,7 +19,7 @@ export default class Entrypoints extends Component { openDevToolsCommand() { if (utils.isElectron()) { - utils.dynamicRequire("@electron/remote").getCurrentWindow().toggleDevTools(); + utils.dynamicRequire("@electron/remote").getCurrentWindow().webContents.toggleDevTools(); } } @@ -110,7 +109,7 @@ export default class Entrypoints extends Component { if (utils.isElectron()) { // standard JS version does not work completely correctly in electron const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); - const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex()); + const activeIndex = webContents.navigationHistory.getActiveIndex(); webContents.goToIndex(activeIndex - 1); } else { @@ -122,7 +121,7 @@ export default class Entrypoints extends Component { if (utils.isElectron()) { // standard JS version does not work completely correctly in electron const webContents = utils.dynamicRequire("@electron/remote").getCurrentWebContents(); - const activeIndex = parseInt(webContents.navigationHistory.getActiveIndex()); + const activeIndex = webContents.navigationHistory.getActiveIndex(); webContents.goToIndex(activeIndex + 1); } else { diff --git a/apps/client/src/services/server.ts b/apps/client/src/services/server.ts index cb557b19b..b2e6efb9a 100644 --- a/apps/client/src/services/server.ts +++ b/apps/client/src/services/server.ts @@ -218,7 +218,7 @@ function ajax(url: string, method: string, data: unknown, headers: Headers, sile if (utils.isElectron()) { const ipc = utils.dynamicRequire("electron").ipcRenderer; - ipc.on("server-response", async (event: string, arg: Arg) => { + ipc.on("server-response", async (_, arg: Arg) => { if (arg.statusCode >= 200 && arg.statusCode < 300) { handleSuccessfulResponse(arg); } else { diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts index 93bddede4..c3ffab6d8 100644 --- a/apps/client/src/services/utils.ts +++ b/apps/client/src/services/utils.ts @@ -311,7 +311,13 @@ function copySelectionToClipboard() { } } -export function dynamicRequire(moduleName: string) { +const dynamicRequireMappings = { + "@electron/remote": import("@electron/remote"), + "electron": import("electron"), + "child_process": import("child_process") +}; + +export function dynamicRequire(moduleName: T): Awaited{ if (typeof __non_webpack_require__ !== "undefined") { return __non_webpack_require__(moduleName); } else { diff --git a/apps/client/src/widgets/basic_widget.ts b/apps/client/src/widgets/basic_widget.ts index f49f2382c..e33267daf 100644 --- a/apps/client/src/widgets/basic_widget.ts +++ b/apps/client/src/widgets/basic_widget.ts @@ -55,7 +55,7 @@ export class TypedBasicWidget> extends TypedCompon * @param components the components to be added as children to this component provided the condition is truthy. * @returns self for chaining. */ - optChild(condition: boolean, ...components: T[]) { + optChild(condition: boolean, ...components: (T | VNode)[]) { if (condition) { return this.child(...components); } else { diff --git a/apps/client/src/widgets/buttons/history_navigation.ts b/apps/client/src/widgets/buttons/history_navigation.ts index 9fa54c60a..5c05defb6 100644 --- a/apps/client/src/widgets/buttons/history_navigation.ts +++ b/apps/client/src/widgets/buttons/history_navigation.ts @@ -4,15 +4,7 @@ import treeService from "../../services/tree.js"; import ButtonFromNoteWidget from "./button_from_note.js"; import type FNote from "../../entities/fnote.js"; import type { CommandNames } from "../../components/app_context.js"; - -interface WebContents { - history: string[]; - getActiveIndex(): number; - clearHistory(): void; - canGoBack(): boolean; - canGoForward(): boolean; - goToIndex(index: string): void; -} +import type { WebContents } from "electron"; interface ContextMenuItem { title: string; @@ -51,14 +43,14 @@ export default class HistoryNavigationButton extends ButtonFromNoteWidget { async showContextMenu(e: JQuery.ContextMenuEvent) { e.preventDefault(); - if (!this.webContents || this.webContents.history.length < 2) { + if (!this.webContents || this.webContents.navigationHistory.length() < 2) { return; } let items: ContextMenuItem[] = []; - const activeIndex = this.webContents.getActiveIndex(); - const history = this.webContents.history; + const history = this.webContents.navigationHistory; + const activeIndex = history.getActiveIndex(); for (const idx in history) { const url = history[idx]; diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index 58670155a..523f3ef77 100644 --- a/apps/client/src/widgets/react/hooks.tsx +++ b/apps/client/src/widgets/react/hooks.tsx @@ -516,7 +516,7 @@ export function useStaticTooltip(elRef: RefObject, config?: Partial, title: string, actionName: KeyboardActionNames) { +export function useStaticTooltipWithKeyboardShortcut(elRef: RefObject, title: string, actionName: KeyboardActionNames | undefined) { const [ keyboardShortcut, setKeyboardShortcut ] = useState(); useStaticTooltip(elRef, { title: keyboardShortcut?.length ? `${title} (${keyboardShortcut?.join(",")})` : title