Spaces:
Running
Running
File size: 7,170 Bytes
fba4209 b225f7d fba4209 b225f7d fba4209 6c68d01 fba4209 6c68d01 fba4209 b225f7d fba4209 6c68d01 fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d 6c68d01 fba4209 b225f7d 6c68d01 b225f7d fba4209 b225f7d 6c68d01 fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 6c68d01 fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d 6c68d01 b225f7d fba4209 b225f7d 6c68d01 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 b225f7d fba4209 | 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 | import React, { useEffect, useState } from "react";
import { apiService } from "../services/api.service";
import { Check, Save, Loader } from "lucide-react";
const Config = () => {
const [config, setConfig] = useState(null);
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
const [saveSuccess, setSaveSuccess] = useState(false);
const editableKeys = [
"pick",
"use_agg",
"put_back",
"drop_object",
"vel_mul",
"range_",
"save_point_clouds",
"save_to_db",
"same_object",
"same_place_index",
];
// Descriptions are always visible instead of in a tooltip, so they're also readable on touch
const descriptions = {
"pick": "Enables the part-picking function",
"use_agg": "Uses the matplotlib Agg backend to avoid opening a chart window",
"put_back": "Puts the part back where it was picked from",
"drop_object": "Enables dropping the object in the scene",
"vel_mul": "Motion speed multiplier",
"range_": "Loop range",
"save_point_clouds": "Saves point clouds to disk",
"save_to_db": "Saves results to the database",
"same_object": "Treats it as the same object",
"same_place_index": "The index the part will be picked from each time (enter -1 to disable this feature)"
};
useEffect(() => {
const fetchConfig = async () => {
try {
const data = await apiService.GetConfig();
setConfig(data);
} catch (error) {
console.error("Config loading failed:", error);
} finally {
setIsLoading(false);
}
};
fetchConfig();
}, []);
const handleChange = (key, value) => {
setConfig((prev) => ({
...prev,
[key]: value,
}));
setSaveSuccess(false);
};
const handleSubmit = async () => {
setIsSaving(true);
try {
await apiService.SetConfig(config);
setSaveSuccess(true);
// Reset success message after 3 seconds
setTimeout(() => {
setSaveSuccess(false);
}, 3000);
} catch (error) {
console.error("Config update failed:", error);
alert("Could not save. Please try again.");
} finally {
setIsSaving(false);
}
};
const formatLabel = (key) => {
return key
.replace(/_/g, ' ')
.trim()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};
if (isLoading) {
return (
<div className="flex h-64 items-center justify-center border border-hmi-line bg-hmi-panel">
<div className="text-center">
<Loader className="mx-auto h-8 w-8 animate-spin text-hmi-accent" />
<div className="mt-2 text-sm uppercase tracking-[0.12em] text-hmi-muted">Loading configuration</div>
</div>
</div>
);
}
if (!config) {
return (
<div className="border border-hmi-err bg-hmi-panel px-4 py-4 text-hmi-err">
Could not retrieve configuration.
</div>
);
}
return (
<section className="border border-hmi-line bg-hmi-panel">
<div className="flex items-center justify-between gap-4 border-b border-hmi-line px-4 py-3">
<span className="text-sm font-semibold uppercase tracking-[0.14em]">Configuration</span>
<button
type="button"
onClick={handleSubmit}
disabled={isSaving}
className={`flex min-h-[44px] items-center gap-2 border-2 px-4 text-sm font-semibold uppercase tracking-[0.08em] transition active:translate-y-px disabled:cursor-not-allowed disabled:opacity-50 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-hmi-accent ${
saveSuccess
? "border-hmi-ok bg-hmi-ok text-hmi-bg"
: "border-hmi-accent bg-hmi-accent text-hmi-bg [@media(hover:hover)]:hover:brightness-110"
}`}
>
{isSaving ? (
<Loader className="h-4 w-4 animate-spin" />
) : saveSuccess ? (
<Check className="h-4 w-4" />
) : (
<Save className="h-4 w-4" />
)}
{saveSuccess ? "Saved" : isSaving ? "Saving" : "Save changes"}
</button>
</div>
<div className="grid grid-cols-1 gap-px bg-hmi-line md:grid-cols-2">
{editableKeys.map((key) => {
const value = config[key];
const isBoolean = typeof value === "boolean";
const isNumber = typeof value === "number";
const isString = typeof value === "string";
const inputId = `config-${key}`;
return (
<div key={key} className="flex flex-col gap-2 bg-hmi-panel px-4 py-3">
<div className="flex items-start justify-between gap-4">
<div className="min-w-0">
<label htmlFor={inputId} className="block font-mono text-sm font-semibold text-hmi-text">
{formatLabel(key)}
</label>
<div className="mt-0.5 text-xs leading-snug text-hmi-muted">
{descriptions[key] || "Configuration setting"}
</div>
</div>
{isBoolean && (
<button
id={inputId}
type="button"
role="switch"
aria-checked={value}
onClick={() => handleChange(key, !value)}
className={`relative h-9 w-16 shrink-0 border-2 transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-hmi-accent ${
value ? "border-hmi-accent bg-hmi-accent/20" : "border-hmi-line bg-hmi-bg"
}`}
>
<span
className={`absolute top-1 h-5 w-5 transition-all ${
value ? "left-[calc(100%-1.5rem)] bg-hmi-accent" : "left-1 bg-hmi-muted"
}`}
aria-hidden="true"
/>
<span className="sr-only">{value ? "On" : "Off"}</span>
</button>
)}
</div>
{isNumber && (
<input
id={inputId}
type="number"
value={value}
step={key === "vel_mul" ? 0.1 : 1}
onChange={(e) => handleChange(key, Number(e.target.value))}
className="min-h-[44px] w-full border border-hmi-line bg-hmi-bg px-3 font-mono text-hmi-text focus:border-hmi-accent focus:outline-none"
/>
)}
{isString && (
<input
id={inputId}
type="text"
value={value}
onChange={(e) => handleChange(key, e.target.value)}
className="min-h-[44px] w-full border border-hmi-line bg-hmi-bg px-3 font-mono text-hmi-text focus:border-hmi-accent focus:outline-none"
/>
)}
</div>
);
})}
</div>
</section>
);
};
export default Config;
|