|
1 | | -import React, { Fragment } from 'react'; |
| 1 | +import React, { Fragment, useState } from 'react'; |
2 | 2 | import { Link } from 'gatsby'; |
| 3 | +import { IoIosArrowDown, IoIosArrowForward } from 'react-icons/io'; |
3 | 4 |
|
4 | | -// Toggles sidebar menus, only one may be open at a time |
5 | | -function toggleMenu(id) { |
6 | | - const element = document.getElementById(id); |
| 5 | +/** |
| 6 | + * Interactive Sidebar with toggleable categories. |
| 7 | + * |
| 8 | + * @requires categories of form: [ { name: "foobar", entires: []} ] |
| 9 | + */ |
| 10 | +const Sidebar = ({ categories = [], currentPathname }) => { |
| 11 | + const [selectedCategory, selectCategory] = useState(() => { |
| 12 | + for (const { categoryName, entries } of categories) { |
| 13 | + for (const entry of entries) { |
| 14 | + if (currentPathname.includes(entry.node.frontmatter.path)) { |
| 15 | + return categoryName; |
| 16 | + } |
| 17 | + } |
| 18 | + } |
7 | 19 |
|
8 | | - const scanners = document.getElementById('Scanners'); |
9 | | - const persistenceProviders = document.getElementById('Persistence Providers'); |
10 | | - const hooks = document.getElementById('Hooks'); |
| 20 | + throw new Error( |
| 21 | + `Could not find matching Category for path ${currentPathname}` |
| 22 | + ); |
| 23 | + }); |
11 | 24 |
|
12 | | - if (element !== scanners) scanners.style.display = 'none'; |
13 | | - if (element !== persistenceProviders) |
14 | | - persistenceProviders.style.display = 'none'; |
15 | | - if (element !== hooks) hooks.style.display = 'none'; |
16 | | - |
17 | | - element.style.display = element.style.display === 'block' ? 'none' : 'block'; |
18 | | -} |
19 | | - |
20 | | -// Determine the naming of Sidebar categories |
21 | | -function getCategoryTitle(category) { |
22 | | - let categoryTitle = ''; |
23 | | - |
24 | | - switch (category[0].node.frontmatter.category) { |
25 | | - case 'scanner': |
26 | | - categoryTitle = 'Scanners'; |
27 | | - break; |
28 | | - |
29 | | - case 'hook': |
30 | | - categoryTitle = |
31 | | - category[0].node.frontmatter.type === 'persistenceProvider' |
32 | | - ? 'Persistence Providers' |
33 | | - : 'Hooks'; |
34 | | - break; |
35 | | - |
36 | | - default: |
37 | | - categoryTitle = 'Unknown Category'; |
38 | | - break; |
39 | | - } |
40 | | - |
41 | | - return categoryTitle; |
42 | | -} |
43 | | - |
44 | | -const Sidebar = (props) => { |
45 | 25 | return ( |
46 | 26 | <nav className="sidebar"> |
47 | | - {props.dataArray.map((category, index) => ( |
48 | | - <Fragment key={index}> |
49 | | - <h1 |
50 | | - onClick={() => toggleMenu(getCategoryTitle(category))} |
51 | | - className="sidebar-header" |
| 27 | + {categories.map(({ categoryName, entries }) => ( |
| 28 | + <Fragment key={categoryName}> |
| 29 | + <button |
| 30 | + onClick={() => |
| 31 | + selectCategory( |
| 32 | + selectedCategory === categoryName ? null : categoryName |
| 33 | + ) |
| 34 | + } |
| 35 | + className="sidebar-category" |
52 | 36 | > |
53 | | - {getCategoryTitle(category)} |
54 | | - </h1> |
| 37 | + {categoryName} |
| 38 | + <IoIosArrowDown |
| 39 | + className="arrow" |
| 40 | + style={{ |
| 41 | + display: selectedCategory === categoryName ? 'block' : 'none', |
| 42 | + }} |
| 43 | + /> |
| 44 | + <IoIosArrowForward |
| 45 | + className="arrow" |
| 46 | + style={{ |
| 47 | + display: selectedCategory === categoryName ? 'none' : 'block', |
| 48 | + }} |
| 49 | + /> |
| 50 | + </button> |
55 | 51 | <ul |
56 | | - id={getCategoryTitle(category)} |
| 52 | + id={categoryName} |
| 53 | + name="Category" |
57 | 54 | className="list-unstyled components show" |
| 55 | + style={{ |
| 56 | + display: selectedCategory === categoryName ? 'block' : 'none', |
| 57 | + }} |
58 | 58 | > |
59 | | - {category.map((element) => ( |
60 | | - <li key={element.node.frontmatter.title}> |
| 59 | + {entries.map((element) => ( |
| 60 | + <li |
| 61 | + key={element.node.frontmatter.title} |
| 62 | + className="sidebar-element" |
| 63 | + > |
61 | 64 | <Link |
62 | 65 | to={`/integrations/${element.node.frontmatter.path}`} |
63 | 66 | activeClassName="active-Link" |
|
0 commit comments