CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/account/config/menu.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Menu } from "antd";
7
import { useEffect, useMemo, useState } from "react";
8
9
import { Icon } from "@cocalc/frontend/components/icon";
10
import { capitalize } from "@cocalc/util/misc";
11
import { menuItem, MenuItems } from "components/antd-menu-items";
12
import { useRouter } from "next/router";
13
import { menu, topIcons } from "./register";
14
15
export default function ConfigMenu({ main, sub }) {
16
const router = useRouter();
17
const [openKeys, setOpenKeys] = useState<string[]>([main]);
18
19
// This useEffect is to ensure that the selected section (main)
20
// is always expanded, e.g., when you get there by clicking on
21
// a search result:
22
useEffect(() => {
23
if (openKeys.indexOf(main) == -1) {
24
setOpenKeys(openKeys.concat([main]));
25
}
26
}, [main]);
27
28
const items: MenuItems = useMemo(
29
() =>
30
Object.keys(menu).map((main) => {
31
const sub = Object.keys(menu[main]).map((sub) => {
32
const { title, icon, danger } = menu[main][sub];
33
return menuItem(`${main}/${sub}`, title, icon, undefined, danger);
34
});
35
36
const title = capitalize(main);
37
const icon = topIcons[main] ?? "gear";
38
return menuItem(main, title, <Icon name={icon} />, sub);
39
}),
40
[]
41
);
42
43
return (
44
<Menu
45
mode="inline"
46
openKeys={openKeys}
47
onOpenChange={(keys) => {
48
setOpenKeys(keys);
49
}}
50
selectedKeys={[main + "/" + sub]}
51
style={{ height: "100%" }}
52
onSelect={(e) => {
53
router.push(`/config/${e.keyPath[0]}`, undefined, {
54
scroll: false,
55
});
56
}}
57
items={items}
58
/>
59
);
60
}
61
62