Skip to content

Commit

Permalink
Merge branch 'transitive-bullshit-main'
Browse files Browse the repository at this point in the history
  • Loading branch information
fky2015 committed Apr 8, 2022
2 parents 9d81e74 + af4d6a2 commit b174ee7
Show file tree
Hide file tree
Showing 34 changed files with 577 additions and 447 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

# Optional (for persisting preview images to redis)
# NOTE: if you want to enable redis, only REDIS_HOST and REDIS_PASSWORD are required
# NOTE: don't forget to set isRedisEnabled to true in the site.config.js file
# NOTE: don't forget to set isRedisEnabled to true in the site.config.ts file
#REDIS_HOST=
#REDIS_PASSWORD=
#REDIS_USER='default'
Expand Down
30 changes: 16 additions & 14 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 @@ -13,17 +14,16 @@ import styles from './styles.module.css'

const year = new Date().getFullYear()

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

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

React.useEffect(() => {
Expand All @@ -34,18 +34,18 @@ export const Footer: React.FC<{
<footer className={styles.footer}>
<div className={styles.copyright}>Copyright {year} {config.author}</div>

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

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

export const Footer = React.memo(FooterImpl)
123 changes: 52 additions & 71 deletions components/NotionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import { Tweet, TwitterContextProvider } from 'react-static-tweets'
import { NotionRenderer } from 'react-notion-x'

// utils
import {
getBlockTitle,
getBlockIcon,
getPageProperty,
isUrl
} from 'notion-utils'
import { getBlockTitle, getPageProperty } 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'
Expand All @@ -37,6 +32,8 @@ import { PageActions } from './PageActions'
import { Footer } from './Footer'
import { PageSocial } from './PageSocial'
const ReactGiscus = dynamic(() => import('./ReactGiscus'))
import { NotionPageHeader } from './NotionPageHeader'
import { GitHubShareButton } from './GitHubShareButton'

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

Expand All @@ -62,7 +59,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 @@ -77,15 +78,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 All @@ -107,8 +141,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 All @@ -121,41 +153,15 @@ export const NotionPage: React.FC<types.PageProps> = ({

const socialImage = mapImageUrl(
getPageProperty<string>('Social Image', block, recordMap) ||
(block as PageBlock).format?.page_cover ||
config.defaultPageCover,
(block as PageBlock).format?.page_cover ||
config.defaultPageCover,
block
)

const socialImageCoverPosition =
(block as PageBlock).format?.page_cover_position ??
config.defaultPageCoverPosition
const socialImageObjectPosition = socialImageCoverPosition
? `center ${(1 - socialImageCoverPosition) * 100}%`
: null

const blockIcon = getBlockIcon(block, recordMap)
const socialAuthorImage = mapImageUrl(
blockIcon && isUrl(blockIcon) ? blockIcon : config.defaultPageIcon,
block
)

const socialAuthor =
getPageProperty<string>('Author', block, recordMap) || config.author

const socialDescription =
getPageProperty<string>('Description', block, recordMap) ||
config.description

const timePublished = getPageProperty<number>('Published', block, recordMap)
const datePublished = timePublished ? new Date(timePublished) : undefined
const socialDate =
isBlogPost && datePublished
? `${datePublished.toLocaleString('en-US', {
month: 'long'
})} ${datePublished.getFullYear()}`
: undefined
const socialDetail = socialDate || site.domain

let comments: React.ReactNode = null
let pageAside: React.ReactNode = null

Expand All @@ -165,7 +171,7 @@ export const NotionPage: React.FC<types.PageProps> = ({
comments = (
<ReactGiscus darkMode={darkMode.value} />
)
}
}
const tweet = getPageTweet(block, recordMap)
if (tweet) {
pageAside = <PageActions tweet={tweet} />
Expand All @@ -175,24 +181,13 @@ 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}
title={title}
description={socialDescription}
image={socialImage}
imageObjectPosition={socialImageObjectPosition}
author={socialAuthor}
authorImage={socialAuthorImage}
detail={socialDetail}
url={canonicalPageUrl}
/>

Expand All @@ -205,16 +200,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
}}
components={components}
recordMap={recordMap}
rootPageId={site.rootNotionPageId}
rootDomain={site.domain}
Expand All @@ -229,15 +215,10 @@ export const NotionPage: React.FC<types.PageProps> = ({
defaultPageCoverPosition={config.defaultPageCoverPosition}
mapPageUrl={siteMapPageUrl}
mapImageUrl={mapImageUrl}
searchNotion={searchNotion}
pageFooter={comments}
searchNotion={config.isSearchEnabled ? searchNotion : null}
pageAside={pageAside}
footer={
<Footer
isDarkMode={darkMode.value}
toggleDarkMode={darkMode.toggle}
/>
}
footer={<Footer />}
/>

</TwitterContextProvider>
Expand Down
82 changes: 82 additions & 0 deletions components/NotionPageHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from 'react'
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, useNotionContext } from 'react-notion-x'

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

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} />
}

return (
<header className='notion-header'>
<div className='notion-nav-header'>
<Breadcrumbs block={block} rootOnly={true} />

<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>
)
}
Loading

0 comments on commit b174ee7

Please sign in to comment.