mirror of
https://github.com/zadam/trilium.git
synced 2026-02-23 22:24:26 +01:00
feat(render): add error boundary
This commit is contained in:
parent
a5806c0d1d
commit
f4e82acc67
@ -1,4 +1,4 @@
|
|||||||
import { h, VNode } from "preact";
|
import { Component, h, VNode } from "preact";
|
||||||
|
|
||||||
import type FNote from "../entities/fnote.js";
|
import type FNote from "../entities/fnote.js";
|
||||||
import { renderReactWidgetAtElement } from "../widgets/react/react_utils.jsx";
|
import { renderReactWidgetAtElement } from "../widgets/react/react_utils.jsx";
|
||||||
@ -6,7 +6,9 @@ import { type Bundle, executeBundleWithoutErrorHandling } from "./bundle.js";
|
|||||||
import froca from "./froca.js";
|
import froca from "./froca.js";
|
||||||
import server from "./server.js";
|
import server from "./server.js";
|
||||||
|
|
||||||
async function render(note: FNote, $el: JQuery<HTMLElement>, onError?: (e: unknown) => void) {
|
type ErrorHandler = (e: unknown) => void;
|
||||||
|
|
||||||
|
async function render(note: FNote, $el: JQuery<HTMLElement>, onError?: ErrorHandler) {
|
||||||
const relations = note.getRelations("renderNote");
|
const relations = note.getRelations("renderNote");
|
||||||
const renderNoteIds = relations.map((rel) => rel.value).filter((noteId) => noteId);
|
const renderNoteIds = relations.map((rel) => rel.value).filter((noteId) => noteId);
|
||||||
|
|
||||||
@ -27,7 +29,7 @@ async function render(note: FNote, $el: JQuery<HTMLElement>, onError?: (e: unkno
|
|||||||
.then(result => {
|
.then(result => {
|
||||||
// Render JSX
|
// Render JSX
|
||||||
if (bundle.html === "") {
|
if (bundle.html === "") {
|
||||||
renderIfJsx(bundle, result, $el).catch(onError);
|
renderIfJsx(bundle, result, $el, onError).catch(onError);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -42,7 +44,7 @@ async function render(note: FNote, $el: JQuery<HTMLElement>, onError?: (e: unkno
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function renderIfJsx(bundle: Bundle, result: unknown, $el: JQuery<HTMLElement>) {
|
async function renderIfJsx(bundle: Bundle, result: unknown, $el: JQuery<HTMLElement>, onError?: ErrorHandler) {
|
||||||
// Ensure the root script note is actually a JSX.
|
// Ensure the root script note is actually a JSX.
|
||||||
const rootScriptNoteId = await froca.getNote(bundle.noteId);
|
const rootScriptNoteId = await froca.getNote(bundle.noteId);
|
||||||
if (rootScriptNoteId?.mime !== "text/jsx") return;
|
if (rootScriptNoteId?.mime !== "text/jsx") return;
|
||||||
@ -55,7 +57,23 @@ async function renderIfJsx(bundle: Bundle, result: unknown, $el: JQuery<HTMLElem
|
|||||||
if (!closestComponent) return;
|
if (!closestComponent) return;
|
||||||
|
|
||||||
// Render the element.
|
// Render the element.
|
||||||
const el = h(result as () => VNode, {});
|
const UserErrorBoundary = class UserErrorBoundary extends Component {
|
||||||
|
constructor(props: object) {
|
||||||
|
super(props);
|
||||||
|
this.state = { error: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidCatch(error: unknown) {
|
||||||
|
onError?.(error);
|
||||||
|
this.setState({ error });
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if ("error" in this.state && this.state?.error) return;
|
||||||
|
return this.props.children;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const el = h(UserErrorBoundary, {}, h(result as () => VNode, {}));
|
||||||
renderReactWidgetAtElement(closestComponent, el, $el[0]);
|
renderReactWidgetAtElement(closestComponent, el, $el[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user