Skip to content

Commit 924e84a

Browse files
committed
apidocs
1 parent f566bec commit 924e84a

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/ApiDocs.res

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Docgen = RescriptTools.Docgen
22

3+
let apiDocsRootPath = "/docs/manual/api"
4+
35
type rec node = {
46
name: string,
57
path: array<string>,
@@ -99,24 +101,24 @@ module SidebarTree = {
99101

100102
let location = useLocation()
101103

102-
Console.log(location)
103-
104104
// Use ReactRouter's useLocation if needed, or refactor to not use router
105105
// let location = ReactRouter.useLocation()
106106
// let url = ""
107107
// let url = location.pathname->Url.parse
108108

109-
let moduleRoute =
110-
(location.pathname :> string)
111-
->String.replace(`/docs/manual/api/`, "")
112-
->String.split("/")
109+
// let moduleRoute =
110+
// (location.pathname :> string)
111+
// ->String.replace(`/docs/manual/api/`, "")
112+
// ->String.split("/")
113+
114+
// Console.log2("moduleRoute", moduleRoute)
113115

114116
let summaryClassName = "truncate py-1 md:h-auto tracking-tight text-gray-60 font-medium text-14 rounded-sm hover:bg-gray-20 hover:-ml-2 hover:py-1 hover:pl-2 "
115117
let classNameActive = " bg-fire-5 text-red-500 -ml-2 pl-2 font-medium hover:bg-fire-70"
116118

117119
let subMenu = switch items->Array.length > 0 {
118120
| true =>
119-
<div className={"xl:hidden ml-5"}>
121+
<div className={"xl:hidden ml-5"} dataTestId={`submenu-${node.name}`}>
120122
<ul className={"list-none py-0.5"}>
121123
<RightSidebar items />
122124
</ul>
@@ -125,32 +127,44 @@ module SidebarTree = {
125127
}
126128

127129
let rec renderNode = node => {
128-
let isCurrentRoute = Array.join(moduleRoute, "/") === Array.join(node.path, "/")
130+
// this value is the relative path to this module, e.g. "/array" or "/int"
131+
let relativePath = node.path->Array.join("/")
132+
133+
// This is the full path to this module, e.g. "/docs/manual/api/stdlib/array" or "/docs/manual/api/stdlib/int"
134+
// TODO: get the "stdlib" part dynamically based on docs page, probably as a prop from the loader?s
135+
let fullPath = apiDocsRootPath ++ "/stdlib/" ++ relativePath
136+
137+
// Console.log3(node, fullPath, location.pathname)
138+
139+
let isCurrentRoute = fullPath == (location.pathname :> string)
129140

130141
let classNameActive = isCurrentRoute ? classNameActive : ""
131142

132143
let hasChildren = node.children->Array.length > 0
133-
let href = node.path->Array.join("/")
144+
145+
// TODO RR7 - this doesnt seem to work
146+
let href = node.path->Array.slice(~start=1, ~end=Array.length(node.path) - 1)->Array.join("/")
134147

135148
let tocModule = isCurrentRoute ? subMenu : React.null
136149

137150
switch hasChildren {
138151
| true =>
139-
let open_ =
140-
href ===
141-
moduleRoute
142-
->Array.slice(~start=0, ~end=Array.length(moduleRoute) - 1)
143-
->Array.join("/")
144-
145-
<details key={href} open_>
146-
<summary className={summaryClassName ++ classNameActive}>
147-
<Link.String className={"inline-block w-10/12"} to=href>
152+
let open_ = isCurrentRoute
153+
// href === apiDocsRootPath ++ moduleRoute->Array.join("/")
154+
155+
<details
156+
key={href} open_ dataTestId={`has-children-${node.name}-${isCurrentRoute->Bool.toString}`}
157+
>
158+
<summary className={summaryClassName ++ classNameActive} dataTestId={`${href}`}>
159+
<Link.String className={"inline-block w-10/12"} to={fullPath}>
148160
{node.name->React.string}
149161
</Link.String>
150162
</summary>
151163
tocModule
164+
// TODO: won't this always be false?
152165
{if hasChildren {
153166
<ul className={"ml-5"}>
167+
// TODO RR7 - fix this, I think i am recursively rendering stuff
154168
{node.children
155169
->Array.map(renderNode)
156170
->React.array}
@@ -160,17 +174,20 @@ module SidebarTree = {
160174
}}
161175
</details>
162176
| false =>
163-
<li className="list-none mt-1 leading-4" key=href>
177+
<li className="list-none mt-1 leading-4" key=href dataTestId={`no-children-${node.name}`}>
164178
<summary className={summaryClassName ++ classNameActive}>
165-
<Link.String className={"block"} to=href> {node.name->React.string} </Link.String>
179+
<Link.String className={"block"} to=fullPath> {node.name->React.string} </Link.String>
166180
</summary>
167181
tocModule
168182
</li>
169183
}
170184
}
171185

172-
// TODO
173-
let url = None
186+
let moduleRoute = []
187+
188+
let {pathname} = ReactRouter.useLocation()
189+
// TODO RR7 - make sure this works
190+
let url = (pathname :> string)->Url.parse->Some
174191
// location.pathname
175192
// ->Url.parse
176193
// ->Some
@@ -364,6 +381,7 @@ let make = (props: props) => {
364381
}
365382
}
366383

384+
// TODO RR7: Is this used anywhere?
367385
let rightSidebar = switch props {
368386
| Ok({module_: {items}}) if Array.length(items) > 0 =>
369387
<div className="hidden xl:block lg:w-1/5 md:h-auto md:relative overflow-y-visible bg-white">

0 commit comments

Comments
 (0)