import { useState, type ReactNode } from "react"; import { Link, useNavigate, useParams } from "react-router"; import { useSWRConfig } from "swr"; import { ChevronRightIcon } from "@heroicons/react/20/solid"; import { ApiError, apiData, variablesApi } from "../lib/api-client"; import { queryKeys } from "../lib/query-keys"; import { Panel, PanelSkeleton, Row } from "../components/settings-panel"; import { ErrorMessage, INPUT_CLASS, PRIMARY_BUTTON_CLASS, SECONDARY_BUTTON_CLASS, } from "../components/ui"; import { useToast } from "../components/toast"; import { useVariable } from "../lib/queries"; export function meta() { return [{ title: "Edit variable — Fabro" }]; } export default function SettingsVariablesEdit() { const { name } = useParams<{ name: string }>(); const query = useVariable(name); return (
{query.data ? ( ) : query.error ? (
Couldn't load this variable. It may have been deleted.
) : ( )}
); } function PageHeader({ name }: { name: string }) { return ( ); } function EditVariableForm({ name, initialValue, initialDescription, }: { name: string; initialValue: string; initialDescription: string; }) { const navigate = useNavigate(); const { mutate } = useSWRConfig(); const toast = useToast(); const [value, setValue] = useState(initialValue); const [description, setDescription] = useState(initialDescription); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const dirty = value !== initialValue || description !== initialDescription; const canSubmit = dirty && !submitting; async function onSubmit(event: React.FormEvent) { event.preventDefault(); if (!canSubmit) return; setSubmitting(true); setError(null); try { await apiData(() => variablesApi.updateVariable(name, { value, description: description.trim() || undefined, }), ); await mutate(queryKeys.variables.list()); await mutate(queryKeys.variables.detail(name)); toast.push({ message: `Variable “${name}” updated.` }); navigate("/settings/variables"); } catch (cause) { setError( cause instanceof ApiError && cause.message ? cause.message : "Couldn't update the variable. Please try again.", ); setSubmitting(false); } } return (
Name} help="The variable name cannot be changed. Delete and recreate to rename.">
{name}
Value} help="Stored as-is. Empty values are allowed." >