Ensure restart dialog isn't shown if config isn't saved successfully first (#17132)

This commit is contained in:
Josh Hawkins 2025-03-13 09:57:12 -05:00 committed by GitHub
parent b8f2d8fb0c
commit c93b82d6e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,7 @@ import { useApiHost } from "@/api";
import Heading from "@/components/ui/heading"; import Heading from "@/components/ui/heading";
import ActivityIndicator from "@/components/indicators/activity-indicator"; import ActivityIndicator from "@/components/indicators/activity-indicator";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import axios from "axios"; import axios, { AxiosError } from "axios";
import copy from "copy-to-clipboard"; import copy from "copy-to-clipboard";
import { useTheme } from "@/context/theme-provider"; import { useTheme } from "@/context/theme-provider";
import { Toaster } from "@/components/ui/sonner"; import { Toaster } from "@/components/ui/sonner";
@ -14,9 +14,15 @@ import { toast } from "sonner";
import { LuCopy, LuSave } from "react-icons/lu"; import { LuCopy, LuSave } from "react-icons/lu";
import { MdOutlineRestartAlt } from "react-icons/md"; import { MdOutlineRestartAlt } from "react-icons/md";
import RestartDialog from "@/components/overlay/dialog/RestartDialog"; import RestartDialog from "@/components/overlay/dialog/RestartDialog";
import { useRestart } from "@/api/ws";
type SaveOptions = "saveonly" | "restart"; type SaveOptions = "saveonly" | "restart";
type ApiErrorResponse = {
message?: string;
detail?: string;
};
function ConfigEditor() { function ConfigEditor() {
const apiHost = useApiHost(); const apiHost = useApiHost();
@ -35,37 +41,40 @@ function ConfigEditor() {
const schemaConfiguredRef = useRef(false); const schemaConfiguredRef = useRef(false);
const [restartDialogOpen, setRestartDialogOpen] = useState(false); const [restartDialogOpen, setRestartDialogOpen] = useState(false);
const { send: sendRestart } = useRestart();
const onHandleSaveConfig = useCallback( const onHandleSaveConfig = useCallback(
async (save_option: SaveOptions) => { async (save_option: SaveOptions): Promise<void> => {
if (!editorRef.current) { if (!editorRef.current) {
return; return;
} }
axios try {
.post( const response = await axios.post(
`config/save?save_option=${save_option}`, `config/save?save_option=${save_option}`,
editorRef.current.getValue(), editorRef.current.getValue(),
{ {
headers: { "Content-Type": "text/plain" }, headers: { "Content-Type": "text/plain" },
}, },
) );
.then((response) => {
if (response.status === 200) {
setError("");
toast.success(response.data.message, { position: "top-center" });
}
})
.catch((error) => {
toast.error("Error saving config", { position: "top-center" });
const errorMessage = if (response.status === 200) {
error.response?.data?.message || setError("");
error.response?.data?.detail || setHasChanges(false);
"Unknown error"; toast.success(response.data.message, { position: "top-center" });
}
} catch (error) {
toast.error("Error saving config", { position: "top-center" });
setError(errorMessage); const axiosError = error as AxiosError<ApiErrorResponse>;
}); const errorMessage =
axiosError.response?.data?.message ||
axiosError.response?.data?.detail ||
"Unknown error";
setError(errorMessage);
throw new Error(errorMessage);
}
}, },
[editorRef], [editorRef],
); );
@ -79,6 +88,15 @@ function ConfigEditor() {
toast.success("Config copied to clipboard.", { position: "top-center" }); toast.success("Config copied to clipboard.", { position: "top-center" });
}, [editorRef]); }, [editorRef]);
const handleSaveAndRestart = useCallback(async () => {
try {
await onHandleSaveConfig("saveonly");
setRestartDialogOpen(true);
} catch (error) {
// If save fails, error is already set in onHandleSaveConfig, no dialog opens
}
}, [onHandleSaveConfig]);
useEffect(() => { useEffect(() => {
if (!config) { if (!config) {
return; return;
@ -206,7 +224,7 @@ function ConfigEditor() {
size="sm" size="sm"
className="flex items-center gap-2" className="flex items-center gap-2"
aria-label="Save and restart" aria-label="Save and restart"
onClick={() => setRestartDialogOpen(true)} onClick={handleSaveAndRestart}
> >
<div className="relative size-5"> <div className="relative size-5">
<LuSave className="absolute left-0 top-0 size-3 text-secondary-foreground" /> <LuSave className="absolute left-0 top-0 size-3 text-secondary-foreground" />
@ -238,7 +256,7 @@ function ConfigEditor() {
<RestartDialog <RestartDialog
isOpen={restartDialogOpen} isOpen={restartDialogOpen}
onClose={() => setRestartDialogOpen(false)} onClose={() => setRestartDialogOpen(false)}
onRestart={() => onHandleSaveConfig("restart")} onRestart={() => sendRestart("restart")}
/> />
</div> </div>
); );