1
- import { Module } from "~/components/modules" ;
2
- import { getPage , getSite } from "~/loaders" ;
1
+ import { useLoaderData } from "@remix-run/react" ;
2
+ import { PreviewSuspense } from "@sanity/preview-kit" ;
3
+
4
+ import { getPage , getPageQueryAndParams , getSite } from "~/loaders" ;
3
5
import { metadata } from "~/loaders/metadata" ;
4
6
import { dynamicLinks } from "~/loaders/dynamicLinks" ;
5
7
import { useRouteData } from "~/hooks/useRouteData" ;
6
8
import { merge } from "~/utils/utils" ;
9
+ import { getSession } from "~/sessions" ;
10
+ import { Page } from "~/components/app/Page" ;
11
+ import { PagePreview } from "~/components/app/PagePreview" ;
7
12
8
13
import type { ActionArgs , LoaderArgs , MetaFunction } from "@remix-run/node" ;
9
14
@@ -17,7 +22,15 @@ export const handle = {
17
22
dynamicLinks
18
23
}
19
24
20
- export const loader = async ( { params } : LoaderArgs ) => {
25
+ export const loader = async ( { request, params } : LoaderArgs ) => {
26
+ const isPreview = ! ! ( await getSession ( request . headers . get ( 'Cookie' ) ) ) . get ( 'preview' )
27
+
28
+ if ( isPreview ) return {
29
+ ...await getSite ( params ) ,
30
+ isPreview,
31
+ ...getPageQueryAndParams ( params )
32
+ }
33
+
21
34
const data = await merge ( [
22
35
getSite ( params ) ,
23
36
getPage ( params )
@@ -26,7 +39,7 @@ export const loader = async ({ params }: LoaderArgs) => {
26
39
if ( ! data . page )
27
40
throw new Response ( "Not Found" , { status : 404 } )
28
41
29
- return data
42
+ return { ... data , isPreview }
30
43
}
31
44
32
45
export const action = async ( { request } : ActionArgs ) => {
@@ -52,14 +65,18 @@ export const action = async ({ request }: ActionArgs) => {
52
65
53
66
export type ActionData = Awaited < ReturnType < typeof action > > | undefined
54
67
55
- export default function Page ( ) {
68
+ export default function Component ( ) {
56
69
const data = useRouteData ( )
70
+ const { isPreview, query, params } = useLoaderData ( )
57
71
58
72
return (
59
73
< >
60
- { data ?. page ?. modules ?. map ( ( module , i ) => (
61
- < Module key = { i } index = { i } data = { module } />
62
- ) ) }
74
+ { isPreview ?
75
+ < PreviewSuspense fallback = "Loading..." >
76
+ < PagePreview query = { query } params = { params } />
77
+ </ PreviewSuspense >
78
+ : < Page page = { data . page } />
79
+ }
63
80
</ >
64
81
) ;
65
82
}
0 commit comments