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

This commit is contained in:
Elian Doran 2025-12-20 22:45:58 +02:00
parent 64a770175f
commit 1e94125133
No known key found for this signature in database
3 changed files with 17 additions and 4 deletions

View File

@ -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"));
}
}

View File

@ -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;
});

View File

@ -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",