Skip to content

Build your personal knowledge base with Trilium Notes

feat(client/bundle): display toast when parent is missing

Elian Doran1e94125133db
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts
index 7631cd3162..9127705a32 100644
--- a/apps/client/src/services/bundle.ts
+++ b/apps/client/src/services/bundle.ts
@@ -6,7 +6,7 @@ import { WidgetDefinitionWithType } from "./frontend_script_api_preact.js";
import { t } from "./i18n.js";
import ScriptContext from "./script_context.js";
import server from "./server.js";
-import toastService from "./toast.js";
+import toastService, { showErrorForScriptNote } from "./toast.js";
import utils, { getErrorMessage } from "./utils.js";
// TODO: Deduplicate with server.
@@ -86,7 +86,7 @@ export class WidgetsByParent {
this.legacyWidgets[widget.parentWidget] = this.legacyWidgets[widget.parentWidget] || [];
this.legacyWidgets[widget.parentWidget].push(widget);
} else {
- console.log(`Custom widget does not have mandatory 'parentWidget' property defined`);
+ showErrorForScriptNote(widget._noteId, t("toast.widget-missing-parent"));
}
}
diff --git a/apps/client/src/services/toast.ts b/apps/client/src/services/toast.ts
index 3e81bf6e1b..4ab4f1efb3 100644
--- a/apps/client/src/services/toast.ts
+++ b/apps/client/src/services/toast.ts
@@ -1,5 +1,6 @@
import { signal } from "@preact/signals";
+import froca from "./froca.js";
import utils from "./utils.js";
export interface ToastOptions {
@@ -61,6 +62,17 @@ function showErrorTitleAndMessage(title: string, message: string, timeout = 1000
});
}
+export async function showErrorForScriptNote(noteId: string, message: string) {
+ const note = await froca.getNote(noteId, true);
+
+ addToast({
+ title: note?.title ?? "",
+ icon: note?.getIcon() ?? "bx bx-error-circle",
+ message,
+ timeout: 15_000
+ });
+}
+
//#region Toast store
export const toasts = signal<ToastOptionsWithRequiredId[]>([]);
@@ -74,7 +86,7 @@ function addToast(opts: ToastOptions) {
function updateToast(id: string, partial: Partial<ToastOptions>) {
toasts.value = toasts.value.map(toast => {
if (toast.id === id) {
- return { ...toast, ...partial }
+ return { ...toast, ...partial };
}
return toast;
});
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index df6d24dd47..648fe747ab 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -28,7 +28,8 @@
},
"widget-render-error": {
"title": "Failed to render a custom React widget"
- }
+ },
+ "widget-missing-parent": "Custom widget does not have mandatory 'parentWidget' property defined."
},
"add_link": {
"add_link": "Add link",