refactor(api): migrate config-api to ApiResponse pattern

This commit is contained in:
DrSmoothl
2026-03-01 17:43:47 +08:00
parent 88e157040f
commit d4bfc9591c
9 changed files with 288 additions and 156 deletions

View File

@@ -262,7 +262,16 @@ function BotConfigPageContent() {
// 加载源代码
const loadSourceCode = useCallback(async () => {
try {
const raw = await getBotConfigRaw()
const result = await getBotConfigRaw()
if (!result.success) {
toast({
variant: 'destructive',
title: '加载失败',
description: result.error,
})
return
}
const raw = result.data
// 将 TOML 基本字符串中的转义序列转换为实际字符以便在编辑器中正确显示
// 使用正则表达式只处理双引号字符串内的转义序列,不影响单引号字符串
const unescaped = raw.replace(/"([^"]*)"/g, (_match, content) => {
@@ -289,8 +298,17 @@ function BotConfigPageContent() {
const loadConfig = useCallback(async () => {
try {
setLoading(true)
const config = await getBotConfig()
parseAndSetConfig(config)
const result = await getBotConfig()
if (!result.success) {
toast({
title: '加载失败',
description: result.error,
variant: 'destructive',
})
setLoading(false)
return
}
parseAndSetConfig(result.data)
setHasUnsavedChanges(false)
initialLoadRef.current = false
@@ -382,7 +400,18 @@ function BotConfigPageContent() {
.replace(/\r/g, '\\r') // 回车符
return `"${encoded}"`
})
await updateBotConfigRaw(escaped)
const result = await updateBotConfigRaw(escaped)
if (!result.success) {
setHasTomlError(true)
const errorMsg = result.error
setTomlErrorMessage(errorMsg)
toast({
variant: 'destructive',
title: '保存失败',
description: errorMsg,
})
return
}
setHasUnsavedChanges(false)
setHasTomlError(false)
setTomlErrorMessage('')
@@ -423,8 +452,16 @@ function BotConfigPageContent() {
} else {
// 切换回可视化时,直接重新加载配置但不显示全局 loading
try {
const config = await getBotConfig()
parseAndSetConfig(config)
const result = await getBotConfig()
if (!result.success) {
toast({
title: '加载失败',
description: result.error,
variant: 'destructive',
})
return
}
parseAndSetConfig(result.data)
setHasUnsavedChanges(false)
} catch (error) {
console.error('加载配置失败:', error)
@@ -444,7 +481,16 @@ function BotConfigPageContent() {
// 取消待处理的自动保存
cancelPendingAutoSave()
await updateBotConfig(buildFullConfig())
const result = await updateBotConfig(buildFullConfig())
if (!result.success) {
toast({
title: '保存失败',
description: result.error,
variant: 'destructive',
})
setSaving(false)
return
}
setHasUnsavedChanges(false)
toast({
title: '保存成功',
@@ -474,7 +520,16 @@ function BotConfigPageContent() {
// 取消待处理的自动保存
cancelPendingAutoSave()
await updateBotConfig(buildFullConfig())
const result = await updateBotConfig(buildFullConfig())
if (!result.success) {
toast({
title: '保存失败',
description: result.error,
variant: 'destructive',
})
setSaving(false)
return
}
setHasUnsavedChanges(false)
toast({
title: '保存成功',
@@ -759,4 +814,4 @@ function BotConfigPageContent() {
</div>
</ScrollArea>
)
}
}