mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-02 22:01:05 +02:00
refactor: simplify hasChanges tracking in GeneralSettingsManager and PromptConfigManager components
This commit is contained in:
parent
05030f6664
commit
3724a1530c
2 changed files with 5 additions and 23 deletions
|
|
@ -40,26 +40,17 @@ export function GeneralSettingsManager({ searchSpaceId }: GeneralSettingsManager
|
||||||
const [name, setName] = useState("");
|
const [name, setName] = useState("");
|
||||||
const [description, setDescription] = useState("");
|
const [description, setDescription] = useState("");
|
||||||
const [saving, setSaving] = useState(false);
|
const [saving, setSaving] = useState(false);
|
||||||
const [hasChanges, setHasChanges] = useState(false);
|
|
||||||
|
|
||||||
// Initialize state from fetched search space
|
// Initialize state from fetched search space
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (searchSpace) {
|
if (searchSpace) {
|
||||||
setName(searchSpace.name || "");
|
setName(searchSpace.name || "");
|
||||||
setDescription(searchSpace.description || "");
|
setDescription(searchSpace.description || "");
|
||||||
setHasChanges(false);
|
|
||||||
}
|
}
|
||||||
}, [searchSpace]);
|
}, [searchSpace]);
|
||||||
|
|
||||||
// Track changes
|
// Derive hasChanges during render
|
||||||
useEffect(() => {
|
const hasChanges = !!searchSpace && ((searchSpace.name || "") !== name || (searchSpace.description || "") !== description);
|
||||||
if (searchSpace) {
|
|
||||||
const currentName = searchSpace.name || "";
|
|
||||||
const currentDescription = searchSpace.description || "";
|
|
||||||
const changed = currentName !== name || currentDescription !== description;
|
|
||||||
setHasChanges(changed);
|
|
||||||
}
|
|
||||||
}, [searchSpace, name, description]);
|
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -73,7 +64,6 @@ export function GeneralSettingsManager({ searchSpaceId }: GeneralSettingsManager
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
setHasChanges(false);
|
|
||||||
await fetchSearchSpace();
|
await fetchSearchSpace();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Error saving search space details:", error);
|
console.error("Error saving search space details:", error);
|
||||||
|
|
|
||||||
|
|
@ -32,24 +32,16 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
|
||||||
|
|
||||||
const [customInstructions, setCustomInstructions] = useState("");
|
const [customInstructions, setCustomInstructions] = useState("");
|
||||||
const [saving, setSaving] = useState(false);
|
const [saving, setSaving] = useState(false);
|
||||||
const [hasChanges, setHasChanges] = useState(false);
|
|
||||||
|
|
||||||
// Initialize state from fetched search space
|
// Initialize state from fetched search space
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (searchSpace) {
|
if (searchSpace) {
|
||||||
setCustomInstructions(searchSpace.qna_custom_instructions || "");
|
setCustomInstructions(searchSpace.qna_custom_instructions || "");
|
||||||
setHasChanges(false);
|
|
||||||
}
|
}
|
||||||
}, [searchSpace]);
|
}, [searchSpace]);
|
||||||
|
|
||||||
// Track changes
|
// Derive hasChanges during render
|
||||||
useEffect(() => {
|
const hasChanges = !!searchSpace && (searchSpace.qna_custom_instructions || "") !== customInstructions;
|
||||||
if (searchSpace) {
|
|
||||||
const currentCustom = searchSpace.qna_custom_instructions || "";
|
|
||||||
const changed = currentCustom !== customInstructions;
|
|
||||||
setHasChanges(changed);
|
|
||||||
}
|
|
||||||
}, [searchSpace, customInstructions]);
|
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -74,7 +66,7 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.success("System instructions saved successfully");
|
toast.success("System instructions saved successfully");
|
||||||
setHasChanges(false);
|
|
||||||
await fetchSearchSpace();
|
await fetchSearchSpace();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error("Error saving system instructions:", error);
|
console.error("Error saving system instructions:", error);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue