Skip to content

Commit

Permalink
feat: use perspective features (#389)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Jacobsen <174970+mmgj@users.noreply.github.com>
  • Loading branch information
stipsan and mmgj committed Jun 29, 2023
1 parent 79d681e commit 4139e28
Show file tree
Hide file tree
Showing 95 changed files with 1,043 additions and 984 deletions.
12 changes: 12 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export interface LiveQueryProviderProps {
/** @defaultValue true */
listen?: boolean
}
/** @defaultValue 10000 */
refreshInterval?: number
/** @defaultValue true */
turboSourceMap?: boolean
}
export function LiveQueryProvider(
Expand Down Expand Up @@ -112,6 +116,7 @@ export function getClient({
dataset,
apiVersion: '2023-06-20',
useCdn: true,
perspective: 'published',
})
if (preview) {
if (!preview.token) {
Expand All @@ -121,6 +126,7 @@ export function getClient({
token: preview.token,
useCdn: false,
ignoreBrowserTokenWarning: true,
perspective: 'previewDrafts',
})
}
return client
Expand Down Expand Up @@ -192,6 +198,7 @@ export function getClient({
dataset,
apiVersion: '2023-06-20',
useCdn: true,
perspective: 'published',
})
if (preview) {
if (!preview.token) {
Expand All @@ -201,6 +208,7 @@ export function getClient({
token: preview.token,
useCdn: false,
ignoreBrowserTokenWarning: true,
perspective: 'previewDrafts',
})
}
return client
Expand Down Expand Up @@ -281,6 +289,7 @@ export function getClient({
dataset,
apiVersion: '2023-06-20',
useCdn: true,
perspective: 'published',
})
if (preview) {
if (!preview.token) {
Expand All @@ -290,6 +299,7 @@ export function getClient({
token: preview.token,
useCdn: false,
ignoreBrowserTokenWarning: true,
perspective: 'previewDrafts',
})
}
return client
Expand Down Expand Up @@ -360,6 +370,7 @@ export function getClient({
dataset,
apiVersion: '2023-06-20',
useCdn: true,
perspective: 'published',
})
if (preview) {
if (!preview.token) {
Expand All @@ -369,6 +380,7 @@ export function getClient({
token: preview.token,
useCdn: false,
ignoreBrowserTokenWarning: true,
perspective: 'previewDrafts',
})
}
return client
Expand Down
14 changes: 14 additions & 0 deletions apps/next-app-router-groq-store/app/DraftModeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { draftMode } from 'next/headers'
import { PreviewDraftsButton, ViewPublishedButton } from 'ui/react'

export default function DraftModeButton() {
return (
<form style={{ display: 'contents' }}>
{draftMode().isEnabled ? (
<ViewPublishedButton formAction="/api/disable-draft" />
) : (
<PreviewDraftsButton formAction="/api/draft" />
)}
</form>
)
}
4 changes: 2 additions & 2 deletions apps/next-app-router-groq-store/app/PreviewProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export default function PreviewProvider({
children: React.ReactNode
token: string
}) {
const client = useMemo(() => getClient({ preview: true, token }), [token])
const client = useMemo(() => getClient({ token }), [token])
return (
<LiveQueryProvider
client={client}
cache={{ maxDocuments: Infinity }}
logger={console}
cache={{ includeTypes: ['page'] }}
>
{children}
</LiveQueryProvider>
Expand Down
32 changes: 22 additions & 10 deletions apps/next-app-router-groq-store/app/RefreshButton.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
'use client'

import { Button } from 'ui/react'
import { useListeningQueryStatus } from '@sanity/preview-kit'
import { useRouter } from 'next/navigation'
import { useEffect, useTransition } from 'react'
import { experimental_useFormStatus as useFormStatus } from 'react-dom'
import { Button, tableQuery } from 'ui/react'
import { revalidate } from './actions'

export default function RefreshButton() {
function useRefresh() {
const { pending } = useFormStatus()
const router = useRouter()
const [loading, startTransition] = useTransition()
const status = useListeningQueryStatus(tableQuery)
useEffect(() => {
if (!pending) {
startTransition(() => router.refresh())
}
}, [pending, router])

return pending || loading || status === 'loading'
}

export default function RefreshButton() {
const loading = useRefresh()

return (
<form
className="section"
onSubmit={(event) => {
event.preventDefault()
router.refresh()
}}
>
<Button>Refresh</Button>
<form action={revalidate} className="section">
<Button isLoading={loading}>Refresh</Button>
</form>
)
}

This file was deleted.

7 changes: 7 additions & 0 deletions apps/next-app-router-groq-store/app/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use server'

import { revalidatePath } from 'next/cache'

export async function revalidate() {
await revalidatePath('/')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { redirect } from 'next/navigation'

import { draftMode } from 'next/headers'

export async function GET(request: Request) {
draftMode().disable()
redirect('/')
}
14 changes: 5 additions & 9 deletions apps/next-app-router-groq-store/app/api/draft/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { redirect } from 'next/navigation'

import { draftMode } from 'next/headers'
import { NextRequest, NextResponse } from 'next/server'

export function GET(req: NextRequest) {
if (draftMode().isEnabled) {
draftMode().disable()
} else {
draftMode().enable()
}
const url = new URL(req.nextUrl)
return NextResponse.redirect(new URL('/', url.origin))
export async function GET(request: Request) {
draftMode().enable()
redirect('/')
}
59 changes: 27 additions & 32 deletions apps/next-app-router-groq-store/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import { draftMode } from 'next/headers'
import { PreviewDraftsButton, Footer, footerQuery } from 'ui/react'
import RefreshButton from './RefreshButton'
import { unstable__adapter, unstable__environment } from '@sanity/client'
import { Table, Timestamp, tableQuery } from 'ui/react'
import { getClient } from './sanity.client'
import { draftMode } from 'next/headers'
import { Footer, Table, Timestamp, footerQuery, tableQuery } from 'ui/react'
import DraftModeButton from './DraftModeButton'
import PreviewFooter from './PreviewFooter'
import PreviewProvider from './PreviewProvider'
import PreviewTable from './PreviewTable'
import PreviewFooter from './PreviewFooter'
import ViewPublishedButtonWithLoadingStatus from './ViewPublishedButtonWithLoadingStatus'
import RefreshButton from './RefreshButton'
import { getClient } from './sanity.client'

export default async function Page() {
const isDraftMode = draftMode().isEnabled
const button = isDraftMode ? (
<ViewPublishedButtonWithLoadingStatus />
) : (
<PreviewDraftsButton />
)
const client = getClient({ preview: isDraftMode })
const table = client.fetch(tableQuery)
const footer = client.fetch(footerQuery)
const token = process.env.SANITY_API_READ_TOKEN!
const preview = draftMode().isEnabled ? { token } : undefined
const client = getClient(preview)
const [table, footer] = await Promise.all([
client.fetch(tableQuery),
client.fetch(footerQuery),
])
return (
<>
<form action="/api/draft" style={{ display: 'contents' }}>
{isDraftMode ? (
<PreviewProvider token={client.config().token!}>
{button}
<PreviewTable data={await table} />
<PreviewFooter data={await footer} />
</PreviewProvider>
) : (
<>
{button}
<Table data={await table} />
<Footer data={await footer} />
</>
)}
<Timestamp date={new Date()} />
</form>
{preview ? (
<PreviewProvider token={preview.token!}>
<DraftModeButton />
<PreviewTable data={table} />
<PreviewFooter data={footer} />
</PreviewProvider>
) : (
<>
<DraftModeButton />
<Table data={table} />
<Footer data={footer} />
</>
)}
<Timestamp date={new Date()} />
<RefreshButton />
<script
type="application/json"
Expand Down
24 changes: 8 additions & 16 deletions apps/next-app-router-groq-store/app/sanity.client.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import { createClient, type SanityClient } from '@sanity/preview-kit/client'
import { apiVersion, dataset, projectId, useCdn } from './sanity.env'

export type { SanityClient }
const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'pv8y60vp'
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET || 'production'
const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2022-11-15'
const useCdn = true

export function getClient({
preview = false,
token = process.env.SANITY_API_READ_TOKEN,
}: {
preview?: boolean
token?: string
}): SanityClient {
export function getClient(preview?: { token: string }): SanityClient {
const client = createClient({
projectId,
dataset,
apiVersion,
useCdn,
studioUrl: 'https://preview-kit-test-studio.sanity.build/',
encodeSourceMapAtPath: () => true,
token,
perspective: 'published',
ignoreBrowserTokenWarning: true,
})

if (preview) {
if (!token) {
if (!preview.token) {
throw new Error('You must provide a token to preview drafts')
}
return client.withConfig({
perspective: 'previewDrafts',
token: preview.token,
useCdn: false,
ignoreBrowserTokenWarning: true,
})
}

return client
}
7 changes: 0 additions & 7 deletions apps/next-app-router-groq-store/app/sanity.env.ts

This file was deleted.

9 changes: 9 additions & 0 deletions apps/next-app-router-groq-store/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
logging: 'verbose',
serverActions: true,
},
}

export default nextConfig
14 changes: 14 additions & 0 deletions apps/next-app-router-live-store/app/DraftModeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { draftMode } from 'next/headers'
import { PreviewDraftsButton, ViewPublishedButton } from 'ui/react'

export default function DraftModeButton() {
return (
<form style={{ display: 'contents' }}>
{draftMode().isEnabled ? (
<ViewPublishedButton formAction="/api/disable-draft" />
) : (
<PreviewDraftsButton formAction="/api/draft" />
)}
</form>
)
}
4 changes: 2 additions & 2 deletions apps/next-app-router-live-store/app/PreviewFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'

import { FooterProps, footerQuery, Footer } from 'ui/react'
import { useListeningQuery } from '@sanity/preview-kit'
import { useLiveQuery } from '@sanity/preview-kit'

export default function PreviewFooter(props: FooterProps) {
const data = useListeningQuery<FooterProps['data']>(props.data, footerQuery)
const [data] = useLiveQuery<FooterProps['data']>(props.data, footerQuery)

return <Footer data={data} />
}
13 changes: 3 additions & 10 deletions apps/next-app-router-live-store/app/PreviewProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use client'

import { LiveQueryProvider } from '@sanity/preview-kit'
import { draftsClient } from './sanity.client'
import { useMemo } from 'react'
import { getClient } from './sanity.client'

export default function PreviewProvider({
children,
Expand All @@ -11,16 +11,9 @@ export default function PreviewProvider({
children: React.ReactNode
token: string
}) {
const client = useMemo(
() => draftsClient.withConfig({ token, ignoreBrowserTokenWarning: true }),
[token]
)
const client = useMemo(() => getClient({ token }), [token])
return (
<LiveQueryProvider
client={client}
experimental__turboSourceMap
logger={console}
>
<LiveQueryProvider client={client} logger={console}>
{children}
</LiveQueryProvider>
)
Expand Down
4 changes: 2 additions & 2 deletions apps/next-app-router-live-store/app/PreviewTable.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client'

import { TableProps, tableQuery, Table } from 'ui/react'
import { useListeningQuery } from '@sanity/preview-kit'
import { useLiveQuery } from '@sanity/preview-kit'

export default function PreviewTable(props: TableProps) {
const data = useListeningQuery<TableProps['data']>(props.data, tableQuery)
const [data] = useLiveQuery<TableProps['data']>(props.data, tableQuery)

return <Table data={data} />
}
Loading

13 comments on commit 4139e28

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-remix – ./apps/remix

preview-kit-remix.sanity.build
preview-kit-remix-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-app-router-no-store – ./apps/next-app-router-no-store

preview-kit-next-app-router-no-store.sanity.build
preview-kit-next-app-router-no-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-app-router-live-store – ./apps/next-app-router-live-store

preview-kit-remix-qhfv.sanity.build
preview-kit-next-app-router-live-store-git-main.sanity.build
preview-kit-next-app-router-live-store.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-app-router – ./apps/next-app-router

preview-kit-next-app-router.sanity.build
preview-kit-next-app-router-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-pages-router-groq-store – ./apps/next-pages-router-groq-store

preview-kit-next-pages-router-groq-store.sanity.build
preview-kit-next-pages-router-groq-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-pages-router – ./apps/next-pages-router

preview-kit-next-pages-router.sanity.build
preview-kit-next-pages-router-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-remix-groq-store – ./apps/remix-groq-store

preview-kit-remix-groq-store.sanity.build
preview-kit-remix-groq-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-test-studio – ./apps/studio

preview-kit-test-studio.sanity.build
preview-kit-test-studio-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-remix-live-store-lazy – ./apps/remix-live-store-lazy

preview-kit-remix-live-store-lazy.sanity.build
preview-kit-remix-live-store-lazy-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-remix-live-store – ./apps/remix-live-store

preview-kit-remix-live-store.sanity.build
preview-kit-remix-live-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-pages-router-live-store – ./apps/next-pages-router-live-store

preview-kit-next-pages-router-live-store.sanity.build
preview-kit-next-pages-router-live-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-next-app-router-groq-store – ./apps/next-app-router-groq-store

preview-kit-next-app-router-groq-store.sanity.build
preview-kit-next-app-router-groq-store-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 4139e28 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

preview-kit-remix-groq-store-lazy – ./apps/remix-groq-store-lazy

preview-kit-remix-groq-store-lazy.sanity.build
preview-kit-remix-groq-store-lazy-git-main.sanity.build

Please sign in to comment.