mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-30 03:16:25 +02:00
Fix create_prompt is_public bug, conditional version bump, and add Jotai prompts atoms
- Pass is_public from request body in create_prompt route - Only bump version on content field changes (name, prompt, mode), not on is_public toggle - Add prompts query and mutation atoms (atomWithQuery/atomWithMutation) with TanStack Query caching, replacing manual useEffect fetches - Update PromptPicker, PromptsContent, and CommunityPromptsContent to consume shared atoms instead of local state
This commit is contained in:
parent
95620a4331
commit
5f4f7780d1
7 changed files with 184 additions and 88 deletions
71
surfsense_web/atoms/prompts/prompts-mutation.atoms.ts
Normal file
71
surfsense_web/atoms/prompts/prompts-mutation.atoms.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { atomWithMutation } from "jotai-tanstack-query";
|
||||
import { toast } from "sonner";
|
||||
import type {
|
||||
PromptCreateRequest,
|
||||
PromptRead,
|
||||
PromptUpdateRequest,
|
||||
} from "@/contracts/types/prompts.types";
|
||||
import { promptsApiService } from "@/lib/apis/prompts-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
import { queryClient } from "@/lib/query-client/client";
|
||||
|
||||
export const createPromptMutationAtom = atomWithMutation(() => ({
|
||||
mutationKey: ["prompts", "create"],
|
||||
mutationFn: async (request: PromptCreateRequest) => {
|
||||
return promptsApiService.create(request);
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success("Prompt created");
|
||||
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.all() });
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message || "Failed to create prompt");
|
||||
},
|
||||
}));
|
||||
|
||||
export const updatePromptMutationAtom = atomWithMutation(() => ({
|
||||
mutationKey: ["prompts", "update"],
|
||||
mutationFn: async ({ id, ...data }: PromptUpdateRequest & { id: number }) => {
|
||||
return promptsApiService.update(id, data);
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success("Prompt updated");
|
||||
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.all() });
|
||||
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.public() });
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message || "Failed to update prompt");
|
||||
},
|
||||
}));
|
||||
|
||||
export const deletePromptMutationAtom = atomWithMutation(() => ({
|
||||
mutationKey: ["prompts", "delete"],
|
||||
mutationFn: async (id: number) => {
|
||||
return promptsApiService.delete(id);
|
||||
},
|
||||
onSuccess: (_: unknown, id: number) => {
|
||||
toast.success("Prompt deleted");
|
||||
queryClient.setQueryData(cacheKeys.prompts.all(), (old: PromptRead[] | undefined) => {
|
||||
if (!old) return old;
|
||||
return old.filter((p) => p.id !== id);
|
||||
});
|
||||
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.public() });
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message || "Failed to delete prompt");
|
||||
},
|
||||
}));
|
||||
|
||||
export const copyPromptMutationAtom = atomWithMutation(() => ({
|
||||
mutationKey: ["prompts", "copy"],
|
||||
mutationFn: async (promptId: number) => {
|
||||
return promptsApiService.copy(promptId);
|
||||
},
|
||||
onSuccess: () => {
|
||||
toast.success("Prompt added to your collection");
|
||||
queryClient.invalidateQueries({ queryKey: cacheKeys.prompts.all() });
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message || "Failed to copy prompt");
|
||||
},
|
||||
}));
|
||||
23
surfsense_web/atoms/prompts/prompts-query.atoms.ts
Normal file
23
surfsense_web/atoms/prompts/prompts-query.atoms.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { atomWithQuery } from "jotai-tanstack-query";
|
||||
import { promptsApiService } from "@/lib/apis/prompts-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
|
||||
export const promptsAtom = atomWithQuery(() => {
|
||||
return {
|
||||
queryKey: cacheKeys.prompts.all(),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
queryFn: async () => {
|
||||
return promptsApiService.list();
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const publicPromptsAtom = atomWithQuery(() => {
|
||||
return {
|
||||
queryKey: cacheKeys.prompts.public(),
|
||||
staleTime: 2 * 60 * 1000,
|
||||
queryFn: async () => {
|
||||
return promptsApiService.listPublic();
|
||||
},
|
||||
};
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue