Spaces:
Running
Running
Download frontend/src/components/Config.js from xenecode/Jaguar_Project: direct link, hf CLI and curl.
- Browser
- Download file 7.17 kB
-
https://huggingface.co/spaces/xenecode/Jaguar_Project/resolve/main/frontend/src/components/Config.js
- Command line
-
hf download hf://spaces/xenecode/Jaguar_Project/frontend/src/components/Config.js
-
curl -L -o Config.js https://huggingface.co/spaces/xenecode/Jaguar_Project/resolve/main/frontend/src/components/Config.js
7.17 kB
| 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; | |