Merge pull request #1431 from dekalouis/refactor/1361-prompt-config-use-mutation-atom

refactor: migrate PromptConfigManager save to updateSearchSpaceMutationAtom
This commit is contained in:
Rohan Verma 2026-05-24 02:48:46 -07:00 committed by GitHub
commit 63e5943cc8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,19 +1,19 @@
"use client"; "use client";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
import { useAtomValue } from "jotai";
import { AlertTriangle, Info } from "lucide-react"; import { AlertTriangle, Info } from "lucide-react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { toast } from "sonner"; import { toast } from "sonner";
import { updateSearchSpaceMutationAtom } from "@/atoms/search-spaces/search-space-mutation.atoms";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { Skeleton } from "@/components/ui/skeleton"; import { Skeleton } from "@/components/ui/skeleton";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service"; import { searchSpacesApiService } from "@/lib/apis/search-spaces-api.service";
import { authenticatedFetch } from "@/lib/auth-utils";
import { cacheKeys } from "@/lib/query-client/cache-keys"; import { cacheKeys } from "@/lib/query-client/cache-keys";
import { Spinner } from "../ui/spinner"; import { Spinner } from "../ui/spinner";
import { BACKEND_URL } from "@/lib/env-config";
interface PromptConfigManagerProps { interface PromptConfigManagerProps {
searchSpaceId: number; searchSpaceId: number;
@ -23,15 +23,17 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
const { const {
data: searchSpace, data: searchSpace,
isLoading: loading, isLoading: loading,
refetch: fetchSearchSpace,
} = useQuery({ } = useQuery({
queryKey: cacheKeys.searchSpaces.detail(searchSpaceId.toString()), queryKey: cacheKeys.searchSpaces.detail(searchSpaceId.toString()),
queryFn: () => searchSpacesApiService.getSearchSpace({ id: searchSpaceId }), queryFn: () => searchSpacesApiService.getSearchSpace({ id: searchSpaceId }),
enabled: !!searchSpaceId, enabled: !!searchSpaceId,
}); });
const { mutateAsync: updateSearchSpace, isPending: isSaving } = useAtomValue(
updateSearchSpaceMutationAtom
);
const [customInstructions, setCustomInstructions] = useState(""); const [customInstructions, setCustomInstructions] = useState("");
const [saving, setSaving] = useState(false);
const hasSearchSpace = !!searchSpace; const hasSearchSpace = !!searchSpace;
const searchSpaceInstructions = searchSpace?.qna_custom_instructions; const searchSpaceInstructions = searchSpace?.qna_custom_instructions;
@ -48,34 +50,16 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
const handleSave = async () => { const handleSave = async () => {
try { try {
setSaving(true); await updateSearchSpace({
id: searchSpaceId,
const payload = { data: { qna_custom_instructions: customInstructions.trim() || "" },
qna_custom_instructions: customInstructions.trim() || "", });
};
const response = await authenticatedFetch(
`${BACKEND_URL}/api/v1/searchspaces/${searchSpaceId}`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
}
);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || "Failed to save system instructions");
}
toast.success("System instructions saved successfully"); toast.success("System instructions saved successfully");
await fetchSearchSpace();
} catch (error: unknown) { } catch (error: unknown) {
const message =
error instanceof Error ? error.message : "Failed to save system instructions";
console.error("Error saving system instructions:", error); console.error("Error saving system instructions:", error);
toast.error(error instanceof Error ? error.message : "Failed to save system instructions"); toast.error(message);
} finally {
setSaving(false);
} }
}; };
@ -184,11 +168,11 @@ export function PromptConfigManager({ searchSpaceId }: PromptConfigManagerProps)
<Button <Button
type="submit" type="submit"
variant="outline" variant="outline"
disabled={!hasChanges || saving} disabled={!hasChanges || isSaving}
className="gap-2 bg-white text-black hover:bg-accent hover:text-accent-foreground dark:bg-white dark:text-black" className="gap-2 bg-white text-black hover:bg-accent hover:text-accent-foreground dark:bg-white dark:text-black"
> >
{saving ? <Spinner size="sm" /> : null} {isSaving ? <Spinner size="sm" /> : null}
{saving ? "Saving" : "Save Instructions"} {isSaving ? "Saving" : "Save Instructions"}
</Button> </Button>
</div> </div>
</form> </form>