Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vault helper hook for the rendering @id. #210

Merged
merged 1 commit into from
May 2, 2024
Merged
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
25 changes: 25 additions & 0 deletions src/hooks/useGetVaultEntityId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NormalizedEntity } from "@iiif/vault/*";
import { useViewerState } from "src/context/viewer-context";

declare type ExtendedNormalizedEntity = NormalizedEntity & { id: string };

export default function useGetVaultEntityId(id?: string): string | undefined {
const { vault } = useViewerState();

try {
const entity: ExtendedNormalizedEntity | undefined | "" =
id && vault.get(id);

if (!entity) throw new Error(`Vault entity ${id} not found.`);

/**
* Vault seems to handle storage `id` and `@id` differently based on the entity type.
* Ex: Manifest level rendering items use `id` while Canvas level rendering items use `@id`.
* The following logic returns `@id` if it exists, otherwise falls back to `id`.
*/
return entity?.["@id"] || entity?.id;
} catch (error) {
console.error(error);
return id;
}
}
14 changes: 9 additions & 5 deletions src/hooks/useViewerDownload.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RenderingItem } from "src/types/presentation-3";
import { getLabelAsString } from "src/lib/label-helpers";
import useGetVaultEntityId from "src/hooks/useGetVaultEntityId";
import useRendering from "src/hooks/use-iiif/useRendering";

type DownloadItem = {
Expand All @@ -12,11 +13,14 @@ function prepareDownloadLinks(
items: RenderingItem[],
defaultLabel: string,
): DownloadItem[] {
return items.map(({ format, id, label }) => ({
format,
id,
label: getLabelAsString(label) || defaultLabel,
}));
return items.map(({ format, id, label }) => {
const resourceId = useGetVaultEntityId(id);
return {
format,
id: resourceId,
label: getLabelAsString(label) || defaultLabel,
};
});
}

export default function useViewerDownload() {
Expand Down
Loading