1
0
Fork 0
freesewing/sites/shared/components/wrappers/page.mjs

102 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-03-26 06:50:59 +02:00
// Dependencies
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
import Head from 'next/head'
import { SwipeWrapper } from 'shared/components/wrappers/swipes.mjs'
import { LayoutWrapper, ns as layoutNs } from 'shared/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'
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 */
export const PageWrapper = (props) => {
/*
* Deconstruct props
*/
const {
layout = DocsLayout,
footer = true,
header = false,
children = [],
path = [],
locale = 'en',
} = props
// Title is typically set in props.t but check props.title too
const pageTitle = props.t ? props.t : props.title ? props.title : null
/*
* 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
*/
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)
useEffect(() => setCurrentTheme(theme), [currentTheme, theme])
2023-03-26 06:50:59 +02:00
/*
* Update navigation context with title and path
*/
useEffect(() => {
// Only update if a new page was loaded
if (path.join('/') !== slug) {
2023-05-08 14:03:47 +02:00
setNavigation({
title: pageTitle,
locale,
path,
})
setNavupdates(navupdates + 1)
}
}, [path, pageTitle, slug, locale, navupdates, setNavigation])
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)
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 (
<SwipeWrapper>
{pageTitle && (
<Head>
<meta property="og:title" content={pageTitle} key="title" />
</Head>
)}
<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
)
}