|
| 1 | +module Link = Next.Link; |
| 2 | + |
| 3 | +// Structure defined by `scripts/extract-indices.js` |
| 4 | +let indexData: |
| 5 | + Js.Dict.t({ |
| 6 | + . |
| 7 | + "moduleName": string, |
| 8 | + "headers": |
| 9 | + array({ |
| 10 | + . |
| 11 | + "name": string, |
| 12 | + "href": string, |
| 13 | + }), |
| 14 | + }) = [%raw |
| 15 | + "require('../index_data/dom_api_index.json')" |
| 16 | +]; |
| 17 | + |
| 18 | +module Sidebar = SidebarLayout.Sidebar; |
| 19 | +module UrlPath = SidebarLayout.UrlPath; |
| 20 | +module NavItem = Sidebar.NavItem; |
| 21 | +module Category = Sidebar.Category; |
| 22 | + |
| 23 | +let moduleNavs = [| |
| 24 | + NavItem.{name: "Storage", href: "/apis/latest/dom/storage"}, |
| 25 | + NavItem.{name: "Storage2", href: "/apis/latest/dom/storage2"}, |
| 26 | +|]; |
| 27 | + |
| 28 | +let categories = [|Category.{name: "Modules", items: moduleNavs}|]; |
| 29 | + |
| 30 | +module Docs = { |
| 31 | + [@react.component] |
| 32 | + let make = (~components=ApiMarkdown.default, ~children) => { |
| 33 | + let router = Next.Router.useRouter(); |
| 34 | + let route = router.route; |
| 35 | + |
| 36 | + // Gather data for the CollapsibleSection |
| 37 | + let headers = |
| 38 | + Belt.Option.( |
| 39 | + Js.Dict.get(indexData, route) |
| 40 | + ->map(data => { |
| 41 | + data##headers |
| 42 | + ->Belt.Array.map(header => (header##name, "#" ++ header##href)) |
| 43 | + }) |
| 44 | + ->getWithDefault([||]) |
| 45 | + ); |
| 46 | + |
| 47 | + let moduleName = |
| 48 | + Belt.Option.( |
| 49 | + Js.Dict.get(indexData, route) |
| 50 | + ->map(data => data##moduleName) |
| 51 | + ->getWithDefault("?") |
| 52 | + ); |
| 53 | + |
| 54 | + let (isSidebarOpen, setSidebarOpen) = React.useState(_ => false); |
| 55 | + let toggleSidebar = () => setSidebarOpen(prev => !prev); |
| 56 | + |
| 57 | + let urlPath = UrlPath.parse(~base="/apis", route); |
| 58 | + |
| 59 | + let breadcrumbs = |
| 60 | + Belt.Option.map( |
| 61 | + urlPath, |
| 62 | + v => { |
| 63 | + let {UrlPath.version} = v; |
| 64 | + let prefix = |
| 65 | + UrlPath.[{name: "Modules", href: "/apis/" ++ version}]; |
| 66 | + UrlPath.toBreadCrumbs(~prefix, v); |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + let toplevelNav = |
| 71 | + switch (urlPath) { |
| 72 | + | Some(urlPath) => |
| 73 | + let version = UrlPath.(urlPath.version); |
| 74 | + let backHref = Some(UrlPath.fullUpLink(urlPath)); |
| 75 | + <Sidebar.ToplevelNav title="Dom" version ?backHref />; |
| 76 | + | None => React.null |
| 77 | + }; |
| 78 | + |
| 79 | + // Todo: We need to introduce router state to be able to |
| 80 | + // listen to anchor changes (#get, #map,...) |
| 81 | + let preludeSection = <Sidebar.CollapsibleSection headers moduleName />; |
| 82 | + |
| 83 | + let sidebar = |
| 84 | + <Sidebar |
| 85 | + isOpen=isSidebarOpen |
| 86 | + toggle=toggleSidebar |
| 87 | + categories |
| 88 | + route={router.route} |
| 89 | + toplevelNav |
| 90 | + preludeSection |
| 91 | + />; |
| 92 | + |
| 93 | + let pageTitle = |
| 94 | + switch (breadcrumbs) { |
| 95 | + | Some([_, {name}]) => name |
| 96 | + | Some([_, _, {name}]) => "Dom." ++ name |
| 97 | + | _ => "Dom" |
| 98 | + }; |
| 99 | + |
| 100 | + <SidebarLayout |
| 101 | + metaTitle={pageTitle ++ " | ReScript API"} |
| 102 | + theme=`Reason |
| 103 | + components |
| 104 | + sidebarState=(isSidebarOpen, setSidebarOpen) |
| 105 | + sidebar |
| 106 | + ?breadcrumbs> |
| 107 | + children |
| 108 | + </SidebarLayout>; |
| 109 | + }; |
| 110 | +}; |
| 111 | + |
| 112 | +module Prose = { |
| 113 | + [@react.component] |
| 114 | + let make = (~children) => { |
| 115 | + <Docs components=Markdown.default> children </Docs>; |
| 116 | + }; |
| 117 | +}; |
0 commit comments