All files / components/ContentfulRichText/nodes EntryHyperlink.svelte

100% Statements 43/43
100% Branches 0/0
100% Functions 0/0
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<script lang="ts">
  import type { Node as NodeType, EntryHyperlink } from "@contentful/rich-text-types";
  import type { PageMetadataMapItem } from "$lib/loadPageMetadataMap";
  import Node from "./Node.svelte";
  import { getContext } from "svelte";
  import { linksKey, type LinksContext } from "../context";
  import { isEntryHyperlink } from "../predicates";
  import Link from "$lib/components/Link";
  import ContactCard from "$lib/components/ContactCard";
 
  export let node: NodeType;
  if (!isEntryHyperlink(node)) {
    throw new Error("node is not an entry hyperlink");
  }
  let entryHyperlink: EntryHyperlink = node;
 
  const linksContext = getContext<LinksContext | undefined>(linksKey);
  if (!linksContext) throw new Error("no context was provided for entry hyperlink");
 
  const { id: entryID } = entryHyperlink.data.target.sys;
 
  const entry = linksContext.linksEntriesMaps.hyperlink.get(entryID);
 
  let entryMetadata: PageMetadataMapItem;
  if (entry?.__typename === "PageMetadata") {
    linksContext.pageMetadataMap.get(entryID);
    const meta = linksContext.pageMetadataMap.get(entryID);
    if (!meta) {
      throw new Error(`the entry asset ${entryID} was not found in the pageMetadataMap`);
    }
    entryMetadata = meta;
  }
</script>
 
{#if entry?.__typename === "PageMetadata"}
  <Link href={entryMetadata?.url}
    >{#each entryHyperlink.content as subNode}<Node node={subNode} />{/each}
  </Link>
{:else if entry?.__typename === "Contact"}
  <ContactCard address={undefined} contacts={[entry]} />
{:else}
  <p>Entry hyperlink of type {entry?.__typename} not supported</p>
{/if}