fix(web): avoid resetting web search draft on config refetch (#2536)

This commit is contained in:
wenjie 2026-04-16 10:26:18 +08:00 committed by GitHub
parent f1b659e5ef
commit f32b303d2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,15 +1,15 @@
import { IconSearch } from "@tabler/icons-react" import { IconSearch } from "@tabler/icons-react"
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { useEffect, useMemo, useState } from "react" import { useMemo, useState } from "react"
import { useTranslation } from "react-i18next" import { useTranslation } from "react-i18next"
import { toast } from "sonner" import { toast } from "sonner"
import { import {
type ToolSupportItem,
type WebSearchConfigResponse,
getTools, getTools,
getWebSearchConfig, getWebSearchConfig,
setToolEnabled, setToolEnabled,
type ToolSupportItem,
type WebSearchConfigResponse,
updateWebSearchConfig, updateWebSearchConfig,
} from "@/api/tools" } from "@/api/tools"
import { PageHeader } from "@/components/page-header" import { PageHeader } from "@/components/page-header"
@ -54,14 +54,9 @@ export function ToolsPage() {
const [searchQuery, setSearchQuery] = useState("") const [searchQuery, setSearchQuery] = useState("")
const [statusFilter, setStatusFilter] = useState("all") const [statusFilter, setStatusFilter] = useState("all")
const [webSearchDraft, setWebSearchDraft] = const [webSearchDraftOverride, setWebSearchDraftOverride] =
useState<WebSearchConfigResponse | null>(null) useState<WebSearchConfigResponse | null>(null)
const webSearchDraft = webSearchDraftOverride ?? webSearchData ?? null
useEffect(() => {
if (webSearchData) {
setWebSearchDraft(webSearchData)
}
}, [webSearchData])
const toggleMutation = useMutation({ const toggleMutation = useMutation({
mutationFn: async ({ name, enabled }: { name: string; enabled: boolean }) => mutationFn: async ({ name, enabled }: { name: string; enabled: boolean }) =>
@ -87,9 +82,12 @@ export function ToolsPage() {
const webSearchMutation = useMutation({ const webSearchMutation = useMutation({
mutationFn: updateWebSearchConfig, mutationFn: updateWebSearchConfig,
onSuccess: (updated) => { onSuccess: (updated) => {
setWebSearchDraft(updated) queryClient.setQueryData(["tools", "web-search-config"], updated)
setWebSearchDraftOverride(null)
toast.success(t("pages.agent.tools.web_search.save_success")) toast.success(t("pages.agent.tools.web_search.save_success"))
void queryClient.invalidateQueries({ queryKey: ["tools", "web-search-config"] }) void queryClient.invalidateQueries({
queryKey: ["tools", "web-search-config"],
})
void queryClient.invalidateQueries({ queryKey: ["tools"] }) void queryClient.invalidateQueries({ queryKey: ["tools"] })
void refreshGatewayState({ force: true }) void refreshGatewayState({ force: true })
}, },
@ -148,7 +146,10 @@ export function ToolsPage() {
const updateDraft = ( const updateDraft = (
updater: (current: WebSearchConfigResponse) => WebSearchConfigResponse, updater: (current: WebSearchConfigResponse) => WebSearchConfigResponse,
) => { ) => {
setWebSearchDraft((current) => (current ? updater(current) : current)) setWebSearchDraftOverride((current) => {
const draft = current ?? webSearchData
return draft ? updater(draft) : current
})
} }
return ( return (
@ -161,7 +162,9 @@ export function ToolsPage() {
<Card className="border-destructive/50 bg-destructive/10 cursor-default"> <Card className="border-destructive/50 bg-destructive/10 cursor-default">
<CardHeader> <CardHeader>
<CardTitle>{t("pages.agent.tools.web_search.title")}</CardTitle> <CardTitle>{t("pages.agent.tools.web_search.title")}</CardTitle>
<CardDescription>{t("pages.agent.tools.web_search.load_error")}</CardDescription> <CardDescription>
{t("pages.agent.tools.web_search.load_error")}
</CardDescription>
</CardHeader> </CardHeader>
</Card> </Card>
) : isWebSearchLoading || !webSearchDraft ? ( ) : isWebSearchLoading || !webSearchDraft ? (
@ -201,7 +204,10 @@ export function ToolsPage() {
<Select <Select
value={webSearchDraft.provider} value={webSearchDraft.provider}
onValueChange={(value) => onValueChange={(value) =>
updateDraft((current) => ({ ...current, provider: value })) updateDraft((current) => ({
...current,
provider: value,
}))
} }
> >
<SelectTrigger> <SelectTrigger>
@ -254,21 +260,30 @@ export function ToolsPage() {
</div> </div>
<div className="grid gap-4 lg:grid-cols-2"> <div className="grid gap-4 lg:grid-cols-2">
{Object.entries(webSearchDraft.settings).map(([providerId, settings]) => { {Object.entries(webSearchDraft.settings).map(
const providerLabel = providerLabelMap.get(providerId) ?? providerId ([providerId, settings]) => {
const providerLabel =
providerLabelMap.get(providerId) ?? providerId
const apiKeyPlaceholder = maskedSecretPlaceholder( const apiKeyPlaceholder = maskedSecretPlaceholder(
settings.api_key_set ? `${providerId}-configured` : "", settings.api_key_set ? `${providerId}-configured` : "",
t("pages.agent.tools.web_search.api_key_placeholder"), t("pages.agent.tools.web_search.api_key_placeholder"),
) )
return ( return (
<Card key={providerId} className="border-border/60 shadow-none"> <Card
key={providerId}
className="border-border/60 shadow-none"
>
<CardHeader className="pb-3"> <CardHeader className="pb-3">
<div className="flex items-center justify-between gap-3"> <div className="flex items-center justify-between gap-3">
<div> <div>
<CardTitle className="text-base">{providerLabel}</CardTitle> <CardTitle className="text-base">
{providerLabel}
</CardTitle>
<CardDescription className="mt-1 text-xs"> <CardDescription className="mt-1 text-xs">
{t("pages.agent.tools.web_search.provider_hint")} {t(
"pages.agent.tools.web_search.provider_hint",
)}
</CardDescription> </CardDescription>
</div> </div>
<Switch <Switch
@ -305,7 +320,8 @@ export function ToolsPage() {
...current.settings, ...current.settings,
[providerId]: { [providerId]: {
...current.settings[providerId], ...current.settings[providerId],
max_results: Number(e.target.value) || 0, max_results:
Number(e.target.value) || 0,
}, },
}, },
})) }))
@ -334,7 +350,9 @@ export function ToolsPage() {
}, },
})) }))
} }
placeholder={t("pages.agent.tools.web_search.base_url_placeholder")} placeholder={t(
"pages.agent.tools.web_search.base_url_placeholder",
)}
/> />
</div> </div>
)} )}
@ -368,7 +386,8 @@ export function ToolsPage() {
</CardContent> </CardContent>
</Card> </Card>
) )
})} },
)}
</div> </div>
<div className="flex justify-end"> <div className="flex justify-end">