Skip to content

Commit 4e8249b

Browse files
authored
Add vault helper hook for the rendering @id. (#210)
1 parent d2f496e commit 4e8249b

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/hooks/useGetVaultEntityId.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NormalizedEntity } from "@iiif/vault/*";
2+
import { useViewerState } from "src/context/viewer-context";
3+
4+
declare type ExtendedNormalizedEntity = NormalizedEntity & { id: string };
5+
6+
export default function useGetVaultEntityId(id?: string): string | undefined {
7+
const { vault } = useViewerState();
8+
9+
try {
10+
const entity: ExtendedNormalizedEntity | undefined | "" =
11+
id && vault.get(id);
12+
13+
if (!entity) throw new Error(`Vault entity ${id} not found.`);
14+
15+
/**
16+
* Vault seems to handle storage `id` and `@id` differently based on the entity type.
17+
* Ex: Manifest level rendering items use `id` while Canvas level rendering items use `@id`.
18+
* The following logic returns `@id` if it exists, otherwise falls back to `id`.
19+
*/
20+
return entity?.["@id"] || entity?.id;
21+
} catch (error) {
22+
console.error(error);
23+
return id;
24+
}
25+
}

src/hooks/useViewerDownload.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RenderingItem } from "src/types/presentation-3";
22
import { getLabelAsString } from "src/lib/label-helpers";
3+
import useGetVaultEntityId from "src/hooks/useGetVaultEntityId";
34
import useRendering from "src/hooks/use-iiif/useRendering";
45

56
type DownloadItem = {
@@ -12,11 +13,14 @@ function prepareDownloadLinks(
1213
items: RenderingItem[],
1314
defaultLabel: string,
1415
): DownloadItem[] {
15-
return items.map(({ format, id, label }) => ({
16-
format,
17-
id,
18-
label: getLabelAsString(label) || defaultLabel,
19-
}));
16+
return items.map(({ format, id, label }) => {
17+
const resourceId = useGetVaultEntityId(id);
18+
return {
19+
format,
20+
id: resourceId,
21+
label: getLabelAsString(label) || defaultLabel,
22+
};
23+
});
2024
}
2125

2226
export default function useViewerDownload() {

0 commit comments

Comments
 (0)