1 "use client"
2
3 import React, { useEffect } from "react"
4 import { Button } from "../../Button"
5 import { TriangleDownMini } from "@medusajs/icons"
6 import clsx from "clsx"
7 import { ContentMenuSectionDivider } from "../Divider"
8 import { useIsBrowser } from "../../.."
9
10 export type ContentMenuSectionProps = {
11 title: string
12 children: React.ReactNode
13 hideChildrenDivider?: boolean
14 }
15
16 export const ContentMenuSection = ({
17 title,
18 children,
19 hideChildrenDivider = false,
20 }: ContentMenuSectionProps) => {
21 const [open, setOpen] = React.useState(true)
22 const { isBrowser } = useIsBrowser()
23 const localStorageKey = `content-menu-section-${title.toLowerCase().replace(/\s/g, "-")}`
24 useEffect(() => {
25 if (!isBrowser) {
26 return
27 }
28
29 const storedState = localStorage.getItem(localStorageKey)
30 if (storedState !== null) {
31 setOpen(storedState === "true")
32 }
33 }, [isBrowser, localStorageKey])
34
35 const toggleOpen = () => {
36 setOpen((prev) => {
37 const newState = !prev
38 if (isBrowser) {
39 localStorage.setItem(localStorageKey, newState.toString())
40 }
41 return newState
42 })
43 }
44
45 if (!children) {
46 return null
47 }
48
49 return (
50 <>
51 <div
52 className={clsx(
53 "py-docs_0.75 px-docs_1 flex gap-docs_0.25 justify-between",
54 "cursor-pointer items-center text-medusa-fg-muted"
55 )}
56 onClick={toggleOpen}
57 data-testid="content-menu-section-header"
58 >
59 <span className="uppercase text-code-label font-monospace">
60 [{title}]
61 </span>
62 <Button variant="transparent" className="!p-[2.5px]">
63 <TriangleDownMini
64 className={clsx("transition-transform", !open && "rotate-180")}
65 />
66 </Button>
67 </div>
68 <ContentMenuSectionDivider />
69 {open && (
70 <>
71 {children}
72 {!hideChildrenDivider && <ContentMenuSectionDivider />}
73 </>
74 )}
75 </>
76 )
77 }