forked from transitive-bullshit/nextjs-notion-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNotionPage.tsx
253 lines (219 loc) · 6.69 KB
/
NotionPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import React from 'react'
import Link from 'next/link'
import Image from 'next/image'
import dynamic from 'next/dynamic'
import cs from 'classnames'
import { useRouter } from 'next/router'
import { useSearchParam } from 'react-use'
import BodyClassName from 'react-body-classname'
import useDarkMode from '@fisch0920/use-dark-mode'
import { PageBlock } from 'notion-types'
import { Tweet, TwitterContextProvider } from 'react-static-tweets'
// core notion renderer
import { NotionRenderer } from 'react-notion-x'
// utils
import { getBlockTitle, getPageProperty, formatDate } from 'notion-utils'
import { mapPageUrl, getCanonicalPageUrl } from 'lib/map-page-url'
import { mapImageUrl } from 'lib/map-image-url'
import { getPageTweet } from 'lib/get-page-tweet'
import { searchNotion } from 'lib/search-notion'
import * as types from 'lib/types'
import * as config from 'lib/config'
// components
import { CustomFont } from './CustomFont'
import { Loading } from './Loading'
import { Page404 } from './Page404'
import { PageHead } from './PageHead'
import { PageActions } from './PageActions'
import { Footer } from './Footer'
import { PageSocial } from './PageSocial'
import { NotionPageHeader } from './NotionPageHeader'
import { GitHubShareButton } from './GitHubShareButton'
import styles from './styles.module.css'
// -----------------------------------------------------------------------------
// dynamic imports for optional components
// -----------------------------------------------------------------------------
const Code = dynamic(() =>
import('react-notion-x/build/third-party/code').then((m) => m.Code)
)
const Collection = dynamic(() =>
import('react-notion-x/build/third-party/collection').then(
(m) => m.Collection
)
)
const Equation = dynamic(() =>
import('react-notion-x/build/third-party/equation').then((m) => m.Equation)
)
const Pdf = dynamic(
() => import('react-notion-x/build/third-party/pdf').then((m) => m.Pdf),
{
ssr: false
}
)
const Modal = dynamic(
() =>
import('react-notion-x/build/third-party/modal').then((m) => {
m.Modal.setAppElement('.notion-viewport')
return m.Modal
}),
{
ssr: false
}
)
const propertyLastEditedTimeValue = (
{ block, pageHeader },
defaultFn: () => React.ReactNode
) => {
if (pageHeader && block?.last_edited_time) {
return `Last updated ${formatDate(block?.last_edited_time, {
month: 'long'
})}`
} else {
return defaultFn()
}
}
const propertyTextValue = (
{ schema, pageHeader },
defaultFn: () => React.ReactNode
) => {
if (pageHeader && schema?.name?.toLowerCase() === 'author') {
return <b>{defaultFn()}</b>
} else {
return defaultFn()
}
}
export const NotionPage: React.FC<types.PageProps> = ({
site,
recordMap,
error,
pageId
}) => {
const router = useRouter()
const lite = useSearchParam('lite')
const components = React.useMemo(
() => ({
nextImage: Image,
nextLink: Link,
Code,
Collection,
Equation,
Pdf,
Modal,
Tweet,
Header: NotionPageHeader,
propertyLastEditedTimeValue,
propertyTextValue
}),
[]
)
const twitterContextValue = React.useMemo(() => {
if (!recordMap) {
return null
}
return {
tweetAstMap: (recordMap as any).tweetAstMap || {},
swrOptions: {
fetcher: (id: string) =>
fetch(`/api/get-tweet-ast/${id}`).then((r) => r.json())
}
}
}, [recordMap])
// lite mode is for oembed
const isLiteMode = lite === 'true'
const darkMode = useDarkMode(false, { classNameDark: 'dark-mode' })
const siteMapPageUrl = React.useMemo(() => {
const params: any = {}
if (lite) params.lite = lite
const searchParams = new URLSearchParams(params)
return mapPageUrl(site, recordMap, searchParams)
}, [site, recordMap, lite])
if (router.isFallback) {
return <Loading />
}
const keys = Object.keys(recordMap?.block || {})
const block = recordMap?.block?.[keys[0]]?.value
if (error || !site || !keys.length || !block) {
return <Page404 site={site} pageId={pageId} error={error} />
}
const title = getBlockTitle(block, recordMap) || site.name
console.log('notion page', {
isDev: config.isDev,
title,
pageId,
rootNotionPageId: site.rootNotionPageId,
recordMap
})
if (!config.isServer) {
// add important objects to the window global for easy debugging
const g = window as any
g.pageId = pageId
g.recordMap = recordMap
g.block = block
}
const canonicalPageUrl =
!config.isDev && getCanonicalPageUrl(site, recordMap)(pageId)
// const isRootPage =
// parsePageId(block.id) === parsePageId(site.rootNotionPageId)
const isBlogPost =
block.type === 'page' && block.parent_table === 'collection'
const showTableOfContents = !!isBlogPost
const minTableOfContentsItems = 3
const socialImage = mapImageUrl(
getPageProperty<string>('Social Image', block, recordMap) ||
(block as PageBlock).format?.page_cover ||
config.defaultPageCover,
block
)
const socialDescription =
getPageProperty<string>('Description', block, recordMap) ||
config.description
let pageAside: React.ReactNode = null
// only display comments and page actions on blog post pages
if (isBlogPost) {
const tweet = getPageTweet(block, recordMap)
if (tweet) {
pageAside = <PageActions tweet={tweet} />
}
} else {
pageAside = <PageSocial />
}
return (
<TwitterContextProvider value={twitterContextValue}>
<PageHead
pageId={pageId}
site={site}
title={title}
description={socialDescription}
image={socialImage}
url={canonicalPageUrl}
/>
<CustomFont site={site} />
{isLiteMode && <BodyClassName className='notion-lite' />}
<NotionRenderer
bodyClassName={cs(
styles.notion,
pageId === site.rootNotionPageId && 'index-page'
)}
components={components}
recordMap={recordMap}
rootPageId={site.rootNotionPageId}
rootDomain={site.domain}
fullPage={!isLiteMode}
darkMode={darkMode.value}
previewImages={!!recordMap.preview_images}
showCollectionViewDropdown={false}
showTableOfContents={showTableOfContents}
minTableOfContentsItems={minTableOfContentsItems}
defaultPageIcon={config.defaultPageIcon}
defaultPageCover={config.defaultPageCover}
defaultPageCoverPosition={config.defaultPageCoverPosition}
mapPageUrl={siteMapPageUrl}
mapImageUrl={mapImageUrl}
searchNotion={config.isSearchEnabled ? searchNotion : null}
pageAside={pageAside}
footer={<Footer />}
/>
<GitHubShareButton />
</TwitterContextProvider>
)
}