2023-03-26 06:50:59 +02:00
|
|
|
// Dependencies
|
2023-04-28 21:23:06 +02:00
|
|
|
import React, { useState, useEffect, useContext } from 'react'
|
2023-05-20 15:47:56 +02:00
|
|
|
//import { useHotkeys } from 'react-hotkeys-hook'
|
2023-03-26 06:50:59 +02:00
|
|
|
// Hooks
|
|
|
|
import { useTheme } from 'shared/hooks/use-theme.mjs'
|
|
|
|
// Components
|
2023-06-17 16:57:17 -05:00
|
|
|
import Head from 'next/head'
|
2023-04-25 17:33:34 +02:00
|
|
|
import { SwipeWrapper } from 'shared/components/wrappers/swipes.mjs'
|
2023-03-26 06:50:59 +02:00
|
|
|
import { LayoutWrapper, ns as layoutNs } from 'site/components/wrappers/layout.mjs'
|
2023-03-28 16:47:07 +02:00
|
|
|
import { DocsLayout, ns as docsNs } from 'site/components/layouts/docs.mjs'
|
2023-03-26 06:50:59 +02:00
|
|
|
import { Feeds } from 'site/components/feeds.mjs'
|
2023-04-28 21:23:06 +02:00
|
|
|
import { ModalContext } from 'shared/context/modal-context.mjs'
|
|
|
|
import { NavigationContext } from 'shared/context/navigation-context.mjs'
|
2023-03-26 06:50:59 +02:00
|
|
|
|
2023-03-28 16:47:07 +02:00
|
|
|
export const ns = [...new Set([...layoutNs, ...docsNs])]
|
2023-03-26 06:50:59 +02:00
|
|
|
|
|
|
|
/* This component should wrap all page content */
|
2023-04-28 21:23:06 +02:00
|
|
|
export const PageWrapper = (props) => {
|
|
|
|
/*
|
|
|
|
* Deconstruct props
|
|
|
|
*/
|
2023-05-08 19:28:03 +02:00
|
|
|
const {
|
|
|
|
layout = DocsLayout,
|
|
|
|
footer = true,
|
|
|
|
header = false,
|
|
|
|
children = [],
|
|
|
|
path = [],
|
|
|
|
locale = 'en',
|
|
|
|
} = props
|
2023-04-28 21:23:06 +02:00
|
|
|
// Title is typically set in props.t but check props.title too
|
|
|
|
const pageTitle = props.t ? props.t : props.title ? props.title : null
|
|
|
|
|
2023-05-20 13:22:36 +02:00
|
|
|
/*
|
|
|
|
* Contexts
|
|
|
|
*/
|
|
|
|
const { modalContent } = useContext(ModalContext)
|
|
|
|
const { setNavigation, slug } = useContext(NavigationContext)
|
|
|
|
|
2023-03-26 06:50:59 +02:00
|
|
|
/*
|
|
|
|
* This forces a re-render upon initial bootstrap of the app
|
|
|
|
* This is needed to avoid hydration errors because theme can't be set reliably in SSR
|
|
|
|
*/
|
2023-05-20 14:47:29 +02:00
|
|
|
const [theme] = useTheme()
|
2023-03-26 06:50:59 +02:00
|
|
|
const [currentTheme, setCurrentTheme] = useState()
|
2023-05-08 14:03:47 +02:00
|
|
|
const [navupdates, setNavupdates] = useState(0)
|
2023-05-20 14:47:29 +02:00
|
|
|
useEffect(() => setCurrentTheme(theme), [currentTheme, theme])
|
2023-03-26 06:50:59 +02:00
|
|
|
|
2023-04-28 21:23:06 +02:00
|
|
|
/*
|
|
|
|
* Update navigation context with title and path
|
|
|
|
*/
|
|
|
|
useEffect(() => {
|
2023-05-20 13:22:36 +02:00
|
|
|
// Only update if a new page was loaded
|
2023-05-24 08:37:29 -05:00
|
|
|
if (path.join('/') !== slug) {
|
2023-05-08 14:03:47 +02:00
|
|
|
setNavigation({
|
|
|
|
title: pageTitle,
|
|
|
|
locale,
|
|
|
|
path,
|
|
|
|
})
|
|
|
|
setNavupdates(navupdates + 1)
|
2023-05-20 13:22:36 +02:00
|
|
|
}
|
|
|
|
}, [path, pageTitle, slug])
|
2023-04-28 21:23:06 +02:00
|
|
|
|
2023-03-26 06:50:59 +02:00
|
|
|
/*
|
|
|
|
* Hotkeys (keyboard actions)
|
|
|
|
*/
|
|
|
|
// Trigger search with /
|
2023-05-20 14:55:16 +02:00
|
|
|
//useHotkeys('/', (evt) => {
|
|
|
|
// evt.preventDefault()
|
|
|
|
// setSearch(true)
|
|
|
|
//})
|
2023-03-26 06:50:59 +02:00
|
|
|
|
|
|
|
// Search state
|
2023-05-20 14:55:16 +02:00
|
|
|
//const [search, setSearch] = useState(false)
|
2023-03-26 06:50:59 +02:00
|
|
|
|
|
|
|
// Helper object to pass props down (keeps things DRY)
|
2023-05-08 19:28:03 +02:00
|
|
|
const childProps = { footer, header, pageTitle }
|
2023-03-26 06:50:59 +02:00
|
|
|
|
|
|
|
// Make layout prop into a (uppercase) component
|
|
|
|
const Layout = layout
|
|
|
|
|
|
|
|
// Return wrapper
|
|
|
|
return (
|
2023-04-28 21:23:06 +02:00
|
|
|
<SwipeWrapper>
|
2023-06-17 16:57:17 -05:00
|
|
|
{pageTitle && (
|
|
|
|
<Head>
|
|
|
|
<meta property="og:title" content={pageTitle} key="title" />
|
|
|
|
</Head>
|
|
|
|
)}
|
2023-04-28 21:23:06 +02:00
|
|
|
<div
|
|
|
|
data-theme={currentTheme} // This facilitates CSS selectors
|
|
|
|
key={currentTheme} // This forces the data-theme update
|
|
|
|
>
|
|
|
|
<Feeds />
|
|
|
|
<LayoutWrapper {...childProps}>
|
|
|
|
{Layout ? <Layout {...childProps}>{children}</Layout> : children}
|
|
|
|
</LayoutWrapper>
|
|
|
|
{modalContent}
|
|
|
|
</div>
|
|
|
|
</SwipeWrapper>
|
2023-03-26 06:50:59 +02:00
|
|
|
)
|
|
|
|
}
|