-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vault helper hook for the rendering
@id
. (#210)
- Loading branch information
1 parent
d2f496e
commit 4e8249b
Showing
2 changed files
with
34 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters