2023-04-09 00:24:42 -05:00
|
|
|
'use client';
|
|
|
|
|
|
2023-04-14 00:40:25 -05:00
|
|
|
import Link from 'next/link.js';
|
2023-04-09 00:24:42 -05:00
|
|
|
import { useState } from 'react';
|
2023-04-28 08:33:11 -05:00
|
|
|
import { useSectionContext } from '../context/section';
|
2023-04-27 16:03:27 -05:00
|
|
|
import { useThemeContext } from '../context/theme';
|
|
|
|
|
import { Doc, DocsStructure } from '../types/types';
|
|
|
|
|
import { toKebabCase } from '../utils/stringUtils';
|
2023-04-07 23:18:45 -05:00
|
|
|
import styles from './SideBar.module.css';
|
|
|
|
|
|
2023-04-09 14:50:50 -05:00
|
|
|
function TreeNode({ doc }: { doc: Doc }) {
|
2023-04-09 00:24:42 -05:00
|
|
|
const [isExpanded, setIsExpanded] =
|
|
|
|
|
useState<boolean>(true);
|
|
|
|
|
|
2023-04-28 08:33:11 -05:00
|
|
|
const { section, setSection } = useSectionContext();
|
|
|
|
|
|
|
|
|
|
const docTitleId = toKebabCase(doc.title);
|
|
|
|
|
|
|
|
|
|
const isActiveSection = docTitleId === section;
|
|
|
|
|
|
|
|
|
|
const linkStyle = isActiveSection
|
|
|
|
|
? styles.activeLink
|
|
|
|
|
: styles.link;
|
|
|
|
|
|
2023-04-09 00:24:42 -05:00
|
|
|
const hasChildren =
|
|
|
|
|
doc.children && doc.children?.length > 0;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<li className={styles.listItem}>
|
|
|
|
|
{hasChildren ? (
|
2023-04-13 09:27:38 -05:00
|
|
|
<div className={styles.expandable}>
|
2023-04-25 07:54:34 -05:00
|
|
|
{doc.copy ? (
|
|
|
|
|
<a
|
2023-04-28 08:33:11 -05:00
|
|
|
onClick={() => setSection(docTitleId)}
|
|
|
|
|
href={`#${docTitleId}`}
|
|
|
|
|
className={linkStyle}
|
2023-04-25 07:54:34 -05:00
|
|
|
>
|
|
|
|
|
{doc.title}
|
|
|
|
|
</a>
|
|
|
|
|
) : (
|
2023-04-28 08:33:11 -05:00
|
|
|
<div className={styles.linkGroup}>
|
|
|
|
|
{doc.title}
|
|
|
|
|
</div>
|
2023-04-25 07:54:34 -05:00
|
|
|
)}
|
2023-04-18 22:51:50 -05:00
|
|
|
<div
|
|
|
|
|
className={styles.chevronContainer}
|
|
|
|
|
onClick={() => setIsExpanded(!isExpanded)}
|
|
|
|
|
>
|
2023-04-13 09:27:38 -05:00
|
|
|
{isExpanded ? (
|
|
|
|
|
<svg
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
width="16"
|
|
|
|
|
height="16"
|
|
|
|
|
fill="currentColor"
|
|
|
|
|
className={styles.chevron}
|
|
|
|
|
viewBox="0 0 16 16"
|
|
|
|
|
>
|
|
|
|
|
<path
|
|
|
|
|
fillRule="evenodd"
|
|
|
|
|
d="M7.646 4.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1-.708.708L8 5.707l-5.646 5.647a.5.5 0 0 1-.708-.708l6-6z"
|
|
|
|
|
/>
|
|
|
|
|
</svg>
|
|
|
|
|
) : (
|
|
|
|
|
<svg
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
width="16"
|
|
|
|
|
height="16"
|
|
|
|
|
fill="currentColor"
|
|
|
|
|
className={styles.chevron}
|
|
|
|
|
viewBox="0 0 16 16"
|
|
|
|
|
>
|
|
|
|
|
<path
|
|
|
|
|
fillRule="evenodd"
|
|
|
|
|
d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z"
|
|
|
|
|
/>
|
|
|
|
|
</svg>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-04-09 00:24:42 -05:00
|
|
|
</div>
|
2023-04-25 07:54:34 -05:00
|
|
|
) : doc.copy ? (
|
2023-04-10 00:04:49 -05:00
|
|
|
<a
|
2023-04-28 08:33:11 -05:00
|
|
|
onClick={() => setSection(docTitleId)}
|
|
|
|
|
href={`#${docTitleId}`}
|
|
|
|
|
className={linkStyle}
|
2023-04-10 00:04:49 -05:00
|
|
|
>
|
2023-04-09 00:24:42 -05:00
|
|
|
{doc.title}
|
|
|
|
|
</a>
|
2023-04-25 07:54:34 -05:00
|
|
|
) : (
|
|
|
|
|
<div>{doc.title}</div>
|
2023-04-09 00:24:42 -05:00
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{doc.children &&
|
|
|
|
|
doc.children?.length > 0 &&
|
|
|
|
|
isExpanded && <ListTree docs={doc.children} />}
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 14:50:50 -05:00
|
|
|
function ListTree({ docs }: { docs: Doc[] }) {
|
2023-04-09 00:24:42 -05:00
|
|
|
return (
|
|
|
|
|
<ul className={styles.list}>
|
|
|
|
|
{docs.map(d => (
|
|
|
|
|
<TreeNode key={d.title} doc={d} />
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SideBar({
|
2023-04-28 10:51:57 -05:00
|
|
|
versionDocStructure,
|
2023-04-09 00:24:42 -05:00
|
|
|
}: {
|
2023-04-28 10:51:57 -05:00
|
|
|
versionDocStructure: DocsStructure;
|
2023-04-09 00:24:42 -05:00
|
|
|
}) {
|
2023-04-20 22:39:47 -05:00
|
|
|
const { theme, setTheme } = useThemeContext();
|
|
|
|
|
|
2023-04-07 23:18:45 -05:00
|
|
|
return (
|
|
|
|
|
<div className={styles.container}>
|
2023-04-20 23:36:52 -05:00
|
|
|
<div className={styles.titleContainer}>
|
2023-04-20 22:39:47 -05:00
|
|
|
<Link href="/" className={styles.link}>
|
|
|
|
|
<h1 className={styles.title}>
|
|
|
|
|
<span className={styles.onspring}>
|
|
|
|
|
Onspring
|
|
|
|
|
</span>{' '}
|
|
|
|
|
<span className={styles.api}>API</span>
|
|
|
|
|
</h1>
|
|
|
|
|
</Link>
|
2023-04-20 23:36:52 -05:00
|
|
|
<label className={styles.switch}>
|
2023-04-20 22:39:47 -05:00
|
|
|
<input
|
2023-04-20 23:36:52 -05:00
|
|
|
title="Toggle dark mode"
|
2023-04-20 22:39:47 -05:00
|
|
|
onChange={() =>
|
|
|
|
|
setTheme(theme === 'light' ? 'dark' : 'light')
|
|
|
|
|
}
|
|
|
|
|
type="checkbox"
|
2023-04-21 09:40:29 -05:00
|
|
|
checked={theme === 'dark'}
|
2023-04-20 22:39:47 -05:00
|
|
|
/>
|
2023-04-20 23:36:52 -05:00
|
|
|
<span
|
|
|
|
|
className={`${styles.slider} ${styles.round}`}
|
|
|
|
|
></span>
|
2023-04-20 22:39:47 -05:00
|
|
|
</label>
|
|
|
|
|
</div>
|
2023-04-28 10:51:57 -05:00
|
|
|
<ListTree docs={versionDocStructure.docs} />
|
2023-04-07 23:18:45 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|