Skip to content

Commit

Permalink
Merge pull request #34 from sanity-io/next-13
Browse files Browse the repository at this point in the history
next-sanity updates
  • Loading branch information
SimeonGriggs authored Oct 12, 2023
2 parents f1d3028 + 034394f commit c812c5f
Show file tree
Hide file tree
Showing 31 changed files with 3,671 additions and 19,445 deletions.
11,599 changes: 1,722 additions & 9,877 deletions studio/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"react-icons": "^4.4.0",
"react-is": "^18.2.0",
"react-xarrows": "^2.0.2",
"sanity": "^3.13.0",
"sanity": "^3.14.2",
"sanity-plugin-dashboard-widget-vercel": "^2.0.1",
"sanity-plugin-documents-pane": "^2.0.0",
"sanity-plugin-google-translate": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion studio/structure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export const structure: StructureResolver = (S) =>
),
])
),
S.divider(),
// Field-level translations
S.documentTypeListItem('course').title('Courses'),
S.divider(),
S.documentTypeListItem('presenter').title('Presenters').icon(FiUsers),
S.divider(),
// Singleton, field-level translations
Expand Down
24 changes: 17 additions & 7 deletions web/app/[language]/[course]/[lesson]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {LiveQuery} from '@sanity/preview-kit/live-query'
import get from 'lodash/get'
import {Metadata} from 'next'
import {draftMode} from 'next/headers'
import {SanityDocument} from 'next-sanity'

import Header from '@/components/Header'
import {LessonLayout} from '@/components/LessonLayout'
import {PreviewWrapper} from '@/components/PreviewWrapper'
import {createLessonLinks} from '@/lib/helpers'
import {cachedClientFetch} from '@/sanity/client'
import {sanityFetch} from '@/sanity/client'
import {COMMON_PARAMS, getLabels, getLessonsWithSlugs} from '@/sanity/loaders'
import {lessonQuery} from '@/sanity/queries'

Expand All @@ -32,9 +33,13 @@ export const metadata: Metadata = {
export default async function Page({params}) {
const {lesson, language} = params
const queryParams = {...COMMON_PARAMS, slug: lesson, language}
const {isEnabled: preview} = draftMode()
const data = await cachedClientFetch(preview)(lessonQuery, queryParams)
const labels = await getLabels(queryParams, preview)
const data = await sanityFetch<SanityDocument>({
query: lessonQuery,
params: queryParams,
tags: ['lesson'],
})

const labels = await getLabels(queryParams)

const lessonPaths = createLessonLinks(data.course.lessons, data.course.slug)
const currentLessonIndex = lessonPaths.findIndex((versions) =>
Expand All @@ -45,9 +50,14 @@ export default async function Page({params}) {
return (
<>
<Header translations={translations} currentLanguage={language} />
<PreviewWrapper preview={preview} initialData={data} query={lessonQuery} params={queryParams}>
<LiveQuery
enabled={draftMode().isEnabled}
initialData={data}
query={lessonQuery}
params={queryParams}
>
<LessonLayout labels={labels} />
</PreviewWrapper>
</LiveQuery>
</>
)
}
17 changes: 10 additions & 7 deletions web/app/[language]/[course]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {LiveQuery} from '@sanity/preview-kit/live-query'
import {Metadata} from 'next'
import {draftMode} from 'next/headers'
import {SanityDocument} from 'next-sanity'

import {CourseLayout} from '@/components/CourseLayout'
import Header from '@/components/Header'
import {PreviewWrapper} from '@/components/PreviewWrapper'
import {Translation} from '@/lib/types'
import {cachedClientFetch} from '@/sanity/client'
import {sanityFetch} from '@/sanity/client'
import {COMMON_PARAMS, getCoursesWithSlugs} from '@/sanity/loaders'
import {courseQuery} from '@/sanity/queries'

Expand Down Expand Up @@ -36,9 +36,12 @@ export async function generateStaticParams() {

export default async function Page({params}) {
const {course, language} = params
const {isEnabled: preview} = draftMode()
const queryParams = {...COMMON_PARAMS, slug: course, language}
const data = await cachedClientFetch(preview)(courseQuery, queryParams)
const data = await sanityFetch<SanityDocument>({
query: courseQuery,
params: queryParams,
tags: ['course'],
})

const currentTitle = data?.title ? data.title[language] ?? data.title[i18n.base] : null

Expand All @@ -61,9 +64,9 @@ export default async function Page({params}) {
return (
<>
<Header translations={translations} currentLanguage={language} />
<PreviewWrapper preview={preview} initialData={data} query={courseQuery} params={queryParams}>
<LiveQuery enabled initialData={data} query={courseQuery} params={queryParams}>
<CourseLayout />
</PreviewWrapper>
</LiveQuery>
</>
)
}
9 changes: 3 additions & 6 deletions web/app/[language]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import {lazy} from 'react'
import ExitPreview from '@/components/ExitPreview'
import LegalLinks from '@/components/LegalLinks'
import VisualEditing from '@/components/VisualEditing'
import {token} from '@/sanity/client'
import {COMMON_PARAMS, getLegals} from '@/sanity/loaders'
const PreviewProvider = lazy(() => import('@/components/PreviewProvider'))

export default async function RootLayout(props) {
const queryParams = {...COMMON_PARAMS, language: props.params.language}
const legals = await getLegals(queryParams)

const preview = draftMode().isEnabled ? process.env.SANITY_API_READ_TOKEN : undefined
if (draftMode().isEnabled && !preview) {
console.info(`Preview mode is enabled but no token was found.`)
}
const preview = draftMode().isEnabled

const children = (
<>
Expand All @@ -42,7 +39,7 @@ export default async function RootLayout(props) {
</head>
<body className="font-sans bg-white text-gray-900">
{preview ? (
<PreviewProvider token={preview}>
<PreviewProvider token={token!}>
{children}
<ExitPreview />
</PreviewProvider>
Expand Down
21 changes: 15 additions & 6 deletions web/app/[language]/legal/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import LiveQuery from '@sanity/preview-kit/live-query'
import {Metadata} from 'next'
import {draftMode} from 'next/headers'
import {SanityDocument} from 'next-sanity'

import Header from '@/components/Header'
import LegalLayout from '@/components/LegalLayout'
import {PreviewWrapper} from '@/components/PreviewWrapper'
import {cachedClientFetch} from '@/sanity/client'
import {sanityFetch} from '@/sanity/client'
import {COMMON_PARAMS} from '@/sanity/loaders'
import {legalQuery} from '@/sanity/queries'

Expand All @@ -16,9 +17,12 @@ export const metadata: Metadata = {

export default async function Page({params}) {
const {language, slug} = params
const {isEnabled: preview} = draftMode()
const queryParams = {...COMMON_PARAMS, slug, language}
const data = await cachedClientFetch(preview)(legalQuery, queryParams)
const data = await sanityFetch<SanityDocument>({
query: legalQuery,
params: queryParams,
tags: ['legal'],
})

const translations = i18n.languages.map((lang) => ({
language: lang.id,
Expand All @@ -29,9 +33,14 @@ export default async function Page({params}) {
return (
<>
<Header translations={translations} currentLanguage={language} />
<PreviewWrapper preview={preview} initialData={data} query={legalQuery} params={queryParams}>
<LiveQuery
enabled={draftMode().isEnabled}
initialData={data}
query={legalQuery}
params={queryParams}
>
<LegalLayout />
</PreviewWrapper>
</LiveQuery>
</>
)
}
7 changes: 2 additions & 5 deletions web/app/[language]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {draftMode} from 'next/headers'

import Header from '@/components/Header'
import {HomeLayout} from '@/components/HomeLayout'
import {COMMON_PARAMS, getHome, getLabels} from '@/sanity/loaders'
Expand All @@ -8,10 +6,9 @@ import {i18n} from '../../../languages'

export default async function Page({params}) {
const {language} = params
const {isEnabled: preview} = draftMode()
const queryParams = {...COMMON_PARAMS, language}
const home = await getHome(queryParams, preview)
const labels = await getLabels(queryParams, preview)
const home = await getHome(queryParams)
const labels = await getLabels(queryParams)

const translations = i18n.languages.map((lang) => {
return {
Expand Down
22 changes: 14 additions & 8 deletions web/app/[language]/presenter/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import LiveQuery from '@sanity/preview-kit/live-query'
import {draftMode} from 'next/headers'

import Header from '@/components/Header'
import type {PresenterLayoutProps} from '@/components/PresenterLayout'
import PresenterLayout from '@/components/PresenterLayout'
import {PreviewWrapper} from '@/components/PreviewWrapper'
import {cachedClientFetch, previewClient} from '@/sanity/client'
import {sanityFetch} from '@/sanity/client'
import {COMMON_PARAMS} from '@/sanity/loaders'
import {presenterQuery} from '@/sanity/queries'

Expand All @@ -12,26 +13,31 @@ import {i18n} from '../../../../../languages'
export default async function Page({params}) {
const {language, slug} = params
const queryParams = {...COMMON_PARAMS, slug, language}
const {isEnabled: preview} = draftMode()
const data = await cachedClientFetch(preview)(presenterQuery, queryParams)
const {isEnabled: previewDrafts} = draftMode()
const data = await sanityFetch<PresenterLayoutProps['data']>({
query: presenterQuery,
params: queryParams,
tags: ['presenter'],
previewDrafts,
})

const translations = i18n.languages.map((lang) => ({
language: lang.id,
path: `/${lang.id}/presenter/${slug}`,
title: data?.title,
title: data?.title ?? ``,
}))

return (
<>
<Header translations={translations} currentLanguage={language} />
<PreviewWrapper
preview={preview}
<LiveQuery
enabled={previewDrafts}
initialData={data}
query={presenterQuery}
params={queryParams}
>
<PresenterLayout />
</PreviewWrapper>
</LiveQuery>
</>
)
}
5 changes: 1 addition & 4 deletions web/app/api/preview/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {vercelStegaSplit} from '@vercel/stega'
import {draftMode} from 'next/headers'
import {groq} from 'next-sanity'

Expand Down Expand Up @@ -29,12 +28,10 @@ function isLinkToOurDomain(url: string) {
export async function GET(request: Request) {
const {origin, searchParams} = new URL(request.url)

const id = searchParams.get('id')
const id = searchParams.get('id')?.replace(`drafts.`, ``)

if (!id) {
return new Response('No "id" provided', {status: 401})
} else if (id.startsWith('drafts.')) {
return new Response('Must use a published "id"', {status: 401})
} else if (!searchParams.get('secret')) {
return new Response('No "secret" provided', {status: 401})
}
Expand Down
13 changes: 10 additions & 3 deletions web/components/Clean.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import {vercelStegaSplit} from '@vercel/stega'
import React from 'react'

export function clean(value?: string | null): string {
return value ? vercelStegaSplit(value).cleaned : ``
}

export default function Clean({value}: {value: string}) {
type CleanProps = {
value: string
as?: 'div' | 'span'
}

export default function Clean({value, as}: CleanProps) {
const {cleaned, encoded} = vercelStegaSplit(value)
const Element = as ?? React.Fragment

return encoded ? (
<>
<Element>
{cleaned}
<span style={{display: 'none'}}>{encoded}</span>
</>
</Element>
) : (
cleaned
)
Expand Down
17 changes: 7 additions & 10 deletions web/components/CourseLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,35 @@ import React, {useMemo} from 'react'
import Blobs from '@/components/Blobs'
import LessonLinks from '@/components/LessonLinks'
import Title from '@/components/Title'
import {createCourseSummary, createLessonLinks} from '@/lib/helpers'
import {createLessonLinks} from '@/lib/helpers'

import {i18n} from '../../languages'
import Presenters from './Presenters'

type CourseLayoutProps = {
data?: any
}

export function CourseLayout(props: CourseLayoutProps) {
const {title, slug, presenters, lessons, labels} = props.data ?? {}
const {language: currentLanguage} = useParams()
const {title, slug, presenters, lessons} = props.data ?? {}

// Render the localized title, if it exists, otherwise fallback to base
const currentTitle = title ? title[currentLanguage] ?? title[i18n.base] : null
const {language: currentLanguage} = useParams()
const titleLanguage = Array.isArray(currentLanguage) ? currentLanguage[0] : currentLanguage
const currentTitle = title ? title[titleLanguage] ?? title[i18n.base] : null

// Each "course" document has an array of "lesson" references
// "lesson" documents have document-level translations
// Each document has a unique slug and are related by an
// array of references stored in a separate "translation.metadata" document
const lessonPaths = useMemo(() => createLessonLinks(lessons, slug), [lessons, slug])

const summary = useMemo(
() => createCourseSummary(lessons, presenters, labels),
[lessons, presenters, labels]
)

return (
<>
<div className="relative">
<section className="bg-gradient-to-r mix-blend-multiply from-cyan-100 via-transparent to-transparent pt-16">
<div className="container mx-auto py-8 p-4 md:p-8 xl:p-16 flex flex-col justify-start items-start gap-2 md:gap-4 xl:gap-8">
<Title subtitle={summary}>{currentTitle}</Title>
<Title subtitle={<Presenters presenters={presenters} />}>{currentTitle}</Title>
</div>
</section>

Expand Down
3 changes: 2 additions & 1 deletion web/components/HomeLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type HomeLayoutProps = {

export function HomeLayout(props: HomeLayoutProps) {
const {courses, labels} = props
const {language} = useParams()
const params = useParams()
const language = Array.isArray(params.language) ? params.language[0] : params.language
const courseStart = getLabelByKey('course.start', labels)

return (
Expand Down
6 changes: 2 additions & 4 deletions web/components/LegalLinks.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Link from 'next/link'
import {SanityDocument} from 'next-sanity'
import React from 'react'
import {SanityDocument, Slug} from 'sanity'

import {clean} from './Clean'

type LegalLinksProps = {
legals: SanityDocument[] & {
slug: Slug
}
legals: SanityDocument[]
}
export default function LegalLinks(props: LegalLinksProps) {
const {legals = []} = props
Expand Down
Loading

2 comments on commit c812c5f

@vercel
Copy link

@vercel vercel bot commented on c812c5f Oct 12, 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:

demo-course-platform-studio – ./studio

demo-course-platform-studio.sanity.build
demo-course-platform-studio-git-main.sanity.build

@vercel
Copy link

@vercel vercel bot commented on c812c5f Oct 12, 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:

demo-course-platform – ./web

demo-course-platform.sanity.build
demo-course-platform-git-main.sanity.build

Please sign in to comment.