From 8536bfeba3629beb03183196246e64fa72047ac9 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Thu, 10 Oct 2024 13:48:50 -0400 Subject: [PATCH 1/2] fix: sidebar navigation scrolling bug + housecleaning (#1638) --- .../ui/app/src/sidebar/DismissableSidebar.tsx | 2 +- packages/ui/app/src/sidebar/SidebarLink.tsx | 5 +- .../src/sidebar/nodes/SidebarApiGroupNode.tsx | 18 ----- .../sidebar/nodes/SidebarApiPackageChild.tsx | 32 ++++++--- .../sidebar/nodes/SidebarApiPackageNode.tsx | 41 ++--------- .../nodes/SidebarGroupApiReferenceNode.tsx | 22 ++++++ .../app/src/sidebar/nodes/SidebarLinkNode.tsx | 12 +--- .../sidebar/nodes/SidebarNavigationChild.tsx | 35 +++++---- .../app/src/sidebar/nodes/SidebarPageNode.tsx | 12 +++- .../nodes/SidebarRootApiPackageNode.tsx | 32 +++------ .../nodes/SidebarRootApiReferenceNode.tsx | 57 --------------- .../src/sidebar/nodes/SidebarRootChild.tsx | 24 +++++++ .../src/sidebar/nodes/SidebarRootHeading.tsx | 36 ++++------ .../app/src/sidebar/nodes/SidebarRootNode.tsx | 72 +++++++++++++------ .../sidebar/nodes/SidebarRootSectionNode.tsx | 34 ++------- .../src/sidebar/nodes/SidebarSectionNode.tsx | 21 ++---- packages/ui/app/src/util/scrollToCenter.ts | 32 ++++++++- 17 files changed, 222 insertions(+), 265 deletions(-) delete mode 100644 packages/ui/app/src/sidebar/nodes/SidebarApiGroupNode.tsx create mode 100644 packages/ui/app/src/sidebar/nodes/SidebarGroupApiReferenceNode.tsx delete mode 100644 packages/ui/app/src/sidebar/nodes/SidebarRootApiReferenceNode.tsx create mode 100644 packages/ui/app/src/sidebar/nodes/SidebarRootChild.tsx diff --git a/packages/ui/app/src/sidebar/DismissableSidebar.tsx b/packages/ui/app/src/sidebar/DismissableSidebar.tsx index c5165f3690..cc3df64168 100644 --- a/packages/ui/app/src/sidebar/DismissableSidebar.tsx +++ b/packages/ui/app/src/sidebar/DismissableSidebar.tsx @@ -28,7 +28,7 @@ export function DismissableSidebar({ className }: { className?: string }): React set(DESKTOP_SIDEBAR_OPEN_ATOM, (prev) => { if (event.clientX < 20) { return true; - } else if (prev && event.target instanceof HTMLElement) { + } else if (prev && event.target instanceof Node) { return sidebarRef.current?.contains(event.target) ?? false; } else { return false; diff --git a/packages/ui/app/src/sidebar/SidebarLink.tsx b/packages/ui/app/src/sidebar/SidebarLink.tsx index a9f8a96393..fa5490351a 100644 --- a/packages/ui/app/src/sidebar/SidebarLink.tsx +++ b/packages/ui/app/src/sidebar/SidebarLink.tsx @@ -18,6 +18,7 @@ import { useImperativeHandle, useRef, } from "react"; +import { useCallbackOne } from "use-memo-one"; import { IS_READY_ATOM, SIDEBAR_SCROLL_CONTAINER_ATOM, useAtomEffect, useCloseMobileSidebar } from "../atoms"; import { FernLink } from "../components/FernLink"; import { useHref } from "../hooks/useHref"; @@ -205,10 +206,10 @@ export const SidebarSlugLink = forwardRef { if (props.selected) { - scrollToCenter(get(SIDEBAR_SCROLL_CONTAINER_ATOM), ref.current, !get(IS_READY_ATOM)); + scrollToCenter(get.peek(SIDEBAR_SCROLL_CONTAINER_ATOM), ref.current, get.peek(IS_READY_ATOM)); } }, [props.selected], diff --git a/packages/ui/app/src/sidebar/nodes/SidebarApiGroupNode.tsx b/packages/ui/app/src/sidebar/nodes/SidebarApiGroupNode.tsx deleted file mode 100644 index cfbb22a70e..0000000000 --- a/packages/ui/app/src/sidebar/nodes/SidebarApiGroupNode.tsx +++ /dev/null @@ -1,18 +0,0 @@ -import type * as FernNavigation from "@fern-api/fdr-sdk/navigation"; -import { SidebarApiPackageChild } from "./SidebarApiPackageChild"; - -interface SidebarApiGroupNodeProps { - nodeChildren: (FernNavigation.ApiPackageChild | FernNavigation.ChangelogNode)[]; - shallow: boolean; -} -export function SidebarApiGroupNode({ nodeChildren, shallow }: SidebarApiGroupNodeProps): React.ReactElement { - return ( - - ); -} diff --git a/packages/ui/app/src/sidebar/nodes/SidebarApiPackageChild.tsx b/packages/ui/app/src/sidebar/nodes/SidebarApiPackageChild.tsx index 8bb27966b2..0da0ef68a5 100644 --- a/packages/ui/app/src/sidebar/nodes/SidebarApiPackageChild.tsx +++ b/packages/ui/app/src/sidebar/nodes/SidebarApiPackageChild.tsx @@ -1,5 +1,5 @@ import type * as FernNavigation from "@fern-api/fdr-sdk/navigation"; -import { visitDiscriminatedUnion } from "@fern-api/ui-core-utils"; +import { UnreachableCaseError } from "ts-essentials"; import { SidebarApiLeafNode } from "./SidebarApiLeafNode"; import { SidebarApiPackageNode } from "./SidebarApiPackageNode"; import { SidebarChangelogNode } from "./SidebarChangelogNode"; @@ -14,14 +14,24 @@ interface SidebarApiPackageChild { } export function SidebarApiPackageChild({ node, depth, shallow }: SidebarApiPackageChild): React.ReactElement { - return visitDiscriminatedUnion(node)._visit({ - page: (node) => , - link: (node) => , - endpoint: (node) => , - endpointPair: (node) => , - webSocket: (node) => , - webhook: (node) => , - apiPackage: (node) => , - changelog: (node) => , - }); + switch (node.type) { + case "page": + return ; + case "link": + return ; + case "endpoint": + return ; + case "endpointPair": + return ; + case "webSocket": + return ; + case "webhook": + return ; + case "apiPackage": + return ; + case "changelog": + return ; + default: + throw new UnreachableCaseError(node); + } } diff --git a/packages/ui/app/src/sidebar/nodes/SidebarApiPackageNode.tsx b/packages/ui/app/src/sidebar/nodes/SidebarApiPackageNode.tsx index ac04752f96..791a872a77 100644 --- a/packages/ui/app/src/sidebar/nodes/SidebarApiPackageNode.tsx +++ b/packages/ui/app/src/sidebar/nodes/SidebarApiPackageNode.tsx @@ -1,4 +1,4 @@ -import type * as FernNavigation from "@fern-api/fdr-sdk/navigation"; +import * as FernNavigation from "@fern-api/fdr-sdk/navigation"; import { useCallback } from "react"; import { useIsApiReferenceShallowLink, @@ -10,6 +10,8 @@ import { import { CollapsibleSidebarGroup } from "../CollapsibleSidebarGroup"; import { SidebarSlugLink } from "../SidebarLink"; import { SidebarApiPackageChild } from "./SidebarApiPackageChild"; +import { SidebarGroupApiReferenceNode } from "./SidebarGroupApiReferenceNode"; +import { SidebarPageNode } from "./SidebarPageNode"; export interface SidebarApiPackageNodeProps { node: FernNavigation.ApiReferenceNode | FernNavigation.ApiPackageNode; @@ -35,45 +37,16 @@ export function SidebarApiPackageNode({ [depth, shallow], ); - if (node.children.length === 0) { - if (node.overviewPageId == null) { - return null; - } - - if (node.hidden && !selected) { - return null; - } - - return ( -