From e6c3c7635d7e25b00382bda740894df1986a273a Mon Sep 17 00:00:00 2001 From: conico974 Date: Thu, 26 Jun 2025 11:35:02 +0200 Subject: [PATCH] refactor toc components to client (#3394) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolas Dorseuil Co-authored-by: Samy Pessé --- .../src/components/PageIcon/PageIcon.tsx | 5 +- .../TableOfContents/PageDocumentItem.tsx | 41 ++---- .../TableOfContents/PageGroupItem.tsx | 18 +-- .../TableOfContents/PageLinkItem.tsx | 15 +-- .../components/TableOfContents/PagesList.tsx | 44 ++----- .../TableOfContents/TOCPageIcon.tsx | 2 +- .../TableOfContents/TableOfContents.tsx | 9 +- .../encodeClientTableOfContents.ts | 124 ++++++++++++++++++ packages/gitbook/src/lib/typescript.ts | 22 ++++ 9 files changed, 190 insertions(+), 90 deletions(-) create mode 100644 packages/gitbook/src/components/TableOfContents/encodeClientTableOfContents.ts diff --git a/packages/gitbook/src/components/PageIcon/PageIcon.tsx b/packages/gitbook/src/components/PageIcon/PageIcon.tsx index 5061fd95e..f935df782 100644 --- a/packages/gitbook/src/components/PageIcon/PageIcon.tsx +++ b/packages/gitbook/src/components/PageIcon/PageIcon.tsx @@ -4,7 +4,10 @@ import { Icon, type IconName } from '@gitbook/icons'; import { Emoji } from '@/components/primitives'; import { type ClassValue, tcls } from '@/lib/tailwind'; -export function PageIcon(props: { page: RevisionPage; style?: ClassValue }) { +export function PageIcon(props: { + page: Pick; + style?: ClassValue; +}) { const { page, style } = props; if (page.emoji) { diff --git a/packages/gitbook/src/components/TableOfContents/PageDocumentItem.tsx b/packages/gitbook/src/components/TableOfContents/PageDocumentItem.tsx index 462924248..13b398887 100644 --- a/packages/gitbook/src/components/TableOfContents/PageDocumentItem.tsx +++ b/packages/gitbook/src/components/TableOfContents/PageDocumentItem.tsx @@ -1,48 +1,32 @@ -import type { GitBookSiteContext } from '@/lib/context'; -import { getPagePaths, hasPageVisibleDescendant } from '@/lib/pages'; -import { tcls } from '@/lib/tailwind'; -import { - type RevisionPage, - type RevisionPageDocument, - SiteInsightsLinkPosition, -} from '@gitbook/api'; +'use client'; +import { tcls } from '@/lib/tailwind'; +import type { ClientTOCPageDocument } from './encodeClientTableOfContents'; + +import { SiteInsightsLinkPosition } from '@gitbook/api'; import { PagesList } from './PagesList'; import { TOCPageIcon } from './TOCPageIcon'; import { ToggleableLinkItem } from './ToggleableLinkItem'; -export async function PageDocumentItem(props: { - rootPages: RevisionPage[]; - page: RevisionPageDocument; - context: GitBookSiteContext; -}) { - const { rootPages, page, context } = props; - let href = context.linker.toPathForPage({ pages: rootPages, page }); - // toPathForPage can returns an empty path, this will cause all links to point to the current page. - if (href === '') { - href = '/'; - } +export function PageDocumentItem(props: { page: ClientTOCPageDocument }) { + const { page } = props; return (
  • 0 ? ( ) : null } diff --git a/packages/gitbook/src/components/TableOfContents/PageGroupItem.tsx b/packages/gitbook/src/components/TableOfContents/PageGroupItem.tsx index 99964bd7b..d1f946de9 100644 --- a/packages/gitbook/src/components/TableOfContents/PageGroupItem.tsx +++ b/packages/gitbook/src/components/TableOfContents/PageGroupItem.tsx @@ -1,18 +1,14 @@ -import type { GitBookSiteContext } from '@/lib/context'; -import type { RevisionPage, RevisionPageGroup } from '@gitbook/api'; +'use client'; + +import type { ClientTOCPageGroup } from './encodeClientTableOfContents'; -import { hasPageVisibleDescendant } from '@/lib/pages'; import { tcls } from '@/lib/tailwind'; import { PagesList } from './PagesList'; import { TOCPageIcon } from './TOCPageIcon'; -export function PageGroupItem(props: { - rootPages: RevisionPage[]; - page: RevisionPageGroup; - context: GitBookSiteContext; -}) { - const { rootPages, page, context } = props; +export function PageGroupItem(props: { page: ClientTOCPageGroup }) { + const { page } = props; return (
  • @@ -36,8 +32,8 @@ export function PageGroupItem(props: { {page.title} - {hasPageVisibleDescendant(page) ? ( - + {page.descendants && page.descendants.length > 0 ? ( + ) : null}
  • ); diff --git a/packages/gitbook/src/components/TableOfContents/PageLinkItem.tsx b/packages/gitbook/src/components/TableOfContents/PageLinkItem.tsx index b250e8f2f..bf14adf9d 100644 --- a/packages/gitbook/src/components/TableOfContents/PageLinkItem.tsx +++ b/packages/gitbook/src/components/TableOfContents/PageLinkItem.tsx @@ -1,22 +1,21 @@ -import type { GitBookSiteContext } from '@/lib/context'; -import { type RevisionPageLink, SiteInsightsLinkPosition } from '@gitbook/api'; +'use client'; + import { Icon } from '@gitbook/icons'; +import type { ClientTOCPageLink } from './encodeClientTableOfContents'; import { Link } from '@/components/primitives'; -import { resolveContentRef } from '@/lib/references'; import { tcls } from '@/lib/tailwind'; +import { SiteInsightsLinkPosition } from '@gitbook/api'; import { TOCPageIcon } from './TOCPageIcon'; -export async function PageLinkItem(props: { page: RevisionPageLink; context: GitBookSiteContext }) { - const { page, context } = props; - - const resolved = await resolveContentRef(page.target, context); +export function PageLinkItem(props: { page: ClientTOCPageLink }) { + const { page } = props; return (
  • {pages.map((page) => { - if (page.type === 'computed') { - throw new Error( - 'Unexpected computed page, it should have been computed in the API' - ); - } - - if (page.hidden) { - return null; - } - switch (page.type) { case 'document': - return ( - - ); + return ; case 'link': - return ; + return ; case 'group': - return ( - - ); + return ; default: assertNever(page); diff --git a/packages/gitbook/src/components/TableOfContents/TOCPageIcon.tsx b/packages/gitbook/src/components/TableOfContents/TOCPageIcon.tsx index 266f3c1e2..c93b1383a 100644 --- a/packages/gitbook/src/components/TableOfContents/TOCPageIcon.tsx +++ b/packages/gitbook/src/components/TableOfContents/TOCPageIcon.tsx @@ -7,7 +7,7 @@ import { PageIcon } from '../PageIcon'; /** * Styled page icon for the table of contents. */ -export function TOCPageIcon({ page }: { page: RevisionPage }) { +export function TOCPageIcon({ page }: { page: Pick }) { return (