Skip to content

Commit db8e954

Browse files
alexanbjaulonm
authored andcommitted
partially working
1 parent 82bea64 commit db8e954

File tree

5 files changed

+99
-5
lines changed

5 files changed

+99
-5
lines changed

apps/docs/app/routes/_docs.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { previewMiddleware } from '@/lib/preview-middleware';
12
import { sanityFetch } from '@/lib/sanity';
23
import { VisualEditing } from '@/lib/visual-editing';
34
import appCss from '@/styles/app.css?url';
5+
import { DisablePreviewMode } from '@/ui/disable-preview-mode';
46
import { Footer } from '@/ui/footer';
57
import { MainNav } from '@/ui/main-nav';
68
import { GrunnmurenProvider } from '@obosbbl/grunnmuren-react';
@@ -12,13 +14,20 @@ import {
1214
createFileRoute,
1315
useRouter,
1416
} from '@tanstack/react-router';
17+
import { createServerFn } from '@tanstack/start';
1518
import { defineQuery } from 'groq';
1619

1720
const COMPONENTS_NAVIGATION_QUERY = defineQuery(
1821
// make sure the slug is always a string so we don't have add fallback value in code just to make TypeScript happy
1922
`*[_type == "component"]{ _id, name, 'slug': coalesce(slug.current, '')} | order(name asc)`,
2023
);
2124

25+
const checkIsPreview = createServerFn({ method: 'GET' })
26+
.middleware([previewMiddleware])
27+
.handler(({ context }) => {
28+
return context.previewMode;
29+
});
30+
2231
// This is the shared layout for all the Grunnmuren docs pages that are "public", ie not the Sanity studio
2332
export const Route = createFileRoute('/_docs')({
2433
component: RootLayout,
@@ -30,11 +39,23 @@ export const Route = createFileRoute('/_docs')({
3039
},
3140
],
3241
}),
33-
loader: () => sanityFetch({ query: COMPONENTS_NAVIGATION_QUERY }),
42+
beforeLoad: async () => {
43+
const isPreview = await checkIsPreview();
44+
return { isPreview };
45+
},
46+
loader: async ({ context }) => {
47+
return {
48+
componentsNavItems: (
49+
await sanityFetch({ query: COMPONENTS_NAVIGATION_QUERY })
50+
).data,
51+
isPreview: context.isPreview,
52+
};
53+
},
3454
});
3555

3656
function RootLayout() {
3757
const router = useRouter();
58+
const { isPreview } = Route.useLoaderData();
3859

3960
return (
4061
<>
@@ -46,6 +67,12 @@ function RootLayout() {
4667
navigate={(to, options) => router.navigate({ to, ...options })}
4768
useHref={(to) => router.buildLocation({ to }).href}
4869
>
70+
{isPreview && (
71+
<>
72+
<VisualEditing />
73+
<DisablePreviewMode />
74+
</>
75+
)}
4976
<div className="grid min-h-screen lg:flex">
5077
<div className="flex grow flex-col px-6">
5178
<main className="grow">
@@ -57,7 +84,6 @@ function RootLayout() {
5784
</div>
5885
</GrunnmurenProvider>
5986
<ScrollRestoration />
60-
<VisualEditing />
6187
</>
6288
);
6389
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const COMPONENT_QUERY = defineQuery(
1313

1414
export const Route = createFileRoute('/_docs/komponenter/$slug')({
1515
component: Page,
16-
loader: async ({ params }) => {
16+
loader: async ({ params, context }) => {
17+
console.log('context in component route', context);
1718
const res = await sanityFetch({
1819
data: {
1920
query: COMPONENT_QUERY,

apps/docs/src/lib/preview-middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createMiddleware } from '@tanstack/start';
22
import { getCookie } from 'vinxi/http';
33

4-
export const previewMiddleware = createMiddleware().server(async ({ next }) => {
4+
export const previewMiddleware = createMiddleware().server(({ next }) => {
55
const isPreview = getCookie('__sanity_preview') === 'true';
6-
console.log({ isPreview });
6+
console.log('middleware', { isPreview });
77
return next({
88
context: {
99
previewMode: isPreview,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {
2+
type HistoryAdapterNavigate,
3+
enableVisualEditing,
4+
} from '@sanity/visual-editing';
5+
import { useNavigate, useRouter } from '@tanstack/react-router';
6+
import { useEffect, useState } from 'react';
7+
8+
export function VisualEditing() {
9+
const router = useRouter();
10+
const [navigate, setNavigate] = useState<
11+
HistoryAdapterNavigate | undefined
12+
>();
13+
14+
useEffect(() => {
15+
const disable = enableVisualEditing({
16+
history: {
17+
subscribe: (_navigate) => {
18+
console.log('subscribe');
19+
setNavigate(() => {
20+
_navigate({ type: 'replace', url: router.state.location.href });
21+
return _navigate;
22+
});
23+
return () => setNavigate(undefined);
24+
},
25+
update: (update) => {
26+
console.log('update', update);
27+
switch (update.type) {
28+
case 'push':
29+
router.history.push(update.url);
30+
break;
31+
case 'replace':
32+
router.history.replace(update.url);
33+
break;
34+
case 'pop':
35+
router.history.back();
36+
break;
37+
}
38+
},
39+
},
40+
});
41+
42+
return disable;
43+
}, [router]);
44+
45+
useEffect(() => {
46+
if (navigate) {
47+
const unsubscribe = router.subscribe('onResolved', (evt) => {
48+
console.log(evt);
49+
navigate({ type: 'push', url: evt.toLocation.href });
50+
});
51+
52+
return unsubscribe;
53+
}
54+
}, [router, navigate]);
55+
56+
return null;
57+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function DisablePreviewMode() {
2+
return (
3+
<a
4+
href="/api/preview-mode/disable"
5+
className="block bg-blue py-2 text-center text-white"
6+
>
7+
Disable preview mode
8+
</a>
9+
);
10+
}

0 commit comments

Comments
 (0)