Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/image viewer: show a message when the content loading fails
Adorian Doranc7370e09e050
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex f2e1292852..30a2858462 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -871,6 +871,9 @@"previous": "Previous image: {{- title}}","next": "Next image: {{- title}}"},+ "image_viewer": {+ "loading_error": "There was an error loading the image."+ },"zpetne_odkazy": {"backlink_one": "{{count}} Backlink","backlink_other": "{{count}} Backlinks",diff --git a/apps/client/src/widgets/react/ContentErrorMessage.css b/apps/client/src/widgets/react/ContentErrorMessage.cssnew file mode 100644index 0000000000..ea4f084eb7--- /dev/null+++ b/apps/client/src/widgets/react/ContentErrorMessage.css@@ -0,0 +1,10 @@+/* Fills and centers within a positioned ancestor, overlaying the content it replaces. */+.content-error-message {+ position: absolute;+ inset: 0;+ z-index: 1;+ display: flex;+ align-items: center;+ justify-content: center;+ text-align: center;+}diff --git a/apps/client/src/widgets/react/ContentErrorMessage.tsx b/apps/client/src/widgets/react/ContentErrorMessage.tsxnew file mode 100644index 0000000000..209d842a8d--- /dev/null+++ b/apps/client/src/widgets/react/ContentErrorMessage.tsx@@ -0,0 +1,18 @@+import "./ContentErrorMessage.css";++interface ContentErrorMessageProps {+ /** The message to display. */+ message: string;+}++/**+ * Centered error message overlaid on a content area (e.g. when a note's content fails to load). Expects+ * a positioned ancestor, since it fills it via absolute positioning.+ */+export default function ContentErrorMessage({ message }: ContentErrorMessageProps) {+ return (+ <div className="content-error-message">+ <div>{message}</div>+ </div>+ );+}diff --git a/apps/client/src/widgets/react/ImageViewer.spec.tsx b/apps/client/src/widgets/react/ImageViewer.spec.tsxindex 3f74c09dee..316b45e11b 100644--- a/apps/client/src/widgets/react/ImageViewer.spec.tsx+++ b/apps/client/src/widgets/react/ImageViewer.spec.tsx@@ -85,15 +85,17 @@ describe("ImageViewer", () => {await vi.waitFor(() => expect(wrapperClassList(container)?.contains("img-loaded")).toBe(true));expect(wrapperClassList(container)?.contains("img-loading-error")).toBe(false);expect(container.querySelector(".image-viewer-controls")).not.toBeNull();+ expect(container.querySelector(".content-error-message")).toBeNull();});- it("tints the viewport red, keeping the image hidden, when decoding fails", async () => {+ it("tints the viewport red and shows an error message, keeping the image hidden, when decoding fails", async () => {HTMLImageElement.prototype.decode = () => Promise.reject(new Error("decode failed"));const container = renderViewer({ src: "x" });await vi.waitFor(() => expect(wrapperClassList(container)?.contains("img-loading-error")).toBe(true));expect(wrapperClassList(container)?.contains("img-loaded")).toBe(false);expect(container.querySelector(".image-viewer-controls")).toBeNull();+ expect(container.querySelector(".content-error-message")).not.toBeNull();});});diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsxindex cd71cbacc4..c034b7ed70 100644--- a/apps/client/src/widgets/react/ImageViewer.tsx+++ b/apps/client/src/widgets/react/ImageViewer.tsx@@ -6,6 +6,7 @@ import { type ReactZoomPanPinchRef, TransformComponent, TransformWrapper } fromimport { t } from "../../services/i18n";import { isMobile } from "../../services/utils";+import ContentErrorMessage from "./ContentErrorMessage";import { useStaticTooltip } from "./hooks";import { useImageViewerKeyboard } from "./image_viewer_keyboard";@@ -148,6 +149,10 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.</TransformComponent></TransformWrapper>+ {loadingError && (+ <ContentErrorMessage message={t("image_viewer.loading_error")} />+ )}+{!isMobile() && loaded && (<div className="image-viewer-controls"><button