Skip to content

Build your personal knowledge base with Trilium Notes

refactor(react/widgets): typings for dynamic require + solve type errors

Elian Doranab48a2863599
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 42f270176c..d888eba6f2 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 6f8aefc242..7989960a6b 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 cb557b19b4..b2e6efb9a4 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 93bddede46..c3ffab6d8e 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<T extends keyof typeof dynamicRequireMappings>(moduleName: T): Awaited<typeof dynamicRequireMappings[T]>{
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 f49f2382c4..e33267dafc 100644
--- a/apps/client/src/widgets/basic_widget.ts
+++ b/apps/client/src/widgets/basic_widget.ts
@@ -55,7 +55,7 @@ export class TypedBasicWidget<T extends TypedComponent<any>> 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 9fa54c60ab..5c05defb6e 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 58670155a3..523f3ef776 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<Element>, config?: Partial<Too
}, [ elRef, config ]);
}
-export function useStaticTooltipWithKeyboardShortcut(elRef: RefObject<Element>, title: string, actionName: KeyboardActionNames) {
+export function useStaticTooltipWithKeyboardShortcut(elRef: RefObject<Element>, title: string, actionName: KeyboardActionNames | undefined) {
const [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();
useStaticTooltip(elRef, {
title: keyboardShortcut?.length ? `${title} (${keyboardShortcut?.join(",")})` : title