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

Web spotlight root in Navigation #31

Merged
merged 5 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion .env.local.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
APP_VERSION=$npm_package_version
APP_NAME=$npm_package_name
NEXT_PUBLIC_KONTENT_ENVIRONMENT_ID=2cedc519-a547-01ba-dde7-b01e00b909a1
KONTENT_COLLECTION_CODENAME=ficto_healthtech_surgical
NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME=ficto_healthtech_surgical
# Collection in format <COLLECTION_CODENAME>:<DOMAIN_WITOUHOUT_PROTOCOL> i.e. "ficto_healthtech:ficto-healthtech.vercel.app,ficto_healthtech_imaging:ficto-healthtech-imaging.vercel.app,ficto_healthtech_surgical:ficto-healthtech-surgical.vercel.app
NEXT_PUBLIC_COLLECTION_DOMAINS=
KONTENT_PREVIEW_API_KEY=
KONTENT_MANAGEMENT_API_KEY=

25 changes: 21 additions & 4 deletions components/shared/ui/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { NextRouter, useRouter } from "next/router";
import { FC, useState } from "react";

import { mainColorBgClass } from "../../../lib/constants/colors";
import { externalPreviewUrlsMapping } from "../../../lib/constants/menu";
import { createItemSmartLink } from "../../../lib/utils/smartLinkUtils";
import { Block_Navigation } from "../../../models";
import { Block_Navigation, contentTypes } from "../../../models";
import { useSiteCodename } from "../siteCodenameContext";

type Link = Readonly<Block_Navigation>;
Expand All @@ -29,9 +30,24 @@ const isCurrentNavigationItemActive = (navigation: Block_Navigation, router: Nex
const pathWithoutQuerystring = router.asPath.replace(/\?.*/, '');
const pathSegments = pathWithoutQuerystring.split("/");
const topLevelSegment = pathSegments[1];
return (navigation.elements.pageLink.linkedItems[0]?.elements.url.value === topLevelSegment);
const pageLink = navigation.elements.pageLink.linkedItems[0];
return (pageLink?.system.codename === contentTypes.page.codename && pageLink.elements.url.value === topLevelSegment);
JiriLojda marked this conversation as resolved.
Show resolved Hide resolved
};

const resolveLink = (link: Readonly<Block_Navigation>) => {
if (link.elements.externalLink.value) {
return link.elements.externalLink.value;
}

const pageLink = link.elements.pageLink.linkedItems[0];
const collectionDomain = externalPreviewUrlsMapping[pageLink?.system.collection ?? ""] || "";

if (pageLink?.system.type === contentTypes.web_spotlight_root.codename) {
return collectionDomain;
}

return collectionDomain + "/" + pageLink?.elements.url.value;
}

const MenuList: FC<MenuListProps> = props => {
const router = useRouter();
Expand Down Expand Up @@ -59,7 +75,7 @@ const MenuList: FC<MenuListProps> = props => {
<Link
{...link.elements.openInANewWindow.value[0] ? { rel: "noopener noreferrer", target: "_blank" } : {}}
className="h-full flex items-center justify-between w-full py-2 pl-3 pr-4 font-medium text-gray-900 border-b border-gray-100 md:w-auto md:bg-transparent md:border-0 md:hover:bg-white"
href={link.elements.externalLink.value ? link.elements.externalLink.value : "/" + link.elements.pageLink.linkedItems[0]?.elements.url.value}
href={resolveLink(link)}
>
{link.elements.label.value}
</Link>
Expand All @@ -70,6 +86,7 @@ const MenuList: FC<MenuListProps> = props => {
);
}


JiriLojda marked this conversation as resolved.
Show resolved Hide resolved
const DropdownButton: FC<Props> = props => {
return (
<button
Expand All @@ -90,7 +107,7 @@ const DropdownMenuItems: FC<DropdownMenuProps> = props => {
<li key={link.system.codename}>
<Link
{...link.elements.openInANewWindow.value[0] ? { rel: "noopener noreferrer", target: "_blank" } : {}}
href={link.elements.externalLink.value ? link.elements.externalLink.value : "/" + link.elements.pageLink.linkedItems[0]?.elements.url.value}
href={resolveLink(link)}
className={`${isCurrentNavigationItemActive(link, router) ? "border-l-gray-500 cursor-default " : "border-l-transparent hover:border-l-gray-500"}
block p-3 bg-gray-200 border-l-8 h-full`}
>
Expand Down
7 changes: 7 additions & 0 deletions lib/constants/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@ export const perCollectionRootItems = {
ficto_healthtech_surgical: "ficto_healthtech_surgical"
} as const satisfies PerCollection<string>;

export const externalPreviewUrlsMapping = Object.fromEntries(
JiriLojda marked this conversation as resolved.
Show resolved Hide resolved
process.env.NEXT_PUBLIC_COLLECTION_DOMAINS?.split(",")
.map(collectionPair => collectionPair.split(":"))
.map(([collectionCodename, domain]) => [collectionCodename, "https://" + domain])
.filter(([collectionCodename]) => collectionCodename !== process.env.NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME) ?? []
);

export const getRootCodename = (siteCodename: ValidCollectionCodename) => perCollectionRootItems[siteCodename];

8 changes: 4 additions & 4 deletions lib/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isValidCollectionCodename } from "../types/perCollection";

const { KONTENT_COLLECTION_CODENAME } = process.env;
const { NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME } = process.env;

if (!isValidCollectionCodename(KONTENT_COLLECTION_CODENAME)) {
throw new Error(`Invalid collection codename "${KONTENT_COLLECTION_CODENAME}".`);
if (!isValidCollectionCodename(NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME)) {
throw new Error(`Invalid collection codename "${NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME}".`);
}

/** Use only on server - for client use `useSiteCodename` hook */
Simply007 marked this conversation as resolved.
Show resolved Hide resolved
export const siteCodename = KONTENT_COLLECTION_CODENAME;
export const siteCodename = NEXT_PUBLIC_KONTENT_COLLECTION_CODENAME;
Loading