-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConfigurator.js
More file actions
157 lines (141 loc) · 4.36 KB
/
Configurator.js
File metadata and controls
157 lines (141 loc) · 4.36 KB
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
// global localStorage
import React, { useContext, useEffect, useMemo, useState } from "react";
import { Alert, Button, message, Steps, theme } from "antd";
import YamlFileUploader from "./YamlFileUploader";
import YamlFileEditor from "./YamlFileEditor";
import InputSelector from "./InputSelector";
import { AppContext } from "../contexts/GlobalContext";
function Configurator(props) {
const { fileList, type } = props;
const context = useContext(AppContext);
const [current, setCurrent] = useState(0);
const [hasAttemptedAdvance, setHasAttemptedAdvance] = useState(false);
const storageKey = `configStep:${type}`;
const next = () => {
if (missingByStep.length > 0) {
setHasAttemptedAdvance(true);
return;
}
setCurrent(current + 1);
setHasAttemptedAdvance(false);
};
const prev = () => {
setCurrent(current - 1);
};
const handleDoneButton = () => {
if (missingByStep.length > 0) {
setHasAttemptedAdvance(true);
return;
}
const label = type === "training" ? "Training" : "Inference";
message.success(`${label} configuration saved.`);
if (type === "training") {
localStorage.setItem("trainingConfig", context.trainingConfig);
} else {
localStorage.setItem("inferenceConfig", context.inferenceConfig);
}
};
const getPathValue = (val) => {
if (!val) return "";
if (typeof val === "string") return val;
return val.path || val.folderPath || "";
};
const missingInputs = useMemo(() => {
const missing = [];
if (!getPathValue(context.inputImage)) missing.push("input image");
if (!getPathValue(context.inputLabel)) missing.push("input label");
if (!getPathValue(context.outputPath)) missing.push("output path");
if (type === "inference" && !getPathValue(context.checkpointPath)) {
missing.push("checkpoint path");
}
return missing;
}, [
context.inputImage,
context.inputLabel,
context.outputPath,
context.checkpointPath,
type,
]);
const hasConfig =
type === "training"
? Boolean(context.trainingConfig)
: Boolean(context.inferenceConfig);
const missingBase = useMemo(
() => (hasConfig ? [] : ["base configuration (preset or upload)"]),
[hasConfig],
);
const missingByStep = useMemo(() => {
if (current === 0) return missingInputs;
if (current === 1) return missingBase;
if (current === 2) return missingBase;
return [];
}, [current, missingInputs, missingBase]);
useEffect(() => {
const stored = localStorage.getItem(storageKey);
if (stored !== null) {
const parsed = Number(stored);
if (!Number.isNaN(parsed) && parsed >= 0 && parsed <= 2) {
setCurrent(parsed);
}
}
}, [storageKey]);
useEffect(() => {
localStorage.setItem(storageKey, String(current));
}, [current, storageKey]);
const items = [
{
title: "Set Inputs",
content: <InputSelector fileList={fileList} type={type} />,
},
{
title: "Base Configuration",
content: <YamlFileUploader type={type} />,
},
{
title: "Advanced Configuration",
content: <YamlFileEditor type={type} />,
},
];
const { token } = theme.useToken();
const contentStyle = {
minHeight: "260px",
textAlign: "left",
color: token.colorTextTertiary,
backgroundColor: token.colorFillAlter,
borderRadius: token.borderRadiusLG,
border: `1px dashed ${token.colorBorder}`,
marginTop: 12,
};
return (
<div style={{ marginTop: 12 }}>
<Steps size="small" current={current} items={items} />
<div style={contentStyle}>{items[current].content}</div>
<div style={{ marginTop: 24 }}>
{current > 0 && (
<Button style={{ marginRight: "8px" }} onClick={() => prev()}>
Previous
</Button>
)}
{current < items.length - 1 && (
<Button type="primary" onClick={() => next()}>
Next
</Button>
)}
{current === items.length - 1 && (
<Button type="primary" onClick={handleDoneButton}>
Done
</Button>
)}
</div>
{hasAttemptedAdvance && missingByStep.length > 0 && (
<Alert
style={{ marginTop: 12 }}
type="warning"
showIcon
message={`Before you continue, add: ${missingByStep.join(", ")}`}
/>
)}
</div>
);
}
export default Configurator;