import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { useState } from "react" import { useTranslation } from "react-i18next" import { toast } from "sonner" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card" import { ScrollArea } from "@/components/ui/scroll-area" import { Textarea } from "@/components/ui/textarea" export function RawJsonPanel() { const { t } = useTranslation() const queryClient = useQueryClient() const { data: config, isLoading } = useQuery({ queryKey: ["config"], queryFn: async () => { const res = await fetch("/api/config") if (!res.ok) { throw new Error("Failed to fetch config") } return res.json() }, }) const mutation = useMutation({ mutationFn: async (newConfig: string) => { const res = await fetch("/api/config", { method: "PUT", headers: { "Content-Type": "application/json" }, body: newConfig, }) if (!res.ok) { throw new Error("Failed to save config") } }, onSuccess: (_, submittedConfig) => { toast.success(t("pages.config.save_success")) try { const savedConfig = JSON.parse(submittedConfig) setLastSavedConfig(savedConfig) setIsDirty(false) queryClient.invalidateQueries({ queryKey: ["config"] }) } catch { queryClient.invalidateQueries({ queryKey: ["config"] }) } }, onError: () => { toast.error(t("pages.config.save_error")) }, }) const [editorValue, setEditorValue] = useState("") const [isDirty, setIsDirty] = useState(false) const [lastSavedConfig, setLastSavedConfig] = useState | null>(null) const effectiveEditorValue = editorValue || (config ? JSON.stringify(config, null, 2) : "") const handleSave = () => { try { JSON.parse(effectiveEditorValue) mutation.mutate(effectiveEditorValue) } catch (error) { toast.error( t( "pages.config.invalid_json", error instanceof Error ? error.message : "Invalid JSON format.", ), ) } } const handleFormat = () => { try { const formatted = JSON.stringify( JSON.parse(effectiveEditorValue), null, 2, ) setEditorValue(formatted) toast.success(t("pages.config.format_success")) } catch (error) { toast.error( t( "pages.config.format_error", error instanceof Error ? error.message : "Invalid JSON format.", ), ) } } const [showResetDialog, setShowResetDialog] = useState(false) const confirmReset = () => { if (lastSavedConfig) { setEditorValue(JSON.stringify(lastSavedConfig, null, 2)) } else if (config) { setEditorValue(JSON.stringify(config, null, 2)) } setIsDirty(false) toast.info(t("pages.config.reset_success")) setShowResetDialog(false) } return ( {t("pages.config.raw_json_title")} {t("pages.config.raw_json_desc")} {isLoading ? (

{t("labels.loading")}

) : (
{isDirty && (
{t("pages.config.unsaved_changes")}
)}