Skip to content

Commit

Permalink
feat: debug component shows nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolink committed Feb 10, 2024
1 parent 18e1b2c commit f0d635e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const preview: Preview = {
space: "621006g5gybw",
accessToken: "UaqOlFCYE6lFHd_Cmvp2xD__UBceBcKA4j8KkFoCdac",
isPreview: true,
livePreview: true,
debugMode: true,
}
}
},
Expand Down
51 changes: 51 additions & 0 deletions src/LivePreviewRenderer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {ContentfulLivePreview} from "@contentful/live-preview";
import {ContentfulLivePreviewProvider, useContentfulLiveUpdates} from "@contentful/live-preview/react";
import React from "react";
import {PartialStoryFn as StoryFunction, Renderer, StoryContext} from "@storybook/types";

type LiveRendererProps = {
StoryFn: StoryFunction<Renderer>,
context: StoryContext<Renderer>
initialContent: any
locale: string
debugMode: boolean
}

export function LivePreviewRenderer(
{
StoryFn,
context,
initialContent,
locale,
debugMode
}: LiveRendererProps) {

if (!ContentfulLivePreview.initialized) {
ContentfulLivePreview.init({
locale,
debugMode,
enableLiveUpdates: true,
enableInspectorMode: true,
});
}

function renderStory() {
const content = useContentfulLiveUpdates(initialContent);
context.args = {
...context.args,
...content.fields,
entry: content,
entryId: content.sys.id
}

console.log({context})

return <>{StoryFn(context)}</>;
}

return (
<ContentfulLivePreviewProvider locale={locale}>
{renderStory()}
</ContentfulLivePreviewProvider>
)
}
5 changes: 3 additions & 2 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const preview: ProjectAnnotations<Renderer> = {
// accessToken: "accessToken",
environment: "master",
locale: "en-US",
// livePreview: true,
// debugMode: false,
livePreview: true,
isPreview: true,
debugMode: false,
},
},
};
Expand Down
41 changes: 36 additions & 5 deletions src/stories/ContentfulDebugComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {useContentfulInspectorMode} from "@contentful/live-preview/react";

type AnyEntry =
{
entryId: string
entryId: string,
depth: number,
breadcrumbs: string[],
}
& Record<string, any>;

Expand All @@ -14,15 +16,44 @@ type AnyEntry =
* - entry: The entry object
* - <fieldId>: value
*/
export function ContentfulDebugComponent({entryId, ...fields}: AnyEntry) {
export function ContentfulDebugComponent({entryId, depth = 0, breadcrumbs = [], ...fields}: AnyEntry) {
// For this example, we don't need the entry as full object
delete fields.entry;
const inspectorProps = useContentfulInspectorMode({entryId});
const fieldComponents = Object.entries(fields).map(([key, value]) => {

if (!value) {
return null;
}

if (Array.isArray(value)) {
return value.map((v, i) => {
if (typeof v === 'object' && v.sys) {
return <ContentfulDebugComponent
key={v.sys.id} depth={depth + 1}
entryId={v.sys.id} {...v.fields}
breadcrumbs={[...breadcrumbs, key]}
/>
}
})
}

if (value.type === 'Entry') {
return <ContentfulDebugComponent
key={key} entryId={value.sys.id}
depth={depth + 1}
breadcrumbs={[...breadcrumbs, key]}
{...value.fields}
/>
}

return (
<pre key={key} {...inspectorProps({fieldId: key})}>
{JSON.stringify(value, null, 2)}
</pre>
<div key={key} style={{margin: 10, paddingLeft: depth * 20}}>
<h5>{[...breadcrumbs, key].join(' > ')}</h5>
<pre style={{padding: 10, backgroundColor: '#eee'}} {...inspectorProps({fieldId: key})}>
{JSON.stringify(value, null, 2)}
</pre>
</div>
);
})
return (
Expand Down
30 changes: 27 additions & 3 deletions src/withContentfulPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {PartialStoryFn as StoryFunction, Renderer, StoryContext} from "@storyboo
import {PARAM_KEY} from "./constants";
import {useContentful} from "./useContentful";
import {mergeParam} from "./utils";
import React from "react";
import {LivePreviewRenderer} from "./LivePreviewRenderer";

export const withContentfulPreview = (
StoryFn: StoryFunction<Renderer>,
Expand All @@ -14,10 +16,14 @@ export const withContentfulPreview = (
const space = mergeParam([contentfulPreview.space, globalContentfulPreview.space])
const accessToken = mergeParam([contentfulPreview.accessToken, globalContentfulPreview.accessToken])
const entryId = searchParams.get('entryId') || contentfulPreview.entryId
const isPreview = mergeParam([contentfulPreview.isPreview, globalContentfulPreview.isPreview])
const locale = mergeParam([searchParams.get('locale'), contentfulPreview.locale, globalContentfulPreview.locale], 'en-US')
const environment = mergeParam([searchParams.get('environment'), contentfulPreview.environment, globalContentfulPreview.environment], 'master')

// Neither merge Params nor this is working
const livePreview = contentfulPreview.livePreview || globalContentfulPreview.livePreview
const isPreview = contentfulPreview.isPreview || globalContentfulPreview.isPreview
const debugMode = contentfulPreview.debugMode || globalContentfulPreview.debugMode

const params = {
space,
accessToken,
Expand All @@ -26,7 +32,7 @@ export const withContentfulPreview = (
locale
}

// console.log({context, globalContentfulPreview, contentfulPreview, params, entryId})
console.log({context, globalContentfulPreview, contentfulPreview, params, entryId, livePreview, debugMode, isPreview})
const {content, isLoading} = useContentful(entryId, params);

if (content) {
Expand All @@ -40,5 +46,23 @@ export const withContentfulPreview = (
if (isLoading) {
return null
}
return StoryFn(context);
console.log('Live preview enabled: ', livePreview )

if (livePreview) {
return (
<LivePreviewRenderer
StoryFn={StoryFn}
context={context}
initialContent={content}
locale={locale}
debugMode={debugMode}
/>
)
}

return (
<>
{StoryFn(context)}
</>
);
}

0 comments on commit f0d635e

Please sign in to comment.