From 650f4ddfb249f3b55875d4854fe7969e7facf85c Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Date: Fri, 25 Nov 2022 15:49:36 -0500 Subject: [PATCH] feat(guide): add next and previous page buttons (#8777) --- apps/guide/src/components/PageButton.tsx | 13 +++++ apps/guide/src/components/SidebarLayout.astro | 49 +++++++++++++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 apps/guide/src/components/PageButton.tsx diff --git a/apps/guide/src/components/PageButton.tsx b/apps/guide/src/components/PageButton.tsx new file mode 100644 index 000000000..f03c07d90 --- /dev/null +++ b/apps/guide/src/components/PageButton.tsx @@ -0,0 +1,13 @@ +export function PageButton({ url, title, direction }: { direction: 'next' | 'prev'; title: string; url: string }) { + return ( + +

{title}

+

+ {direction === 'next' ? 'Next Page' : 'Previous Page'} +

+
+ ); +} diff --git a/apps/guide/src/components/SidebarLayout.astro b/apps/guide/src/components/SidebarLayout.astro index a07e05dcb..abf6e29b6 100644 --- a/apps/guide/src/components/SidebarLayout.astro +++ b/apps/guide/src/components/SidebarLayout.astro @@ -4,13 +4,32 @@ import type { MarkdownLayoutProps } from 'astro'; import { ExternalLink } from './ExternalLink.jsx'; import { Navbar } from './Navbar.jsx'; import { Outline } from './Outline.jsx'; +import { PageButton } from './PageButton.jsx'; import { SidebarItems } from './SidebarItems.jsx'; import { generateGithubURL } from '~/util/url.js'; const pages = await Astro.glob<{ category: string; title: string }>('../pages/**/*.mdx'); type Props = MarkdownLayoutProps<{}>; -const { headings, url } = Astro.props; +const { headings, url, frontmatter } = Astro.props; + +const groupedPages = pages.reduce>((acc, page) => { + const { category } = page.frontmatter; + acc[category] ??= []; + + acc[category]?.push(page); + return acc; +}, {}); + +// @ts-expect-error props is not typed +const category = frontmatter.category as string; + +const curCategoryPages = groupedPages[category]; +const curCategoryIndex = curCategoryPages!.findIndex((page) => page.url === url); + +const pagePrev = curCategoryPages![curCategoryIndex - 1]; +// eslint-disable-next-line @typescript-eslint/restrict-plus-operands +const pageNext = curCategoryPages![curCategoryIndex + 1]; ---