-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathModelArea.tsx
223 lines (216 loc) · 6.15 KB
/
ModelArea.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* (c) 2021-2022, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import {
Box,
Collapse,
Divider,
Flex,
HStack,
List,
ListItem,
Select,
Stack,
Text,
useDisclosure,
} from "@chakra-ui/react";
import AreaHeading from "../common/AreaHeading";
import HeadedScrollablePanel from "../common/HeadedScrollablePanel";
import { docStyles } from "../common/documentation-styles";
import { useRouterTabSlug } from "../router-hooks";
import DocumentationHeading from "./common/DocumentationHeading";
import ShowMoreButton from "./common/ShowMoreButton";
import Highlight from "./reference/Highlight";
import CodeEmbed from "./common/CodeEmbed";
import OpenButton from "../project/OpenButton";
import { useModelData } from "./ml/use-model-data";
import { ChangeEvent, useCallback, useState } from "react";
interface ModelTopicEntry {
name: string;
text: string;
code: string | ((actionName: string) => string);
slug: string;
detail?: string;
alternativesLabel?: string;
}
const modelTopicEntries: ModelTopicEntry[] = [
{
name: "Class names",
text: "Return the class names of the current model.",
code: "import model\n\n\nnamesList = model.get_class_names()",
slug: "class-names",
},
{
name: "Current action",
text: "Return the current recognised action.",
code: "import model\n\n\ndisplay.scroll(model.current_action())",
slug: "current-action",
},
{
name: "Is action",
text: "Check if an action is currently being performed.",
code: (actionName) =>
`import model\n\n\nwhile True:\n if model.is_action('${actionName}'):\n display.scroll('Yes')`,
slug: "is-action",
detail: "Triggered on the current recognised action.",
alternativesLabel: "Select action:",
},
{
name: "Was action",
text: "Check if an action was performed.",
code: (actionName) =>
`import model\n\n\nwhile True:\n if model.was_action('${actionName}'):\n display.scroll('Yes')`,
slug: "was-action",
detail: "Triggered if the specified action was recognised since last time.",
alternativesLabel: "Select action:",
},
];
const ModelArea = () => {
const [modelData] = useModelData();
return (
<HeadedScrollablePanel
heading={
<AreaHeading
name="Machine learning"
description="Use the machine learning model that was trained"
/>
}
>
<Box
p={5}
pb={1}
fontSize="sm"
sx={{
...docStyles,
}}
>
<Stack spacing={3}>
<Text>Open your data.json file to get started.</Text>
<OpenButton alignSelf="flex-start" fileType="mljson" mode="button" />
</Stack>
</Box>
{modelData.length > 0 && (
<>
<Box
p={5}
pb={1}
fontSize="sm"
sx={{
...docStyles,
}}
>
<Text>Some optional introduction text</Text>
</Box>
<List flex="1 1 auto">
{modelTopicEntries.map((entry) => (
<ListItem key={entry.slug}>
<ModelTopicEntry content={entry} />
<Divider />
</ListItem>
))}
</List>
</>
)}
</HeadedScrollablePanel>
);
};
interface MlItemProps {
content: ModelTopicEntry;
}
const ModelTopicEntry = ({ content }: MlItemProps) => {
const { name, text, slug, code, detail, alternativesLabel } = content;
const hasMore = true;
const disclosure = useDisclosure();
const [anchor] = useRouterTabSlug("model");
const [modelData] = useModelData();
const [selectedAlternative, setSelectedAlternative] = useState<
string | undefined
>(alternativesLabel ? modelData[0].ID.toString() : undefined);
const [processedCode, setProcessedCode] = useState<string>(() => {
return typeof code === "string" ? code : code(modelData[0].name);
});
const handleSelectChange = useCallback(
(e: ChangeEvent<HTMLSelectElement>) => {
const actionName = modelData.find(
(action) => action.ID === parseInt(e.currentTarget.value)
)?.name;
if (actionName && typeof code === "function") {
setProcessedCode(code(actionName));
}
setSelectedAlternative(e.currentTarget.value);
},
[code, modelData]
);
return (
<Highlight
anchor={anchor}
id={name}
active={anchor?.id === slug}
disclosure={disclosure}
>
<Box
fontSize="sm"
p={5}
pr={3}
mt={1}
mb={1}
className="docs-code"
sx={{
...docStyles,
}}
>
<HStack justifyContent="space-between" flexWrap="nowrap">
<DocumentationHeading name={name} isV2Only={false} />
{hasMore && (
<ShowMoreButton
isBrief
onClick={disclosure.onToggle}
isOpen={disclosure.isOpen}
/>
)}
</HStack>
<Stack spacing={3} mt={3}>
<Text noOfLines={disclosure.isOpen ? undefined : 1}>{text}</Text>
{alternativesLabel && (
<Flex wrap="wrap" as="label">
<Text alignSelf="center" mr={2} as="span">
{alternativesLabel}
</Text>
<Select
w="fit-content"
onChange={handleSelectChange}
value={selectedAlternative}
size="sm"
>
{modelData.map((action) => (
<option key={action.ID} value={action.ID}>
{action.name}
</option>
))}
</Select>
</Flex>
)}
<CodeEmbed
code={processedCode}
parentSlug={slug}
toolkitType="model"
/>
</Stack>
{hasMore && (
<Collapse in={disclosure.isOpen}>
<Stack
spacing={3}
pt={detail ? 3 : 0}
noOfLines={!disclosure.isOpen ? 1 : undefined}
>
<Text>{detail}</Text>
</Stack>
</Collapse>
)}
</Box>
</Highlight>
);
};
export default ModelArea;