diff --git a/surfsense_web/atoms/chat/chat-thread-mutation.atoms.ts b/surfsense_web/atoms/chat/chat-thread-mutation.atoms.ts deleted file mode 100644 index 35858171e..000000000 --- a/surfsense_web/atoms/chat/chat-thread-mutation.atoms.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { atomWithMutation } from "jotai-tanstack-query"; -import { toast } from "sonner"; -import type { - PublicChatSnapshotCreateRequest, - PublicChatSnapshotCreateResponse, -} from "@/contracts/types/chat-threads.types"; -import { chatThreadsApiService } from "@/lib/apis/chat-threads-api.service"; - -export const createPublicChatSnapshotMutationAtom = atomWithMutation(() => ({ - mutationFn: async (request: PublicChatSnapshotCreateRequest) => { - return chatThreadsApiService.createPublicChatSnapshot(request); - }, - onSuccess: (response: PublicChatSnapshotCreateResponse) => { - const publicUrl = `${window.location.origin}/public/${response.share_token}`; - navigator.clipboard.writeText(publicUrl); - if (response.is_new) { - toast.success("Public link created and copied to clipboard", { - description: "Anyone with this link can view a snapshot of this chat", - }); - } else { - toast.success("Public link copied to clipboard", { - description: "This snapshot already exists", - }); - } - }, - onError: (error: Error) => { - console.error("Failed to create snapshot:", error); - toast.error("Failed to create public link"); - }, -})); diff --git a/surfsense_web/atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms.ts b/surfsense_web/atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms.ts index a0343fb9d..bfdfccb16 100644 --- a/surfsense_web/atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms.ts +++ b/surfsense_web/atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms.ts @@ -1,13 +1,49 @@ import { atomWithMutation } from "jotai-tanstack-query"; import { toast } from "sonner"; -import type { PublicChatSnapshotDeleteRequest } from "@/contracts/types/chat-threads.types"; +import type { + PublicChatSnapshotCreateRequest, + PublicChatSnapshotCreateResponse, + PublicChatSnapshotDeleteRequest, +} from "@/contracts/types/chat-threads.types"; import { chatThreadsApiService } from "@/lib/apis/chat-threads-api.service"; +import { cacheKeys } from "@/lib/query-client/cache-keys"; +import { queryClient } from "@/lib/query-client/client"; + +export const createPublicChatSnapshotMutationAtom = atomWithMutation(() => ({ + mutationFn: async (request: PublicChatSnapshotCreateRequest) => { + return chatThreadsApiService.createPublicChatSnapshot(request); + }, + onSuccess: (response: PublicChatSnapshotCreateResponse) => { + queryClient.invalidateQueries({ + queryKey: cacheKeys.publicChatSnapshots.all, + }); + + const publicUrl = `${window.location.origin}/public/${response.share_token}`; + navigator.clipboard.writeText(publicUrl); + if (response.is_new) { + toast.success("Public link created and copied to clipboard", { + description: "Anyone with this link can view a snapshot of this chat", + }); + } else { + toast.success("Public link copied to clipboard", { + description: "This snapshot already exists", + }); + } + }, + onError: (error: Error) => { + console.error("Failed to create snapshot:", error); + toast.error("Failed to create public link"); + }, +})); export const deletePublicChatSnapshotMutationAtom = atomWithMutation(() => ({ mutationFn: async (request: PublicChatSnapshotDeleteRequest) => { return chatThreadsApiService.deletePublicChatSnapshot(request); }, onSuccess: () => { + queryClient.invalidateQueries({ + queryKey: cacheKeys.publicChatSnapshots.all, + }); toast.success("Public link deleted"); }, onError: (error: Error) => { diff --git a/surfsense_web/components/new-chat/chat-share-button.tsx b/surfsense_web/components/new-chat/chat-share-button.tsx index 23af0995e..336a3d4e8 100644 --- a/surfsense_web/components/new-chat/chat-share-button.tsx +++ b/surfsense_web/components/new-chat/chat-share-button.tsx @@ -5,7 +5,7 @@ import { useAtomValue, useSetAtom } from "jotai"; import { Globe, User, Users } from "lucide-react"; import { useCallback, useMemo, useState } from "react"; import { toast } from "sonner"; -import { createPublicChatSnapshotMutationAtom } from "@/atoms/chat/chat-thread-mutation.atoms"; +import { createPublicChatSnapshotMutationAtom } from "@/atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms"; import { currentThreadAtom, setThreadVisibilityAtom } from "@/atoms/chat/current-thread.atom"; import { myAccessAtom } from "@/atoms/members/members-query.atoms"; import { Button } from "@/components/ui/button"; diff --git a/surfsense_web/components/public-chat-snapshots/public-chat-snapshots-manager.tsx b/surfsense_web/components/public-chat-snapshots/public-chat-snapshots-manager.tsx index 1c5c1fe42..f5fd281be 100644 --- a/surfsense_web/components/public-chat-snapshots/public-chat-snapshots-manager.tsx +++ b/surfsense_web/components/public-chat-snapshots/public-chat-snapshots-manager.tsx @@ -23,7 +23,7 @@ export function PublicChatSnapshotsManager({ const [deletingId, setDeletingId] = useState(); // Data fetching - const { data: snapshotsData, isLoading, isError, refetch } = useAtomValue(publicChatSnapshotsAtom); + const { data: snapshotsData, isLoading, isError } = useAtomValue(publicChatSnapshotsAtom); // Permissions const { data: access } = useAtomValue(myAccessAtom); @@ -57,14 +57,13 @@ export function PublicChatSnapshotsManager({ thread_id: snapshot.thread_id, snapshot_id: snapshot.id, }); - await refetch(); } catch (error) { console.error("Failed to delete snapshot:", error); } finally { setDeletingId(undefined); } }, - [deleteSnapshot, refetch] + [deleteSnapshot] ); // Loading state diff --git a/surfsense_web/lib/query-client/cache-keys.ts b/surfsense_web/lib/query-client/cache-keys.ts index df456eba3..4d220a62a 100644 --- a/surfsense_web/lib/query-client/cache-keys.ts +++ b/surfsense_web/lib/query-client/cache-keys.ts @@ -83,6 +83,7 @@ export const cacheKeys = { byToken: (shareToken: string) => ["public-chat", shareToken] as const, }, publicChatSnapshots: { + all: ["public-chat-snapshots"] as const, bySearchSpace: (searchSpaceId: number) => ["public-chat-snapshots", "search-space", searchSpaceId] as const, },