refactor(settings): use key prop to reset LLM role manager form state

Fixes #1018

Remove the sync useEffect that copied preferences into local state,
along with the savingRef guard that prevented mid-save overwrites.
Instead, pass key={searchSpaceId} on the LLMRoleManager component so
React remounts the form with correct initial state whenever the search
space changes — no extra re-render, no effect dependency array.

Changes:
- llm-role-manager.tsx: remove useEffect + useRef + savingRef pattern;
  drop useEffect and useRef from imports (now only useCallback, useState)
- search-space-settings-dialog.tsx: add key={searchSpaceId} to
  <LLMRoleManager> so the component remounts on search-space change

Before: useEffect synced preferences → assignments on each preference
  update, with savingRef to avoid overwriting an in-flight save.
After:  React remounts the component with correct initial state from
  the preferences selector; no mid-save race possible.
This commit is contained in:
guangyang1206 2026-04-29 12:14:08 +08:00
parent 61f4d05cd1
commit 345cb88224
2 changed files with 2 additions and 21 deletions

View file

@ -11,7 +11,7 @@ import {
RefreshCw,
ScanEye,
} from "lucide-react";
import { useCallback, useEffect, useRef, useState } from "react";
import { useCallback, useState } from "react";
import { toast } from "sonner";
import {
globalImageGenConfigsAtom,
@ -143,23 +143,6 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
}));
const [savingRole, setSavingRole] = useState<string | null>(null);
const savingRef = useRef(false);
useEffect(() => {
if (!savingRef.current) {
setAssignments({
agent_llm_id: preferences.agent_llm_id ?? "",
document_summary_llm_id: preferences.document_summary_llm_id ?? "",
image_generation_config_id: preferences.image_generation_config_id ?? "",
vision_llm_config_id: preferences.vision_llm_config_id ?? "",
});
}
}, [
preferences?.agent_llm_id,
preferences?.document_summary_llm_id,
preferences?.image_generation_config_id,
preferences?.vision_llm_config_id,
]);
const handleRoleAssignment = useCallback(
async (prefKey: string, configId: string) => {
@ -167,7 +150,6 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
setAssignments((prev) => ({ ...prev, [prefKey]: value }));
setSavingRole(prefKey);
savingRef.current = true;
try {
await updatePreferences({
@ -177,7 +159,6 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
toast.success("Role assignment updated");
} finally {
setSavingRole(null);
savingRef.current = false;
}
},
[updatePreferences, searchSpaceId]

View file

@ -116,7 +116,7 @@ export function SearchSpaceSettingsDialog({ searchSpaceId }: SearchSpaceSettings
const content: Record<string, React.ReactNode> = {
general: <GeneralSettingsManager searchSpaceId={searchSpaceId} />,
models: <AgentModelManager searchSpaceId={searchSpaceId} />,
roles: <LLMRoleManager searchSpaceId={searchSpaceId} />,
roles: <LLMRoleManager key={searchSpaceId} searchSpaceId={searchSpaceId} />,
"image-models": <ImageModelManager searchSpaceId={searchSpaceId} />,
"vision-models": <VisionModelManager searchSpaceId={searchSpaceId} />,
"team-roles": <RolesManager searchSpaceId={searchSpaceId} />,