Skip to content

Commit 38f4f89

Browse files
committed
docs: presentation mode
1 parent 4b75e7d commit 38f4f89

File tree

15 files changed

+350
-9
lines changed

15 files changed

+350
-9
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dist/
55
storybook-static/
66
next-env.d.ts
77

8+
packages/icons-react/icons.tsx
89

910
# editors
1011
.idea/
@@ -17,5 +18,4 @@ next-env.d.ts
1718

1819
# Secrets
1920
.FIGMA_TOKEN
20-
.vercel
21-
packages/icons-react/icons.tsx
21+
.env.local

apps/docs/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Creata a viewer token at sanity.io/manage
2+
SANITY_VIEWER_TOKEN=

apps/docs/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.output
22
.vinxi
33
public/resources/icons/
4-
docgen.ts
4+
docgen.ts
5+
6+
.env
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createMiddleware } from '@tanstack/start';
2+
import { getCookie } from 'vinxi/http';
3+
4+
export const previewMiddleware = createMiddleware().server(async ({ next }) => {
5+
const isPreview = getCookie('__sanity_preview') === 'true';
6+
console.log({ isPreview });
7+
return next({
8+
context: {
9+
previewMode: isPreview,
10+
},
11+
});
12+
});

apps/docs/app/lib/sanity.server.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { QueryParams } from '@sanity/client';
2+
import { createServerFn } from '@tanstack/start';
3+
import { previewMiddleware } from './preview-middleware';
4+
import { sanityFetch as _sanityFetch, client } from './sanity';
5+
6+
export const sanityFetch = createServerFn({ method: 'GET' })
7+
.middleware([previewMiddleware])
8+
.validator((data: { query: string; params: QueryParams }) => data)
9+
.handler(async ({ data, context }) => {
10+
const { query, params } = data;
11+
12+
if (context.previewMode) {
13+
const previewClient = client.withConfig({
14+
perspective: 'previewDrafts',
15+
token: process.env.SANITY_VIEWER_TOKEN, // Needed for accessing previewDrafts perspective
16+
useCdn: false, // the previewDrafts perspective requires this to be `false
17+
});
18+
return _sanityFetch({ query, params, client: previewClient });
19+
}
20+
21+
return _sanityFetch({ query, params });
22+
});

apps/docs/app/lib/sanity.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ export const client = createClient({
55
dataset: 'grunnmuren',
66
apiVersion: '2024-09-18',
77
useCdn: true,
8+
perspective: 'published',
89
});
910

1011
export async function sanityFetch<const QueryString extends string>({
1112
query,
1213
params = {},
14+
client: _client = client,
1315
}: {
1416
query: QueryString;
1517
params?: QueryParams;
18+
client?: typeof client;
1619
}) {
1720
// Not sure what's happening here, but I need to set filterReponse to false to get the data as an array?
18-
const { result } = await client.fetch(query, params, {
21+
const { result } = await _client.fetch(query, params, {
1922
filterResponse: false,
2023
});
2124

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { enableVisualEditing } from '@sanity/visual-editing'
2+
import { useNavigate, useRouter } from '@tanstack/react-router';
3+
import { useEffect } from 'react'
4+
5+
export function VisualEditing() {
6+
const router = useRouter();
7+
8+
9+
useEffect(() => {
10+
const disable = enableVisualEditing({
11+
history: {
12+
// subscribe: (_navigate) => {
13+
// router.history.subscribe
14+
// },
15+
// subscribe: (_navigate) => {
16+
// },
17+
// subscribe: (_navigate) => {
18+
// setNavigate(() => _navigate)
19+
// return () => setNavigate(undefined)
20+
// },
21+
update: (update) => {
22+
console.log(update);
23+
switch (update.type) {
24+
case 'push':
25+
router.history.push(update.url);
26+
break;
27+
case 'replace':
28+
router.history.replace(update.url);
29+
break;
30+
case 'pop':
31+
router.history.back();
32+
break;
33+
}
34+
},
35+
}
36+
})
37+
38+
}, [router]);
39+
40+
return null;
41+
}

apps/docs/app/routes/_docs.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sanityFetch } from '@/lib/sanity';
2+
import { VisualEditing } from '@/lib/visual-editing';
23
import appCss from '@/styles/app.css?url';
34
import { Footer } from '@/ui/footer';
45
import { MainNav } from '@/ui/main-nav';
@@ -56,6 +57,7 @@ function RootLayout() {
5657
</div>
5758
</GrunnmurenProvider>
5859
<ScrollRestoration />
60+
<VisualEditing />
5961
</>
6062
);
6163
}

apps/docs/app/routes/_docs/komponenter/$slug.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as badgeExamples from '@/examples/badge';
22
import * as buttonExamples from '@/examples/button';
3-
import { sanityFetch } from '@/lib/sanity';
3+
import { sanityFetch } from '@/lib/sanity.server';
44
import { Content } from '@/ui/content';
55
import { PropsTable } from '@/ui/props-table';
66
import { createFileRoute, notFound } from '@tanstack/react-router';
@@ -15,8 +15,10 @@ export const Route = createFileRoute('/_docs/komponenter/$slug')({
1515
component: Page,
1616
loader: async ({ params }) => {
1717
const res = await sanityFetch({
18-
query: COMPONENT_QUERY,
19-
params: { slug: params.slug },
18+
data: {
19+
query: COMPONENT_QUERY,
20+
params: { slug: params.slug },
21+
},
2022
});
2123

2224
if (res.data == null) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createAPIFileRoute } from '@tanstack/start/api';
2+
import { deleteCookie, sendRedirect } from 'vinxi/http';
3+
4+
export const APIRoute = createAPIFileRoute('/api/preview-mode/disable')({
5+
GET: () => {
6+
deleteCookie('__sanity_preview', {
7+
path: '/',
8+
secure: import.meta.env.PROD,
9+
httpOnly: true,
10+
sameSite: 'strict',
11+
});
12+
sendRedirect('/');
13+
},
14+
});

0 commit comments

Comments
 (0)