mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-05-25 16:00:35 +00:00
feat(frontend): add factory reset button with confirmation dialog
Add resetAppConfig API function, AlertDialog-confirmed factory reset button in config page, and i18n keys for en/zh/pt-br locales.
This commit is contained in:
@@ -16,8 +16,8 @@ import (
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal"
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/agent"
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/auth"
|
||||
configcmd "github.com/sipeed/picoclaw/cmd/picoclaw/internal/config"
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/cliui"
|
||||
configcmd "github.com/sipeed/picoclaw/cmd/picoclaw/internal/config"
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/cron"
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/gateway"
|
||||
"github.com/sipeed/picoclaw/cmd/picoclaw/internal/mcp"
|
||||
|
||||
@@ -39,6 +39,7 @@ func TestNewPicoclawCommand(t *testing.T) {
|
||||
allowedCommands := []string{
|
||||
"agent",
|
||||
"auth",
|
||||
"config",
|
||||
"cron",
|
||||
"gateway",
|
||||
"mcp",
|
||||
|
||||
@@ -77,6 +77,12 @@ export async function patchAppConfig(
|
||||
})
|
||||
}
|
||||
|
||||
export async function resetAppConfig(): Promise<ConfigActionResponse> {
|
||||
return request<ConfigActionResponse>("/api/config/reset", {
|
||||
method: "POST",
|
||||
})
|
||||
}
|
||||
|
||||
// WeChat QR login flow API
|
||||
|
||||
export interface WeixinFlowResponse {
|
||||
|
||||
@@ -5,7 +5,7 @@ import { useEffect, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { patchAppConfig } from "@/api/channels"
|
||||
import { patchAppConfig, resetAppConfig } from "@/api/channels"
|
||||
import { launcherFetch } from "@/api/http"
|
||||
import { postLauncherDashboardSetup } from "@/api/launcher-auth"
|
||||
import {
|
||||
@@ -41,6 +41,17 @@ import {
|
||||
} from "@/components/config/form-model"
|
||||
import { PageHeader } from "@/components/page-header"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from "@/components/ui/alert-dialog"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { showSaveSuccessOrRestartToast } from "@/lib/restart-required"
|
||||
import { refreshGatewayState } from "@/store/gateway"
|
||||
@@ -72,15 +83,24 @@ export function ConfigPage() {
|
||||
const [autoStartEnabled, setAutoStartEnabled] = useState(false)
|
||||
const [autoStartBaseline, setAutoStartBaseline] = useState(false)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [showFactoryResetDialog, setShowFactoryResetDialog] = useState(false)
|
||||
|
||||
const { data, isLoading, error } = useQuery({
|
||||
queryKey: ["config"],
|
||||
queryFn: async () => {
|
||||
const res = await launcherFetch("/api/config")
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to load config")
|
||||
const controller = new AbortController()
|
||||
const timer = setTimeout(() => controller.abort(), 5000)
|
||||
try {
|
||||
const res = await launcherFetch("/api/config", {
|
||||
signal: controller.signal,
|
||||
})
|
||||
if (!res.ok) {
|
||||
throw new Error("Failed to load config")
|
||||
}
|
||||
return res.json()
|
||||
} finally {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
return res.json()
|
||||
},
|
||||
})
|
||||
|
||||
@@ -208,6 +228,27 @@ export function ConfigPage() {
|
||||
toast.info(t("pages.config.reset_success"))
|
||||
}
|
||||
|
||||
const handleFactoryReset = async () => {
|
||||
try {
|
||||
await resetAppConfig()
|
||||
const fresh = await launcherFetch("/api/config").then((r) => r.json())
|
||||
const parsed = buildFormFromConfig(fresh)
|
||||
setForm(parsed)
|
||||
setBaseline(parsed)
|
||||
await queryClient.invalidateQueries({ queryKey: ["config"] })
|
||||
await refreshGatewayState()
|
||||
toast.success(t("pages.config.factory_reset_success"))
|
||||
} catch (err) {
|
||||
toast.error(
|
||||
err instanceof Error
|
||||
? err.message
|
||||
: t("pages.config.factory_reset_error"),
|
||||
)
|
||||
} finally {
|
||||
setShowFactoryResetDialog(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSave = async () => {
|
||||
try {
|
||||
setSaving(true)
|
||||
@@ -625,8 +666,38 @@ export function ConfigPage() {
|
||||
}
|
||||
}
|
||||
|
||||
const factoryResetButton = (
|
||||
<AlertDialog
|
||||
open={showFactoryResetDialog}
|
||||
onOpenChange={setShowFactoryResetDialog}
|
||||
>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="destructive" disabled={saving}>
|
||||
{t("pages.config.factory_reset")}
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>
|
||||
{t("pages.config.factory_reset_confirm_title")}
|
||||
</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{t("pages.config.factory_reset_confirm_desc")}
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>{t("common.cancel")}</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={handleFactoryReset}>
|
||||
{t("pages.config.factory_reset_confirm")}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
)
|
||||
|
||||
const actionButtons = (
|
||||
<div className="flex justify-end gap-2">
|
||||
{factoryResetButton}
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={handleReset}
|
||||
@@ -672,8 +743,11 @@ export function ConfigPage() {
|
||||
{t("labels.loading")}
|
||||
</div>
|
||||
) : error ? (
|
||||
<div className="text-destructive py-6 text-sm">
|
||||
{t("pages.config.load_error")}
|
||||
<div className="space-y-4">
|
||||
<div className="text-destructive py-6 text-sm">
|
||||
{t("pages.config.load_error")}
|
||||
</div>
|
||||
<div className="flex justify-end">{factoryResetButton}</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-6">
|
||||
|
||||
@@ -883,7 +883,13 @@
|
||||
"format_success": "JSON formatted successfully.",
|
||||
"format_error": "Invalid JSON format.",
|
||||
"format": "Format",
|
||||
"unsaved_changes": "You have unsaved changes."
|
||||
"unsaved_changes": "You have unsaved changes.",
|
||||
"factory_reset": "Factory Reset",
|
||||
"factory_reset_confirm_title": "Reset to Factory Defaults",
|
||||
"factory_reset_confirm_desc": "This will reset all configuration to factory defaults. API keys and security credentials will be preserved. A backup of the current config will be created.",
|
||||
"factory_reset_confirm": "Reset to Defaults",
|
||||
"factory_reset_success": "Configuration has been reset to factory defaults.",
|
||||
"factory_reset_error": "Failed to reset configuration."
|
||||
},
|
||||
"logs": {
|
||||
"log_level_error": "Failed to update log level.",
|
||||
|
||||
@@ -750,7 +750,13 @@
|
||||
"format_success": "JSON formatado com sucesso.",
|
||||
"format_error": "Formato JSON inválido.",
|
||||
"format": "Formatar",
|
||||
"unsaved_changes": "Você tem alterações não salvas."
|
||||
"unsaved_changes": "Você tem alterações não salvas.",
|
||||
"factory_reset": "Restaurar Padrões",
|
||||
"factory_reset_confirm_title": "Restaurar Configurações de Fábrica",
|
||||
"factory_reset_confirm_desc": "Isso redefinirá todas as configurações para os padrões de fábrica. As chaves de API e credenciais de segurança serão preservadas. Um backup da configuração atual será criado.",
|
||||
"factory_reset_confirm": "Redefinir para Padrões",
|
||||
"factory_reset_success": "A configuração foi redefinida para os padrões de fábrica.",
|
||||
"factory_reset_error": "Falha ao redefinir a configuração."
|
||||
},
|
||||
"logs": {
|
||||
"log_level_error": "Falha ao atualizar nível de log.",
|
||||
|
||||
@@ -884,7 +884,13 @@
|
||||
"format_success": "JSON 格式化成功",
|
||||
"format_error": "JSON 格式无效",
|
||||
"format": "格式化",
|
||||
"unsaved_changes": "您有未保存的更改"
|
||||
"unsaved_changes": "您有未保存的更改",
|
||||
"factory_reset": "恢复出厂设置",
|
||||
"factory_reset_confirm_title": "恢复出厂设置",
|
||||
"factory_reset_confirm_desc": "这将把所有配置重置为出厂默认值。API 密钥和安全凭证将被保留。当前配置将被备份。",
|
||||
"factory_reset_confirm": "确认重置",
|
||||
"factory_reset_success": "配置已重置为出厂默认值。",
|
||||
"factory_reset_error": "重置配置失败。"
|
||||
},
|
||||
"logs": {
|
||||
"log_level_error": "更新日志等级失败。",
|
||||
|
||||
Reference in New Issue
Block a user