'use client'
import { useContentfulLiveUpdates } from '@contentful/live-preview/react'
const isFunction = <T extends CallableFunction = CallableFunction>(value: unknown): value is T =>
typeof value === 'function'
export const runIfFunction = <T, U>(valueOrFn: T | ((...fnArgs: U[]) => T), ...args: U[]) => {
return isFunction(valueOrFn) ? valueOrFn(...args) : valueOrFn
}
type MaybeRenderProp<P> = React.ReactNode | ((props: P) => React.ReactNode)
type LivePreviewWrapperProps<T> = {
children: MaybeRenderProp<{
updatedData: T
}>
data: T
}
export const LivePreviewWrapper = <T extends Record<string, unknown>>({
children,
data
}: LivePreviewWrapperProps<T>) => {
const updatedData = useContentfulLiveUpdates<T>(data, { locale: 'en-US' })
return runIfFunction(children, { updatedData })
}
'use client'
import { useContentfulLiveUpdates } from '@contentful/live-preview/react'
const isFunction = <T extends CallableFunction = CallableFunction>(value: unknown): value is T =>
typeof value === 'function'
export const runIfFunction = <T, U>(valueOrFn: T | ((...fnArgs: U[]) => T), ...args: U[]) => {
return isFunction(valueOrFn) ? valueOrFn(...args) : valueOrFn
}
type MaybeRenderProp<P> = React.ReactNode | ((props: P) => React.ReactNode)
type LivePreviewWrapperProps<T> = {
children: MaybeRenderProp<{
updatedData: T
}>
data: T
}
export const LivePreviewWrapper = <T extends Record<string, unknown>>({
children,
data
}: LivePreviewWrapperProps<T>) => {
const updatedData = useContentfulLiveUpdates<T>(data, { locale: 'en-US' })
return runIfFunction(children, { updatedData })
}