Skip to content

Commit

Permalink
feature/additionally fetch header and footer to the context in previe…
Browse files Browse the repository at this point in the history
…w mode to resolve header ad footer later (#57)
  • Loading branch information
dogfrogfog authored Nov 11, 2024
1 parent 46df51e commit e363e46
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
20 changes: 18 additions & 2 deletions apps/storyblok/src/components/CoreLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import type { ISbStoryData } from "@storyblok/react/rsc";

import { fetchStoriesByParams, isDraftModeEnv } from "@/lib/api";
import { DataContextProvider } from "@/components/DataContext";
import StoryblokProvider from "@/components/StoryblokProvider";

import type { ICoreLayoutProps } from "./types";

export default function CoreLayout({
export default async function CoreLayout({
children,
allResolvedLinks,
}: ICoreLayoutProps) {
let headersAndFooters: ISbStoryData[] = [];

if (isDraftModeEnv) {
const { data } = await fetchStoriesByParams(isDraftModeEnv, {
by_slugs: "headers/*,footers/*",
});

headersAndFooters = data;
}

return (
<StoryblokProvider>
<DataContextProvider allResolvedLinks={allResolvedLinks}>
<DataContextProvider
headersAndFooters={headersAndFooters}
allResolvedLinks={allResolvedLinks}
>
{children}
</DataContextProvider>
</StoryblokProvider>
Expand Down
3 changes: 3 additions & 0 deletions apps/storyblok/src/components/DataContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import type { IDataContextProviderProps, IDataContextValues } from "./types";

export const DataContext = createContext<IDataContextValues>({
allResolvedLinks: [],
headersAndFooters: [],
});

export function DataContextProvider({
children,
allResolvedLinks,
headersAndFooters,
}: IDataContextProviderProps) {
return (
<DataContext.Provider
value={{
allResolvedLinks,
headersAndFooters,
}}
>
{children}
Expand Down
3 changes: 3 additions & 0 deletions apps/storyblok/src/components/DataContext/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { ISbStoryData } from "@storyblok/react/rsc";

import type { IResolvedLink } from "@/lib/api";

export interface IDataContextValues {
allResolvedLinks: IResolvedLink[];
headersAndFooters: ISbStoryData[];
}

export interface IDataContextProviderProps extends IDataContextValues {
Expand Down
14 changes: 12 additions & 2 deletions apps/storyblok/src/components/Page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ import { StoryblokComponent, StoryblokStory } from "@storyblok/react/rsc";

import { cn, CookieBanner } from "@shared/ui";

import { useDataContext } from "../DataContext";
import type { IPageContainerProps } from "./types";
import { getHeaderAndFooterStories } from "./utils";

export default function PageContainer({ blok }: IPageContainerProps) {
const { headersAndFooters } = useDataContext();

const { sections, showCookieBanner, theme } = blok;

if (!sections) return null;

const { header, footer } = getHeaderAndFooterStories(
headersAndFooters,
blok.header,
blok.footer,
);

return (
<div className={cn("bg-bgColor", theme)}>
<StoryblokStory story={blok.header} />
<StoryblokStory story={header} />
{sections.map((s) => (
<StoryblokComponent blok={s} key={s._uid} />
))}
<StoryblokStory story={blok.footer} />
<StoryblokStory story={footer} />
{showCookieBanner && <CookieBanner />}
</div>
);
Expand Down
27 changes: 27 additions & 0 deletions apps/storyblok/src/components/Page/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { ISbStoryData } from "@storyblok/react/rsc";

export function getHeaderAndFooterStories(
stories: ISbStoryData[],
header: string | ISbStoryData,
footer: string | ISbStoryData,
) {
let resolvedHeader: ISbStoryData | undefined;
let resolvedFooter: ISbStoryData | undefined;

if (typeof header === "string") {
resolvedHeader = stories.find((s) => s.uuid === header);
} else {
resolvedHeader = header;
}

if (typeof footer === "string") {
resolvedFooter = stories.find((s) => s.uuid === footer);
} else {
resolvedFooter = footer;
}

return {
header: resolvedHeader,
footer: resolvedFooter,
};
}
2 changes: 1 addition & 1 deletion apps/storyblok/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SB_CACHE_VERSION_TAG } from "@/constants/cacheTags";

const API_GATE = process.env.NEXT_PUBLIC_API_GATE;
const isDevMode = process.env.NODE_ENV === "development";
const isDraftModeEnv =
export const isDraftModeEnv =
process.env.NEXT_PUBLIC_IS_PREVIEW === "true" || isDevMode;

// Get the actual SB cache version
Expand Down

0 comments on commit e363e46

Please sign in to comment.