Skip to content

Commit

Permalink
fix: add return types (#18)
Browse files Browse the repository at this point in the history
* fix: add return types

* fix: wrap returned children

---------

Co-authored-by: Noah Gentile <[email protected]>
  • Loading branch information
thebiggianthead and nkgentile authored Aug 2, 2023
1 parent a5f503a commit c68d397
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/preview/PreviewProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/* eslint-disable react/require-default-props */
import type {LiveQueryProviderProps} from '@sanity/preview-kit'
import {type ClientConfig, createClient} from '@sanity/preview-kit/client'
import {lazy, type ReactNode, Suspense, useEffect, useState, useTransition} from 'react'
import {
lazy,
type ReactElement,
type ReactNode,
Suspense,
useEffect,
useState,
useTransition,
} from 'react'

import {PreviewContext} from './context'

Expand All @@ -18,15 +26,15 @@ type SanityPreviewProps = Omit<LiveQueryProviderProps, 'client'> & {
* TODO: inline documentation
* @see https://www.sanity.io/docs/preview-content-on-site
*/
export function PreviewProvider(props: SanityPreviewProps) {
export function PreviewProvider(props: SanityPreviewProps): ReactElement {
const {children, previewConfig, fallback = children, ...rest} = props

const [, startTransition] = useTransition()
const [hydrated, setHydrated] = useState(false)
useEffect(() => startTransition(() => setHydrated(true)), [])

if (!hydrated || !previewConfig || !previewConfig.projectId) {
return children
return <>{children}</>
}

const client = createClient(previewConfig)
Expand Down
6 changes: 3 additions & 3 deletions src/preview/SanityPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/require-default-props */
import type {QueryParams} from '@sanity/client'
import {useLiveQuery} from '@sanity/preview-kit'
import {type ReactNode} from 'react'
import {type ReactElement, type ReactNode} from 'react'

import {usePreviewContext} from './context'

Expand All @@ -18,12 +18,12 @@ type PreviewProps<T> = {
* When provided a Sanity query and render prop,
* changes will be streamed in the client
*/
export function SanityPreview<T = unknown>(props: PreviewProps<T>) {
export function SanityPreview<T = unknown>(props: PreviewProps<T>): ReactElement {
const {data, children, query, params} = props
const isPreview = Boolean(usePreviewContext())

if (typeof children !== 'function') {
return children
return <>{children}</>
}

if (isPreview && query) {
Expand Down

0 comments on commit c68d397

Please sign in to comment.