mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
refactor: consolidate public chat snapshot mutations with cache invalidation
This commit is contained in:
parent
67f797232e
commit
ee56334abe
5 changed files with 41 additions and 35 deletions
|
|
@ -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");
|
||||
},
|
||||
}));
|
||||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ export function PublicChatSnapshotsManager({
|
|||
const [deletingId, setDeletingId] = useState<number | undefined>();
|
||||
|
||||
// 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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue