-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.tsx
103 lines (89 loc) · 2.22 KB
/
app.tsx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { FC, ReactElement, useState } from "react";
import { Box, Newline, Text } from "ink";
import { Form, FormProps } from "ink-form";
import SelectInput, { Item } from "ink-select-input";
import { Config, ConfigEdit, loadConfig } from "./Config";
type MenuProps = {
onSelect: (item: Item<string>) => void;
};
const MenuItems = ({ onSelect }: MenuProps) => {
const items: Item<string>[] = [
{
label: "Sandbox Config",
value: "config",
},
{
label: "Test",
value: "test",
},
{
label: "Exit",
value: "exit",
},
];
return <SelectInput items={items} onSelect={onSelect} />;
};
export interface MenuItemProps {
close: (page?: string | null) => void;
config: Config;
}
const Test: React.FC<MenuItemProps> = ({ close }) => {
setTimeout(() => {
close();
}, 1000);
return (
<>
<Text>{`TestTest`}</Text>
</>
);
};
const Menu: React.FC<MenuItemProps> = ({ close }) => {
return (
<MenuItems
onSelect={(item) => {
if (item.value == "exit") process.exit();
close(item.value);
}}
/>
);
};
const componentMap: { [key: string]: FC<MenuItemProps> } = {
config: ConfigEdit,
test: Test,
menu: Menu,
};
type Props = {
cmdState?: string;
path: string;
};
export default function App({ path, cmdState = "" }: Props) {
const [state, setState] = useState<string>(cmdState);
const [config, setConfig] = useState<Config | null>(null);
React.useEffect(() => {
loadConfig().then(setConfig);
}, []);
React.useEffect(() => {
if (state == "") setTimeout(() => setState("menu"), 1);
}, [state]);
const SelectedComponent = config ? componentMap[state] : null;
return (
<Box borderStyle="round" flexDirection="column" borderColor="greenBright">
<Box justifyContent="center">
<Text color="green" underline bold>
{config ? config.sbas.name : ""}: {state}
</Text>
</Box>
{SelectedComponent && config && (
<SelectedComponent
close={(page) => {
console.clear();
setState(page || "");
}}
config={config}
/>
)}
<Newline />
<Text color="blueBright">{path}</Text>
</Box>
);
}