Skip to content

Commit 0321c41

Browse files
committed
working on syntax page
1 parent 6942b6b commit 0321c41

File tree

5 files changed

+98
-49
lines changed

5 files changed

+98
-49
lines changed

app/routes/SyntaxLookupRoute.res

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
1+
open ReactRouter
2+
open Mdx
3+
4+
type loaderData = {mdxSources: array<SyntaxLookup.item>}
5+
6+
let convert = (mdx: Mdx.attributes): SyntaxLookup.item => {
7+
{
8+
category: SyntaxLookup.Category.fromString(mdx.category->Option.getOrThrow),
9+
id: mdx.id->Option.getOrThrow,
10+
keywords: mdx.keywords->Option.getOr([]),
11+
name: mdx.name->Option.getOrThrow,
12+
children: <div> {React.string("TODO: render MDX here")} </div>,
13+
status: SyntaxLookup.Status.fromString(mdx.status->Option.getOr("active")),
14+
summary: mdx.summary->Option.getOrThrow,
15+
}
16+
}
17+
18+
let loader: Loader.t<loaderData> = async ({request}) => {
19+
let allMdx = await Shims.runWithoutLogging(() => loadAllMdx())
20+
21+
let mdxSources =
22+
allMdx
23+
->Array.filter(page => page.path->String.includes("docs/syntax"))
24+
->Array.map(convert)
25+
26+
{
27+
mdxSources: mdxSources,
28+
}
29+
}
30+
131
let default = () => {
2-
// TODO: RR7 implement syntax lookup page
3-
<div> {React.string("This is the Syntax Lookup Route")} </div>
32+
let {mdxSources} = useLoaderData()
33+
<SyntaxLookup mdxSources />
434
}

src/SyntaxLookup.res

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,13 @@ let getAnchor = path => {
9999

100100
module Tag = {
101101
@react.component
102-
let make = (~deprecated: bool, ~text: string) => {
102+
let make = (~deprecated: bool, ~text: string, ~id: string) => {
103+
let navigate = ReactRouter.useNavigate()
104+
let onClick = evt => {
105+
navigate("#" ++ id)
106+
}
103107
<span
108+
onClick
104109
className={`
105110
py-1 px-3 rounded text-16
106111
${deprecated
@@ -132,6 +137,7 @@ module DetailBox = {
132137

133138
<div>
134139
<div className="text-24 border-b border-gray-40 pb-4 mb-4 font-semibold"> summaryEl </div>
140+
// TODO: get the actual children
135141
<div className="mt-16"> children </div>
136142
</div>
137143
}
@@ -144,7 +150,6 @@ type state =
144150

145151
let scrollToTop = () => WebAPI.Window.scrollTo(window, ~options={left: 0.0, top: 0.0})
146152

147-
type props = {mdxSources: array<MdxRemote.output>}
148153
type params = {slug: string}
149154

150155
let decode = (json: JSON.t) => {
@@ -177,21 +182,39 @@ let decode = (json: JSON.t) => {
177182
}
178183
}
179184

180-
let make = (props: props) => {
181-
let {mdxSources} = props
185+
type item = {
186+
id: string,
187+
keywords: array<string>,
188+
name: string,
189+
summary: string,
190+
category: Category.t,
191+
children: React.element,
192+
status: Status.t,
193+
}
182194

195+
@react.component
196+
let make = (~mdxSources: array<item>) => {
183197
let allItems = mdxSources->Array.map(mdxSource => {
184-
let {id, keywords, category, summary, name, status} = decode(mdxSource.frontmatter)
185-
186-
let children =
187-
<MdxRemote
188-
frontmatter={mdxSource.frontmatter}
189-
compiledSource={mdxSource.compiledSource}
190-
scope={mdxSource.scope}
191-
components={MarkdownComponents.default}
192-
/>
193-
194-
{Item.id, keywords, category, summary, name, status, children}
198+
let {id, keywords, category, summary, name, status} = mdxSource
199+
200+
// TODO: children?
201+
// let children =
202+
// <MdxRemote
203+
// frontmatter={mdxSource.frontmatter}
204+
// compiledSource={mdxSource.compiledSource}
205+
// scope={mdxSource.scope}
206+
// components={MarkdownComponents.default}
207+
// />
208+
209+
{
210+
Item.id,
211+
keywords,
212+
category,
213+
summary,
214+
name,
215+
status,
216+
children: <div> {React.string("Hello!")} </div>,
217+
}
195218
})
196219

197220
let fuseOpts = Fuse.Options.t(
@@ -210,9 +233,13 @@ let make = (props: props) => {
210233
let location = ReactRouter.useLocation()
211234
let (state, setState) = React.useState(_ => ShowAll)
212235

213-
let findItemById = id => allItems->Array.find(item => item.id === id)
236+
let findItemById = id => allItems->Array.find(item => "#" ++ item.id === id)
214237

215-
let findItemByExactName = name => allItems->Array.find(item => item.name === name)
238+
let findItemByExactName = name => {
239+
allItems->Array.find(item => {
240+
item.name === name
241+
})
242+
}
216243

217244
let searchItems = value =>
218245
fuse
@@ -221,13 +248,15 @@ let make = (props: props) => {
221248
m["item"]
222249
})
223250

251+
let navigate = ReactRouter.useNavigate()
252+
224253
// This effect is responsible for updating the view state when the router anchor changes.
225254
// This effect is triggered when:
226255
// [A] The page first loads.
227256
// [B] The search box is cleared.
228257
// [C] The search box value exactly matches an item name.
229258
React.useEffect(() => {
230-
switch getAnchor((location.pathname :> string)) {
259+
switch location.hash {
231260
| None => setState(_ => ShowAll)
232261
| Some(anchor) =>
233262
switch findItemById(anchor) {
@@ -239,7 +268,7 @@ let make = (props: props) => {
239268
}
240269
}
241270
None
242-
}, [location.pathname])
271+
}, [location.hash])
243272

244273
// onSearchValueChange() is called when:
245274
// [A] The search value changes.
@@ -252,7 +281,7 @@ let make = (props: props) => {
252281
// [3] Search does not match an item - immediately update the view state to show filtered items.
253282
let onSearchValueChange = value => {
254283
switch value {
255-
| "" => ReactRouter.navigate("/syntax-lookup")
284+
| "" => navigate("/syntax-lookup")
256285
| value =>
257286
switch findItemByExactName(value) {
258287
| None => {
@@ -318,7 +347,7 @@ let make = (props: props) => {
318347
onSearchValueChange(item.name)
319348
}
320349
<span className="mr-2 mb-2 cursor-pointer" onMouseDown key=item.name>
321-
<Tag text={item.name} deprecated={item.status == Deprecated} />
350+
<Tag text={item.name} deprecated={item.status == Deprecated} id=item.id />
322351
</span>
323352
})
324353
let el =
@@ -392,20 +421,3 @@ let make = (props: props) => {
392421
</div>
393422
</>
394423
}
395-
396-
let getStaticProps: Next.GetStaticProps.t<props, params> = async _ctx => {
397-
let dir = Node.Path.resolve("misc_docs", "syntax")
398-
399-
let allFiles = Node.Fs.readdirSync(dir)->Array.map(async file => {
400-
let fullPath = Node.Path.join2(dir, file)
401-
let source = fullPath->Node.Fs.readFileSync
402-
await MdxRemote.serialize(
403-
source,
404-
{parseFrontmatter: true, mdxOptions: MdxRemote.defaultMdxOptions},
405-
)
406-
})
407-
408-
let mdxSources = await Promise.all(allFiles)
409-
410-
{"props": {mdxSources: mdxSources}}
411-
}

src/SyntaxLookup.resi

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/bindings/ReactRouter.res

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
open WebAPI
22

3+
type navigateOptions = {replace?: bool}
4+
35
@module("react-router-dom")
4-
external navigate: string => unit = "navigate"
6+
external navigate: (string, ~options: navigateOptions=?) => unit = "navigate"
57

68
// https://api.reactrouter.com/v7/functions/react_router.useNavigate.html
79
@module("react-router")
@@ -136,10 +138,15 @@ module Mdx = {
136138
canonical: Path.t,
137139
category?: string,
138140
description?: string,
141+
id?: string,
142+
keywords?: array<string>,
143+
name?: string,
139144
metaTitle?: string,
140145
order?: int,
141146
path: string,
142147
section?: string,
148+
summary?: string,
149+
status?: string,
143150
slug: string,
144151
title: string,
145152
}

src/components/SearchBox.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ let make = (
7878
onBlur
7979
className={(
8080
state === Active ? "border-fire" : "border-fire-30"
81-
) ++ " flex items-center border rounded-lg py-4 px-5"}>
81+
) ++ " flex items-center border rounded-lg py-4 px-5"}
82+
>
8283
<Icon.MagnifierGlass
8384
className={(state === Active ? "text-fire" : "text-fire-70") ++ " w-4 h-4"}
8485
/>
86+
// TODO RR7: deleting input stops working with one character left...
8587
<input
8688
value
8789
ref={ReactDOM.Ref.domRef((Obj.magic(textInput): React.ref<Nullable.t<Dom.element>>))}
@@ -92,7 +94,11 @@ let make = (
9294
className="text-16 outline-hidden ml-4 w-full"
9395
type_="text"
9496
/>
95-
<button onFocus className={value === "" ? "hidden" : "block"} onMouseDown=onMouseDownClear>
97+
<button
98+
onFocus
99+
className={"value" /* TODO */ === "" ? "hidden" : "block"}
100+
onMouseDown=onMouseDownClear
101+
>
96102
<Icon.Close className="w-4 h-4 text-fire" />
97103
</button>
98104
</div>

0 commit comments

Comments
 (0)