Raw
1 "use client"
2
3 // @refresh reset
4
5 import React, { useCallback, useEffect, useMemo, useRef } from "react"
6 import { Sidebar } from "types"
7 import { Badge } from "@/components/Badge"
8 import { checkSidebarItemVisibility } from "@/utils/check-sidebar-item-visibility"
9 import { useSidebar } from "@/providers/Sidebar"
10 import { SidebarItem } from "@/components/Sidebar/Item"
11 import { SidebarItemTitle } from "@/components/Sidebar/Item/Title"
12 import clsx from "clsx"
13 import Link from "next/link"
14 import { useMobile } from "@/providers/Mobile"
15
16 export type SidebarItemLinkProps = {
17 item: Sidebar.SidebarItemLink
18 nested?: boolean
19 isParentCategoryOpen?: boolean
20 } & React.AllHTMLAttributes<HTMLLIElement>
21
22 export const SidebarItemLink = ({
23 item,
24 className,
25 nested = false,
26 isParentCategoryOpen,
27 }: SidebarItemLinkProps) => {
28 const {
29 isItemActive,
30 setMobileSidebarOpen: setSidebarOpen,
31 disableActiveTransition,
32 sidebarRef,
33 sidebarTopHeight,
34 } = useSidebar()
35 const { isMobile } = useMobile()
36 const active = useMemo(
37 () =>
38 isItemActive({
39 item,
40 checkLinkChildren: false,
41 }),
42 [isItemActive, item]
43 )
44 const ref = useRef<HTMLLIElement>(null)
45
46 const getNewTopCalculator = useCallback(() => {
47 if (!sidebarRef.current || !ref.current) {
48 return 0
49 }
50
51 const sidebarBoundingRect = sidebarRef.current.getBoundingClientRect()
52 const itemBoundingRect = ref.current.getBoundingClientRect()
53
54 return (
55 itemBoundingRect.top -
56 (sidebarBoundingRect.top + sidebarTopHeight) +
57 sidebarRef.current.scrollTop -
58 10 // remove extra margin just in case
59 )
60 }, [sidebarTopHeight, sidebarRef.current, ref.current])
61
62 useEffect(() => {
63 if (
64 !active ||
65 !ref.current ||
66 !sidebarRef.current ||
67 isMobile ||
68 !isParentCategoryOpen
69 ) {
70 return
71 }
72
73 const isVisible = checkSidebarItemVisibility(
74 (ref.current.children.item(0) as HTMLElement) || ref.current,
75 !disableActiveTransition
76 )
77 if (isVisible) {
78 return
79 }
80 if (!disableActiveTransition) {
81 ref.current.scrollIntoView({
82 block: "center",
83 })
84 } else {
85 sidebarRef.current.scrollTo({
86 top: getNewTopCalculator(),
87 })
88 }
89 }, [
90 active,
91 sidebarRef.current,
92 disableActiveTransition,
93 isMobile,
94 ref.current,
95 getNewTopCalculator,
96 isParentCategoryOpen,
97 ])
98
99 useEffect(() => {
100 if (active && isMobile) {
101 setSidebarOpen(false)
102 }
103 }, [active, isMobile])
104
105 const hasChildren = useMemo(() => {
106 return !item.hideChildren && (item.children?.length || 0) > 0
107 }, [item.children])
108
109 const isTitleOneWord = useMemo(
110 () => item.title.split(" ").length === 1,
111 [item.title]
112 )
113
114 return (
115 <li ref={ref}>
116 <span className="block px-docs_0.75">
117 <Link
118 href={item.path}
119 className={clsx(
120 "py-docs_0.25 px-docs_0.5",
121 "block w-full rounded-docs_sm",
122 !isTitleOneWord && "break-words",
123 active && [
124 "bg-medusa-bg-base",
125 "shadow-elevation-card-rest dark:shadow-elevation-card-rest-dark",
126 "text-medusa-fg-base",
127 ],
128 !active && [
129 !nested && "text-medusa-fg-subtle",
130 nested && "text-medusa-fg-muted",
131 "hover:bg-medusa-bg-base-hover lg:hover:bg-medusa-bg-subtle-hover",
132 ],
133 "text-compact-small-plus",
134 "flex justify-between items-center gap-[6px]",
135 className
136 )}
137 target={item.type === "external" ? "_blank" : undefined}
138 rel={item.type === "external" ? "noopener noreferrer" : undefined}
139 {...item.linkProps}
140 >
141 <SidebarItemTitle
142 title={item.title}
143 isTitleOneWord={isTitleOneWord}
144 className={clsx(nested && "inline-block pl-docs_1.5")}
145 />
146 {item.additionalElms}
147 {item.badge && (
148 <Badge variant={item.badge.variant}>{item.badge.text}</Badge>
149 )}
150 </Link>
151 </span>
152 {hasChildren && (
153 <ul
154 className={clsx(
155 "ease-ease",
156 "flex flex-col gap-docs_0.125",
157 "pt-docs_0.125 pb-docs_0.5"
158 )}
159 >
160 {item.children!.map((childItem, index) => (
161 <SidebarItem
162 item={childItem}
163 key={index}
164 nested={!item.childrenSameLevel}
165 isParentCategoryOpen={isParentCategoryOpen}
166 />
167 ))}
168 </ul>
169 )}
170 </li>
171 )
172 }