Skip to content

Commit

Permalink
docs: fix sidebar not updating when api reference path changes (#8409)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Aug 5, 2024
1 parent 8eb538f commit 2682e2e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
7 changes: 6 additions & 1 deletion www/apps/api-reference/components/Tags/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Tags = () => {
const [expand, setExpand] = useState<string>("")
const { baseSpecs, setBaseSpecs } = useBaseSpecs()
const { addItems } = useSidebar()
const { area } = useArea()
const { area, prevArea } = useArea()

const { data } = useSWR<ExpandedDocument>(
loadData && !baseSpecs
Expand Down Expand Up @@ -63,6 +63,11 @@ const Tags = () => {

useEffect(() => {
if (baseSpecs) {
if (prevArea !== area) {
setBaseSpecs(null)
return
}

addItems(
baseSpecs.tags?.map((tag) => {
const tagPathName = getSectionId([tag.name.toLowerCase()])
Expand Down
5 changes: 4 additions & 1 deletion www/apps/api-reference/providers/area.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use client"

import type { Area } from "@/types/openapi"
import { useSearch } from "docs-ui"
import { usePrevious, useSearch } from "docs-ui"
import { createContext, useContext, useEffect, useState } from "react"

type AreaContextType = {
area: Area
prevArea: Area | undefined
setArea: (value: Area) => void
}

Expand All @@ -18,6 +19,7 @@ type AreaProviderProps = {

const AreaProvider = ({ area: passedArea, children }: AreaProviderProps) => {
const [area, setArea] = useState<Area>(passedArea)
const prevArea = usePrevious(area)
const { defaultFilters, setDefaultFilters } = useSearch()

useEffect(() => {
Expand All @@ -30,6 +32,7 @@ const AreaProvider = ({ area: passedArea, children }: AreaProviderProps) => {
<AreaContext.Provider
value={{
area,
prevArea,
setArea,
}}
>
Expand Down
2 changes: 1 addition & 1 deletion www/apps/api-reference/providers/base-specs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ReactNode, createContext, useContext, useState } from "react"

type BaseSpecsContextType = {
baseSpecs: ExpandedDocument | null
setBaseSpecs: (value: ExpandedDocument) => void
setBaseSpecs: React.Dispatch<React.SetStateAction<ExpandedDocument | null>>
getSecuritySchema: (securityName: string) => SecuritySchemeObject | null
}

Expand Down
11 changes: 11 additions & 0 deletions www/apps/api-reference/providers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import {
SidebarProvider as UiSidebarProvider,
usePageLoading,
usePrevious,
useScrollController,
} from "docs-ui"
import { config } from "../config"
import { usePathname } from "next/navigation"
import { useCallback } from "react"

type SidebarProviderProps = {
children?: React.ReactNode
Expand All @@ -13,6 +16,13 @@ type SidebarProviderProps = {
const SidebarProvider = ({ children }: SidebarProviderProps) => {
const { isLoading, setIsLoading } = usePageLoading()
const { scrollableElement } = useScrollController()
const pathname = usePathname()
const prevPathname = usePrevious(pathname)

const resetOnCondition = useCallback(
() => prevPathname !== undefined && prevPathname !== pathname,
[pathname, prevPathname]
)

return (
<UiSidebarProvider
Expand All @@ -21,6 +31,7 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
shouldHandleHashChange={true}
scrollableElement={scrollableElement}
initialItems={config.sidebar}
resetOnCondition={resetOnCondition}
>
{children}
</UiSidebarProvider>
Expand Down
45 changes: 39 additions & 6 deletions www/packages/docs-ui/src/providers/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type SidebarContextType = {
shouldHandleHashChange: boolean
sidebarRef: React.RefObject<HTMLDivElement>
goBack: () => void
resetItems: () => void
} & SidebarStyleOptions

export const SidebarContext = createContext<SidebarContextType | null>(null)
Expand All @@ -75,11 +76,16 @@ export type ActionOptionsType = {
ignoreExisting?: boolean
}

export type ActionType = {
type: "add" | "update"
items: SidebarItemType[]
options?: ActionOptionsType
}
export type ActionType =
| {
type: "add" | "update"
items: SidebarItemType[]
options?: ActionOptionsType
}
| {
type: "replace"
replacementItems: SidebarSectionItemsType
}

const findItem = (
section: SidebarItemType[],
Expand All @@ -105,8 +111,15 @@ const findItem = (

export const reducer = (
state: SidebarSectionItemsType,
{ type, items, options }: ActionType
actionData: ActionType
) => {
if (actionData.type === "replace") {
return actionData.replacementItems
}

const { type, options } = actionData
let { items } = actionData

const {
section = SidebarItemSections.TOP,
parent,
Expand Down Expand Up @@ -173,6 +186,7 @@ export type SidebarProviderProps = {
shouldHandlePathChange?: boolean
scrollableElement?: Element | Window
staticSidebarItems?: boolean
resetOnCondition?: () => boolean
} & SidebarStyleOptions

export const SidebarProvider = ({
Expand All @@ -186,6 +200,7 @@ export const SidebarProvider = ({
staticSidebarItems = false,
disableActiveTransition = false,
noTitleStyling = false,
resetOnCondition,
}: SidebarProviderProps) => {
const [items, dispatch] = useReducer(reducer, {
top: initialItems?.top || [],
Expand Down Expand Up @@ -315,6 +330,17 @@ export const SidebarProvider = ({
router.replace(backItem.path!)
}

const resetItems = useCallback(() => {
dispatch({
type: "replace",
replacementItems: {
top: initialItems?.top || [],
bottom: initialItems?.bottom || [],
mobile: initialItems?.mobile || [],
},
})
}, [initialItems])

useEffect(() => {
if (shouldHandleHashChange) {
init()
Expand Down Expand Up @@ -413,6 +439,12 @@ export const SidebarProvider = ({
}
}, [getCurrentSidebar, activePath])

useEffect(() => {
if (resetOnCondition?.()) {
resetItems()
}
}, [resetOnCondition])

return (
<SidebarContext.Provider
value={{
Expand All @@ -435,6 +467,7 @@ export const SidebarProvider = ({
shouldHandleHashChange,
sidebarRef,
goBack,
resetItems,
}}
>
{children}
Expand Down

0 comments on commit 2682e2e

Please sign in to comment.