Skip to content

Commit

Permalink
refactor(docs): update version handling and context use
Browse files Browse the repository at this point in the history
  • Loading branch information
hirotomoyamada committed May 3, 2024
1 parent 7324438 commit 84d46bb
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 36 deletions.
7 changes: 3 additions & 4 deletions docs/components/layouts/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,17 @@ import {
import { NextLinkIconButton, Tree } from "components/navigation"
import { CONSTANT } from "constant"
import { useI18n } from "contexts/i18n-context"
import { usePage } from "contexts/page-context"
import Link from "next/link"
import { useRouter } from "next/router"
import packageJSON from "package.json"
import type { FC } from "react"
import { memo, useEffect, useRef, useState } from "react"

const version = `v${packageJSON.dependencies["@yamada-ui/react"].split("-")[0]}`

export type HeaderProps = CenterProps & {}

export const Header = memo(
forwardRef<HeaderProps, "div">(({ ...rest }, ref) => {
const { currentVersion } = usePage()
const headerRef = useRef<HTMLHeadingElement>()
const { scrollY } = useScroll()
const [y, setY] = useState<number>(0)
Expand Down Expand Up @@ -132,7 +131,7 @@ export const Header = memo(
letterSpacing="1px"
minW="auto"
>
{version}
{currentVersion}
</Tag>

<Spacer />
Expand Down
9 changes: 7 additions & 2 deletions docs/contexts/page-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import type { FC, PropsWithChildren } from "react"
import { createContext, useContext, useMemo } from "react"

type PageContext = Omit<PageProviderProps, "children"> & {
documentMap: Pick<DocumentTypes, "title" | "description" | "label" | "slug">[]
documentMap: Pick<
DocumentTypeTree,
"title" | "description" | "label" | "slug"
>[]
}

const defaultValue = {
currentVersion: undefined,
documentTree: [],
documentMap: [],
}
Expand All @@ -20,14 +24,15 @@ export const PageContext = createContext<PageContext>(defaultValue)

const getDocumentMap = (
tree: DocumentTypeTree[],
): Pick<DocumentTypes, "title" | "slug" | "label" | "description">[] =>
): Pick<DocumentTypeTree, "title" | "slug" | "label" | "description">[] =>
tree.flatMap(({ title, slug, label, description, children }) => [
{ title, slug, label, description },
...getDocumentMap(children),
])

export type PageProviderProps = PropsWithChildren<
{
currentVersion: string | undefined | null
documentTree: DocumentTypeTree[]
documentTabs?: DocumentTypesNavigationItem[]
documentBreadcrumbs?: DocumentTypesNavigationItem[]
Expand Down
3 changes: 2 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"remark-slug": "^7.0.1",
"typescript": "^5.4.5",
"unified": "^10.1.2",
"unist-util-visit": "^5.0.0"
"unist-util-visit": "^5.0.0",
"find-packages": "^10.0.4"
}
}
4 changes: 2 additions & 2 deletions docs/pages/404.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export const getStaticProps = getStaticCommonProps

type PageProps = InferGetStaticPropsType<typeof getStaticProps>

const Page: NextPage<PageProps> = ({ documentTree }) => {
const Page: NextPage<PageProps> = ({ currentVersion, documentTree }) => {
const { tc } = useI18n()

return (
<PageProvider {...{ documentTree }}>
<PageProvider {...{ currentVersion, documentTree }}>
<TopLayout>
<SEO
title={tc("not-found.title") as string}
Expand Down
12 changes: 8 additions & 4 deletions docs/pages/examples/[slug].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,22 @@ export const getStaticProps = async ({
params,
}: GetStaticPropsContext) => {
const {
props: { documentTree },
props: { currentVersion, documentTree },
} = await getStaticCommonProps({ locale })
const slug = params?.slug as Slug

const props = { documentTree, slug }
const props = { currentVersion, documentTree, slug }

return { props }
}

type PageProps = InferGetStaticPropsType<typeof getStaticProps>

const Page: NextPage<PageProps> = ({ slug: currentSlug, documentTree }) => {
const Page: NextPage<PageProps> = ({
currentVersion,
slug: currentSlug,
documentTree,
}) => {
const { t, tc } = useI18n()

const examples = useMemo(() => {
Expand Down Expand Up @@ -105,7 +109,7 @@ const Page: NextPage<PageProps> = ({ slug: currentSlug, documentTree }) => {
}, [currentSlug])

return (
<PageProvider {...{ documentTree }}>
<PageProvider {...{ currentVersion, documentTree }}>
<TopLayout>
<SEO
title={t(`examples.${currentSlug}.title`)}
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export const getStaticProps = getStaticCommonProps

type PageProps = InferGetStaticPropsType<typeof getStaticProps>

const Page: NextPage<PageProps> = ({ documentTree }) => {
const Page: NextPage<PageProps> = ({ currentVersion, documentTree }) => {
const { t, tc } = useI18n()

return (
<PageProvider {...{ documentTree }}>
<PageProvider {...{ currentVersion, documentTree }}>
<TopLayout>
<SEO
title={t("home.title")}
Expand Down
18 changes: 17 additions & 1 deletion docs/utils/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@ import {
getDocumentPaths,
} from "./contentlayer"
import { toKebabCase } from "./string"
import { findPackages } from "find-packages"
import path from "path"
import { CONSTANT } from "constant"

const getVersion = async () => {
const packages = await findPackages(
path.resolve(CONSTANT.PATH.ROOT, "packages", "react"),
)

const { version } = packages[0].manifest

return version ? `v${version}` : null
}

export const getStaticCommonProps = async ({
locale,
}: GetStaticPropsContext) => {
const currentVersion = await getVersion()
const documents = getDocuments(locale)
const documentTree = getDocumentTree(omitDocumentTabs(documents))()

return { props: { documents, documentTree } }
return { props: { currentVersion, documents, documentTree } }
}

export const getStaticDocumentProps =
(documentTypeName: DocumentTypeNames) =>
async ({ params, locale, defaultLocale }: GetStaticPropsContext) => {
const currentVersion = await getVersion()
const paths = [
toKebabCase(documentTypeName),
...toArray(params?.slug ?? []),
Expand Down Expand Up @@ -55,6 +70,7 @@ export const getStaticDocumentProps =

return {
props: {
currentVersion,
...document,
documentTree,
documentBreadcrumbs,
Expand Down
24 changes: 4 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 84d46bb

Please sign in to comment.