Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 22 additions & 41 deletions app/src/components/items/uv/universalViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, { useEffect, useMemo, useRef } from "react";
import { IIIFEvents as BaseEvents, IIIFURLAdapter } from "universalviewer";
import { useRouter, usePathname, useSearchParams } from "next/navigation";
import { useCanvasContext } from "../../../context/CanvasProvider";
import { sendDownloadEvent } from "@/src/utils/ga4Utils";

export type UniversalViewerProps = {
config?: any;
Expand Down Expand Up @@ -70,33 +71,8 @@ const UniversalViewer: React.FC<UniversalViewerProps> = React.memo(
}, [canvasIndex, uv]);

useEffect(() => {
let mo: MutationObserver | undefined;

if (uv) {
// Hide specific default download options by button/anchor text. Right now, just "Whole imiage".
function pruneDownloadButtons() {
const host =
document.querySelector(".uv-iiif-extension-host") || document;
const nodes = host.querySelectorAll<HTMLElement>(
"li.option.single > button, li.option.single button, li.option.single > a, li.option.single a"
);
nodes.forEach((el) => {
const text = (el.textContent || "").trim().toLowerCase();
const isWholeImage = text.startsWith("whole image");

if (isWholeImage) {
const li = el.closest("li");
if (li instanceof HTMLElement) {
li.style.display = "none";
} else {
(el as HTMLElement).style.display = "none";
}
// Uncomment to verify what got hidden:
// console.log("[UV prune] hid", text, el);
}
});
}

// override config using an inline json object
uv.on("configure", function ({ config, cb }) {
console.log("config on uv.on(configure) is : ", config);
Expand Down Expand Up @@ -211,41 +187,46 @@ const UniversalViewer: React.FC<UniversalViewerProps> = React.memo(
},
downloadDialogue: {
options: {
downloadWholeImageHighResEnabled: false,
downloadWholeImageLowResEnabled: false,
downloadCurrentViewEnabled: false,
},
},
},
},
[uv]
);

// Initial pass (in case the dialog already exists)
pruneDownloadButtons();

// Watch for dialog render/changes and re-prune
try {
mo = new MutationObserver(() => pruneDownloadButtons());
mo.observe(document.body, { subtree: true, childList: true });
} catch {}
});
}

// cleanup: disconnect observer on unmount / dependency change
return () => {
try {
mo?.disconnect();
} catch {}
};
}, [canvasIndex, uv]);

useEvent(uv, BaseEvents.CANVAS_INDEX_CHANGE, (i) => {
setCurrentCanvasIndex(i);
});

useEvent(uv, BaseEvents.DOWNLOAD, (i) => {
console.log("blah i ", i);
useEvent(uv, BaseEvents.DOWNLOAD, ({ label }) => {
const fileInfo = parseUVDownloadFilename(label);
if (fileInfo) {
sendDownloadEvent(fileInfo.name, fileInfo.extension);
} else {
console.log(`Could not parse file info from label ${label}`);
sendDownloadEvent(label);
}
});

const parseUVDownloadFilename = (fileLabel: string) => {
const match = fileLabel.match(/^(.*)\s\((.*)\)$/);
if (match) {
return {
name: match[1],
extension: match[2],
};
}
return null;
};

return (
<>
<div
Expand Down
4 changes: 2 additions & 2 deletions app/src/utils/ga4Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const trackGa4PageView = (

export const sendDownloadEvent = (
fileName: string,
extension: string,
extension?: string,
division?: string,
collection?: string,
subcollection?: string
Expand All @@ -29,7 +29,7 @@ export const sendDownloadEvent = (
dataLayer.push({
event: "file_download",
file_name: fileName,
file_extension: extension,
file_extension: extension ?? GA_NOT_SET,
division_center: division ? division : GA_NOT_SET,
collection: collection ? collection : GA_NOT_SET,
subcollection: subcollection ? subcollection : GA_NOT_SET,
Expand Down
Loading
Loading