1+ import { previewMiddleware } from '@/lib/preview-middleware' ;
12import { sanityFetch } from '@/lib/sanity' ;
23import { VisualEditing } from '@/lib/visual-editing' ;
34import appCss from '@/styles/app.css?url' ;
5+ import { DisablePreviewMode } from '@/ui/disable-preview-mode' ;
46import { Footer } from '@/ui/footer' ;
57import { MainNav } from '@/ui/main-nav' ;
68import { 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' ;
1518import { defineQuery } from 'groq' ;
1619
1720const 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
2332export 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
3656function 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}
0 commit comments