mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-30 11:26:24 +02:00
feat(web): add chat comments atoms and hooks
This commit is contained in:
parent
33670aceb5
commit
134f9577b9
3 changed files with 163 additions and 0 deletions
110
surfsense_web/atoms/chat-comments/comments-mutation.atoms.ts
Normal file
110
surfsense_web/atoms/chat-comments/comments-mutation.atoms.ts
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { atomWithMutation } from "jotai-tanstack-query";
|
||||
import { toast } from "sonner";
|
||||
import type {
|
||||
CreateCommentRequest,
|
||||
CreateReplyRequest,
|
||||
DeleteCommentRequest,
|
||||
MarkMentionReadRequest,
|
||||
UpdateCommentRequest,
|
||||
} from "@/contracts/types/chat-comments.types";
|
||||
import { chatCommentsApiService } from "@/lib/apis/chat-comments-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
import { queryClient } from "@/lib/query-client/client";
|
||||
|
||||
export const createCommentMutationAtom = atomWithMutation(() => ({
|
||||
mutationFn: async (request: CreateCommentRequest) => {
|
||||
return chatCommentsApiService.createComment(request);
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.comments.byMessage(variables.message_id),
|
||||
});
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error("Error creating comment:", error);
|
||||
toast.error("Failed to create comment");
|
||||
},
|
||||
}));
|
||||
|
||||
export const createReplyMutationAtom = atomWithMutation(() => ({
|
||||
mutationFn: async (request: CreateReplyRequest & { message_id: number }) => {
|
||||
return chatCommentsApiService.createReply(request);
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.comments.byMessage(variables.message_id),
|
||||
});
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error("Error creating reply:", error);
|
||||
toast.error("Failed to create reply");
|
||||
},
|
||||
}));
|
||||
|
||||
export const updateCommentMutationAtom = atomWithMutation(() => ({
|
||||
mutationFn: async (request: UpdateCommentRequest & { message_id: number }) => {
|
||||
return chatCommentsApiService.updateComment(request);
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.comments.byMessage(variables.message_id),
|
||||
});
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error("Error updating comment:", error);
|
||||
toast.error("Failed to update comment");
|
||||
},
|
||||
}));
|
||||
|
||||
export const deleteCommentMutationAtom = atomWithMutation(() => ({
|
||||
mutationFn: async (request: DeleteCommentRequest & { message_id: number }) => {
|
||||
return chatCommentsApiService.deleteComment(request);
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.comments.byMessage(variables.message_id),
|
||||
});
|
||||
toast.success("Comment deleted");
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error("Error deleting comment:", error);
|
||||
toast.error("Failed to delete comment");
|
||||
},
|
||||
}));
|
||||
|
||||
export const markMentionReadMutationAtom = atomWithMutation(() => ({
|
||||
mutationFn: async (request: MarkMentionReadRequest & { search_space_id?: number }) => {
|
||||
return chatCommentsApiService.markMentionRead(request);
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.mentions.all(variables.search_space_id),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.mentions.unreadOnly(variables.search_space_id),
|
||||
});
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error("Error marking mention as read:", error);
|
||||
},
|
||||
}));
|
||||
|
||||
export const markAllMentionsReadMutationAtom = atomWithMutation(() => ({
|
||||
mutationFn: async (request: { search_space_id?: number }) => {
|
||||
return chatCommentsApiService.markAllMentionsRead();
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.mentions.all(variables.search_space_id),
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: cacheKeys.mentions.unreadOnly(variables.search_space_id),
|
||||
});
|
||||
toast.success("All mentions marked as read");
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
console.error("Error marking all mentions as read:", error);
|
||||
toast.error("Failed to mark mentions as read");
|
||||
},
|
||||
}));
|
||||
|
||||
34
surfsense_web/atoms/chat-comments/comments-query.atoms.ts
Normal file
34
surfsense_web/atoms/chat-comments/comments-query.atoms.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { atomWithQuery } from "jotai-tanstack-query";
|
||||
import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms";
|
||||
import { chatCommentsApiService } from "@/lib/apis/chat-comments-api.service";
|
||||
import { cacheKeys } from "@/lib/query-client/cache-keys";
|
||||
|
||||
export const mentionsAtom = atomWithQuery((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
|
||||
return {
|
||||
queryKey: cacheKeys.mentions.all(searchSpaceId ? Number(searchSpaceId) : undefined),
|
||||
staleTime: 60 * 1000, // 1 minute
|
||||
queryFn: async () => {
|
||||
return chatCommentsApiService.getMentions({
|
||||
search_space_id: searchSpaceId ? Number(searchSpaceId) : undefined,
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export const unreadMentionsAtom = atomWithQuery((get) => {
|
||||
const searchSpaceId = get(activeSearchSpaceIdAtom);
|
||||
|
||||
return {
|
||||
queryKey: cacheKeys.mentions.unreadOnly(searchSpaceId ? Number(searchSpaceId) : undefined),
|
||||
staleTime: 30 * 1000, // 30 seconds
|
||||
queryFn: async () => {
|
||||
return chatCommentsApiService.getMentions({
|
||||
search_space_id: searchSpaceId ? Number(searchSpaceId) : undefined,
|
||||
unread_only: true,
|
||||
});
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue