Download web/src/apps/reports/ReportSettings.tsx from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 3.98 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/apps/reports/ReportSettings.tsx
- Command line
-
hf download hf://spaces/fsanyoto/loopable/web/src/apps/reports/ReportSettings.tsx
-
curl -L -o ReportSettings.tsx https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/apps/reports/ReportSettings.tsx
3.98 kB
| import { useEffect, useRef, useState } from "react"; | |
| import { BodyPortal, useOverlayLayer } from "../../customer-grid/seam"; | |
| import { fetchReportSettings, reportErrorMessage, saveReportSettings } from "./reportsApi"; | |
| import type { Report, ReportSettingsData } from "./reportsApi"; | |
| type SettingsState = { status: "loading" } | { status: "error"; message: string } | |
| | { status: "ready"; data: ReportSettingsData; saving: boolean; error: string }; | |
| function SettingsFields({ data, disabled, onChange }: { | |
| data: ReportSettingsData; | |
| disabled: boolean; | |
| onChange: (key: string, value: string) => void; | |
| }) { | |
| return <>{data.fields.map(field => <div className="set-field" key={field.key}> | |
| <label className="set-label"> | |
| {field.label} | |
| <input className="set-input" name={field.key} type={field.kind} value={data.values[field.key]} | |
| disabled={disabled} onChange={event => onChange(field.key, event.target.value)} /> | |
| </label> | |
| </div>)}</>; | |
| } | |
| export default function ReportSettings({ report, onClose }: { report: Report; onClose: () => void }) { | |
| const [state, setState] = useState<SettingsState>({ status: "loading" }); | |
| const [revision, setRevision] = useState(0); | |
| const panel = useRef<HTMLElement>(null); | |
| const requests = useRef<AbortController | null>(null); | |
| useOverlayLayer({ panelRef: panel, onDismiss: onClose, dismissOnOutside: false, restoreFocus: true }); | |
| useEffect(() => { | |
| const controller = new AbortController(); | |
| requests.current = controller; | |
| setState({ status: "loading" }); | |
| void fetchReportSettings(report.key, controller.signal).then(data => { | |
| if (!controller.signal.aborted) setState({ status: "ready", data, saving: false, error: "" }); | |
| }).catch(failure => { | |
| if (!controller.signal.aborted) setState({ status: "error", message: reportErrorMessage(failure) }); | |
| }); | |
| return () => controller.abort(); | |
| }, [report.key, revision]); | |
| async function save(): Promise<void> { | |
| const controller = requests.current; | |
| if (state.status !== "ready" || state.saving || !controller || controller.signal.aborted) return; | |
| setState({ ...state, saving: true, error: "" }); | |
| try { | |
| await saveReportSettings(report.key, state.data.values, controller.signal); | |
| if (!controller.signal.aborted) onClose(); | |
| } catch (failure) { | |
| if (!controller.signal.aborted) setState({ ...state, saving: false, error: reportErrorMessage(failure) }); | |
| } | |
| } | |
| function change(key: string, value: string): void { | |
| setState(current => current.status === "ready" ? { ...current, | |
| data: { ...current.data, values: { ...current.data.values, [key]: value } } } : current); | |
| } | |
| return <BodyPortal><aside ref={panel} className="cg-activity-drawer set-pane" role="dialog" | |
| aria-label={`${report.name} settings`} tabIndex={-1}> | |
| <header className="cg-activity-heading set-field"> | |
| <h2>Settings</h2> | |
| <button className="set-secondary" type="button" onClick={onClose}>Close</button> | |
| </header> | |
| {state.status === "loading" ? <div className="set-skeleton" role="status" aria-label="Loading settings" aria-busy="true"> | |
| <span className="set-skeleton-line" aria-hidden="true" /> | |
| <span className="set-skeleton-line" aria-hidden="true" /> | |
| <span className="set-skeleton-line" aria-hidden="true" /> | |
| </div> : state.status === "error" ? <div className="set-error" role="alert"> | |
| <span>{state.message}</span> | |
| <button className="set-secondary" type="button" onClick={() => setRevision(value => value + 1)}>Retry</button> | |
| </div> : <form onSubmit={event => { event.preventDefault(); void save(); }}> | |
| <SettingsFields data={state.data} disabled={state.saving} onChange={change} /> | |
| {state.error ? <p className="set-error" role="alert">{state.error}</p> : null} | |
| <div className="set-actions"> | |
| <button className="set-primary" type="submit" disabled={state.saving}>Save</button> | |
| </div> | |
| </form>} | |
| </aside></BodyPortal>; | |
| } | |