Skip to content

Commit

Permalink
👅
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Apr 7, 2022
1 parent 679131a commit 45b20d9
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 86 deletions.
43 changes: 19 additions & 24 deletions components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import useDarkMode from '@fisch0920/use-dark-mode'
import { FaTwitter } from '@react-icons/all-files/fa/FaTwitter'
import { FaZhihu } from '@react-icons/all-files/fa/FaZhihu'
import { FaGithub } from '@react-icons/all-files/fa/FaGithub'
Expand All @@ -11,39 +12,31 @@ import styles from './styles.module.css'

// TODO: merge the data and icons from PageSocial with the social links in Footer

export const Footer: React.FC<{
isDarkMode: boolean
toggleDarkMode: () => void
}> = ({ isDarkMode, toggleDarkMode }) => {
const [hasMounted, setHasMounted] = React.useState(false)
const toggleDarkModeCb = React.useCallback(
export const FooterImpl: React.FC = () => {
const darkMode = useDarkMode(false, { classNameDark: 'dark-mode' })

const onToggleDarkMode = React.useCallback(
(e) => {
e.preventDefault()
toggleDarkMode()
darkMode.toggle()
},
[toggleDarkMode]
[darkMode]
)

React.useEffect(() => {
setHasMounted(true)
}, [])

return (
<footer className={styles.footer}>
<div className={styles.copyright}>Copyright 2022 {config.author}</div>

{hasMounted ? (
<div className={styles.settings}>
<a
className={styles.toggleDarkMode}
href='#'
onClick={toggleDarkModeCb}
title='Toggle dark mode'
>
{isDarkMode ? <IoMoonSharp /> : <IoSunnyOutline />}
</a>
</div>
) : null}
<div className={styles.settings}>
<a
className={styles.toggleDarkMode}
href='#'
role='button'
onClick={onToggleDarkMode}
>
{darkMode.value ? <IoMoonSharp /> : <IoSunnyOutline />}
</a>
</div>

<div className={styles.social}>
{config.twitter && (
Expand Down Expand Up @@ -97,3 +90,5 @@ export const Footer: React.FC<{
</footer>
)
}

export const Footer = React.memo(FooterImpl)
78 changes: 45 additions & 33 deletions components/NotionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ const Pdf = dynamic(
}
)
const Modal = dynamic(
() => import('react-notion-x/build/third-party/modal').then((m) => m.Modal),
() =>
import('react-notion-x/build/third-party/modal').then((m) => {
m.Modal.setAppElement('.notion-viewport')
return m.Modal
}),
{
ssr: false
}
Expand All @@ -73,15 +77,48 @@ export const NotionPage: React.FC<types.PageProps> = ({
const router = useRouter()
const lite = useSearchParam('lite')

const params: any = {}
if (lite) params.lite = lite
const components = React.useMemo(
() => ({
nextImage: Image,
nextLink: Link,
Code,
Collection,
Equation,
Pdf,
Modal,
Tweet,
Header: NotionPageHeader
}),
[]
)

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 searchParams = new URLSearchParams(params)

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 />
}
Expand Down Expand Up @@ -111,8 +148,6 @@ export const NotionPage: React.FC<types.PageProps> = ({
g.block = block
}

const siteMapPageUrl = mapPageUrl(site, recordMap, searchParams)

const canonicalPageUrl =
!config.isDev && getCanonicalPageUrl(site, recordMap)(pageId)

Expand Down Expand Up @@ -147,15 +182,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
}

return (
<TwitterContextProvider
value={{
tweetAstMap: (recordMap as any).tweetAstMap || {},
swrOptions: {
fetcher: (id) =>
fetch(`/api/get-tweet-ast/${id}`).then((r) => r.json())
}
}}
>
<TwitterContextProvider value={twitterContextValue}>
<PageHead
pageId={pageId}
site={site}
Expand All @@ -174,17 +201,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
styles.notion,
pageId === site.rootNotionPageId && 'index-page'
)}
components={{
nextImage: Image,
nextLink: Link,
Code,
Collection,
Equation,
Pdf,
Modal,
Tweet,
Header: NotionPageHeader
}}
components={components}
recordMap={recordMap}
rootPageId={site.rootNotionPageId}
rootDomain={site.domain}
Expand All @@ -199,14 +216,9 @@ export const NotionPage: React.FC<types.PageProps> = ({
defaultPageCoverPosition={config.defaultPageCoverPosition}
mapPageUrl={siteMapPageUrl}
mapImageUrl={mapImageUrl}
searchNotion={searchNotion}
searchNotion={config.isSearchEnabled ? searchNotion : null}
pageAside={pageAside}
footer={
<Footer
isDarkMode={darkMode.value}
toggleDarkMode={darkMode.toggle}
/>
}
footer={<Footer />}
/>

<GitHubShareButton />
Expand Down
66 changes: 61 additions & 5 deletions components/NotionPageHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import React from 'react'
// import useDarkMode from '@fisch0920/use-dark-mode'
import cs from 'classnames'
import useDarkMode from '@fisch0920/use-dark-mode'
import { IoSunnyOutline } from '@react-icons/all-files/io5/IoSunnyOutline'
import { IoMoonSharp } from '@react-icons/all-files/io5/IoMoonSharp'

import { Header, Breadcrumbs, Search } from 'react-notion-x'
import { Header, Breadcrumbs, Search, useNotionContext } from 'react-notion-x'

import * as types from 'lib/types'
import { navigationStyle } from 'lib/config'
import { navigationStyle, navigationLinks, isSearchEnabled } from 'lib/config'

// import styles from './styles.module.css'
import styles from './styles.module.css'

export const NotionPageHeader: React.FC<{
block: types.CollectionViewPageBlock | types.PageBlock
}> = ({ block }) => {
const darkMode = useDarkMode(false, { classNameDark: 'dark-mode' })
const [hasMounted, setHasMounted] = React.useState(false)
const { components, mapPageUrl } = useNotionContext()

React.useEffect(() => {
setHasMounted(true)
}, [])

if (navigationStyle === 'default') {
return <Header block={block} />
}
Expand All @@ -19,7 +30,52 @@ export const NotionPageHeader: React.FC<{
<header className='notion-header'>
<div className='notion-nav-header'>
<Breadcrumbs block={block} rootOnly={true} />
<Search block={block} />

<div className='notion-nav-header-rhs breadcrumbs'>
{navigationLinks
?.map((link, index) => {
if (!link.pageId && !link.url) {
return null
}

if (link.pageId) {
return (
<components.PageLink
href={mapPageUrl(link.pageId)}
key={index}
className={cs(styles.navLink, 'breadcrumb', 'button')}
>
{link.title}
</components.PageLink>
)
} else {
return (
<components.Link
href={link.url}
key={index}
className={cs(styles.navLink, 'breadcrumb', 'button')}
>
{link.title}
</components.Link>
)
}
})
.filter(Boolean)}

<div
className={cs('breadcrumb', 'button')}
role='button'
onClick={darkMode.toggle}
>
{hasMounted && darkMode.value ? (
<IoMoonSharp />
) : (
<IoSunnyOutline />
)}
</div>

{isSearchEnabled && <Search block={block} title={null} />}
</div>
</div>
</header>
)
Expand Down
9 changes: 9 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { parsePageId } from 'notion-utils'
import posthog from 'posthog-js'
import { getEnv, getSiteConfig } from './get-config-value'
import { NavigationLink } from './site-config'
import {
PageUrlOverridesInverseMap,
PageUrlOverridesMap,
Expand Down Expand Up @@ -93,6 +94,14 @@ export const navigationStyle: NavigationStyle = getSiteConfig(
'default'
)

export const navigationLinks: Array<NavigationLink | null> = getSiteConfig(
'navigationLinks',
null
)

// Optional site search
export const isSearchEnabled: boolean = getSiteConfig('isSearchEnabled', true)

// ----------------------------------------------------------------------------

// Optional redis instance for persisting preview images
Expand Down
6 changes: 4 additions & 2 deletions lib/map-page-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ const uuid = !!includeNotionIdInUrls
export const mapPageUrl =
(site: Site, recordMap: ExtendedRecordMap, searchParams: URLSearchParams) =>
(pageId = '') => {
if (uuidToId(pageId) === site.rootNotionPageId) {
const pageUuid = parsePageId(pageId, { uuid: true })

if (uuidToId(pageUuid) === site.rootNotionPageId) {
return createUrl('/', searchParams)
} else {
return createUrl(
`/${getCanonicalPageId(pageId, recordMap, { uuid })}`,
`/${getCanonicalPageId(pageUuid, recordMap, { uuid })}`,
searchParams
)
}
Expand Down
35 changes: 33 additions & 2 deletions lib/notion.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
import pMap from 'p-map'
import { ExtendedRecordMap, SearchParams, SearchResults } from 'notion-types'
import { mergeRecordMaps } from 'notion-utils'

import { notion } from './notion-api'
import { getPreviewImageMap } from './preview-images'
import { getTweetAstMap } from './tweet-embeds'
import {
isPreviewImageSupportEnabled,
isTweetEmbedSupportEnabled
isTweetEmbedSupportEnabled,
navigationStyle,
navigationLinks
} from './config'

export async function getPage(pageId: string): Promise<ExtendedRecordMap> {
const recordMap = await notion.getPage(pageId)
let recordMap = await notion.getPage(pageId)

if (navigationStyle !== 'default') {
const navigationLinkPageIds = (navigationLinks || [])
.map((link) => link.pageId)
.filter(Boolean)

if (navigationLinkPageIds.length) {
const navigationLinkRecordMaps: ExtendedRecordMap[] = await pMap(
navigationLinkPageIds,
async (navigationLinkPageId) =>
notion.getPage(navigationLinkPageId, {
fetchMissingBlocks: false,
fetchCollections: false,
signFileUrls: false
}),
{
concurrency: 4
}
)

recordMap = navigationLinkRecordMaps.reduce(
(map, navigationLinkRecordMap) =>
mergeRecordMaps(map, navigationLinkRecordMap),
recordMap
)
}
}

if (isPreviewImageSupportEnabled) {
const previewImageMap = await getPreviewImageMap(recordMap)
Expand Down
8 changes: 8 additions & 0 deletions lib/site-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ export interface SiteConfig {
isPreviewImageSupportEnabled?: boolean
isTweetEmbedSupportEnabled?: boolean
isRedisEnabled?: boolean
isSearchEnabled?: boolean

includeNotionIdInUrls?: boolean
pageUrlOverrides?: types.PageUrlOverridesMap
pageUrlAdditions?: types.PageUrlOverridesMap

navigationStyle?: types.NavigationStyle
navigationLinks?: Array<NavigationLink>
}

export interface NavigationLink {
title: string
pageId?: string
url?: string
}

export const siteConfig = (config: SiteConfig): SiteConfig => {
Expand Down
Loading

0 comments on commit 45b20d9

Please sign in to comment.