import type {GetStaticProps, NextPage} from 'next'; import Link from 'next/link'; import * as fs from 'fs/promises'; import * as path from 'path'; import * as Navigation from '@tesseract-design/web-navigation-react'; import { DefaultLayout } from '@/components/DefaultLayout'; import * as React from 'react' import {Section} from '@/components/Section'; type Page = { id: string, href: string, label: string, } type Props = { componentPages: Page[], examplePages: Page[], } const createPageLink = (p: Page) => (
{p.label}
) const IndexPage: NextPage = ({ componentPages, examplePages, }) => { return (
{componentPages.map(createPageLink)}
{examplePages.map(createPageLink)}
) } export const getStaticProps: GetStaticProps = async () => { const pagesPath = path.resolve('src/pages'); const categoriesPath = path.resolve(pagesPath, 'categories'); const categoriesRaw = await fs.readdir(categoriesPath); const categoriesIndexPage = await Promise.all( categoriesRaw.map(async (c) => { const indexPath = await path.resolve(categoriesPath, c, 'index.tsx'); try { const statResult = await fs.stat(indexPath); return [c, statResult.isFile()]; } catch { // noop } return [c, false]; }) ) as [string, boolean][]; const categories = categoriesIndexPage .filter(([, hasIndexPage]) => hasIndexPage) .map(([key]) => key); const examplesPath = path.resolve(pagesPath, 'examples'); const examplesRaw = await fs.readdir(examplesPath); const examplesIndexPage = await Promise.all( examplesRaw.map(async (c) => { const indexPath = await path.resolve(examplesPath, c, 'index.tsx'); try { const statResult = await fs.stat(indexPath); return [c, statResult.isFile()]; } catch { // noop } return [c, false]; }) ) as [string, boolean][]; const examples = examplesIndexPage .filter(([, hasIndexPage]) => hasIndexPage) .map(([key]) => key); return { props: { componentPages: categories.map((c) => ({ id: c, href: `/categories/${c}`, label: c.split('-').map((cc) => cc.slice(0, 1).toUpperCase() + cc.slice(1)).join(' '), })), examplePages: examples.map((e) => ({ id: e, href: `/examples/${e}`, label: e.split('-').map((ee) => ee.slice(0, 1).toUpperCase() + ee.slice(1)).join(' '), })), }, }; }; export default IndexPage