aboutsummaryrefslogtreecommitdiff
path: root/components/chapters.tsx
blob: facf1e152140cbc3ef1c4a055c954c03aa582477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { ReactNode, useState, CSSProperties } from 'react';

import { NavbarItem } from '../components/navbar';

import RemoveRoundedIcon from '@material-ui/icons/RemoveRounded';
import KeyboardArrowDownRoundedIcon from '@material-ui/icons/KeyboardArrowDownRounded';

export interface chapter {
	name: string;
	sectionLink?: string;
	children?: Array<chapter>;
}

function NavbarChapter(props: {
	level: number;
	chapter: chapter;
	children?: ReactNode;
}) {
	var [ collapsed, setCollapsed ] = useState(true);

	var icon = <div className={ "collapseIcon" + (collapsed ? "" : " collapsed") }>
		{
			props.chapter.children?.length > 0 ?
				<KeyboardArrowDownRoundedIcon/> :
				<RemoveRoundedIcon/>
		}
	</div>

	var classes = [
		"chapter",
		`indentLevel${props.level}`
	]
	!collapsed && classes.push("childrenCollapsed");

	var outercss = /* { "--children-height": 0 + "px" } */ {} as CSSProperties;

	return <NavbarItem
	icon={icon}
	classList={classes}
	title={props.chapter.name}
	onIconClick={() => props.chapter.children?.length > 0 && setCollapsed(!collapsed)}
	href={props.chapter.sectionLink}
	key={(() => Math.round(Math.random() * 1e12))()}
	style={{
		marginLeft: 12 * props.level,
	}} outerStyle={outercss}>
		{props.children}
	</NavbarItem>
}

class Chapter {
	constructor(public chapters: Array<chapter>, public level: number) {}
	render() {
		return <div className="chapterChildren">
			{
				this.chapters?.map(chapter => {
					return <NavbarChapter level={this.level} chapter={chapter}>
						{ new Chapter(chapter.children, this.level + 1).render() }
					</NavbarChapter>
				})
			}
		</div>
	}
}

export default function Chapters(props: {
	chapters: Array<chapter>;
}) {
	return new Chapter(props.chapters, 0).render();
}