import type { CreateComment, UpdateComment } from "@shared/schema"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import type { CommentSearchParams } from "@/api/comment-api-client"; import { commentApiClient } from "@/api/comment-api-client"; // Query hooks export function useComment(commentId: string) { return useQuery({ queryKey: ["comment", commentId], queryFn: () => commentApiClient.getComment(commentId), enabled: !!commentId, }); } export function useComments(params?: CommentSearchParams) { return useQuery({ queryKey: ["comments", params], queryFn: () => commentApiClient.getComments(params), }); } export function useWorkComments(workId: string) { return useQuery({ queryKey: ["work-comments", workId], queryFn: () => commentApiClient.getWorkComments(workId), enabled: !!workId, }); } export function useTranslationComments(translationId: string) { return useQuery({ queryKey: ["translation-comments", translationId], queryFn: () => commentApiClient.getTranslationComments(translationId), enabled: !!translationId, }); } export function useCommentReplies(commentId: string) { return useQuery({ queryKey: ["comment-replies", commentId], queryFn: () => commentApiClient.getCommentReplies(commentId), enabled: !!commentId, }); } // Mutation hooks export function useCreateComment() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (data: CreateComment) => commentApiClient.createComment(data), onSuccess: (_, variables) => { if (variables.workId) { queryClient.invalidateQueries({ queryKey: ["work-comments", variables.workId], }); } if (variables.translationId) { queryClient.invalidateQueries({ queryKey: ["translation-comments", variables.translationId], }); } if (variables.parentId) { queryClient.invalidateQueries({ queryKey: ["comment-replies", variables.parentId], }); } queryClient.invalidateQueries({ queryKey: ["comments"] }); }, }); } export function useCreateWorkComment() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ workId, data, }: { workId: string; data: Omit; }) => commentApiClient.createWorkComment(workId, data), onSuccess: (_, variables) => { queryClient.invalidateQueries({ queryKey: ["work-comments", variables.workId], }); queryClient.invalidateQueries({ queryKey: ["comments"] }); }, }); } export function useCreateTranslationComment() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ translationId, data, }: { translationId: string; data: Omit; }) => commentApiClient.createTranslationComment(translationId, data), onSuccess: (_, variables) => { queryClient.invalidateQueries({ queryKey: ["translation-comments", variables.translationId], }); queryClient.invalidateQueries({ queryKey: ["comments"] }); }, }); } export function useCreateCommentReply() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ parentCommentId, data, }: { parentCommentId: string; data: Omit; }) => commentApiClient.createCommentReply(parentCommentId, data), onSuccess: (_, variables) => { queryClient.invalidateQueries({ queryKey: ["comment-replies", variables.parentCommentId], }); queryClient.invalidateQueries({ queryKey: ["comments"] }); }, }); } export function useUpdateComment() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ commentId, data, }: { commentId: string; data: UpdateComment; }) => commentApiClient.updateComment(commentId, data), onSuccess: (_, variables) => { queryClient.invalidateQueries({ queryKey: ["comment", variables.commentId], }); queryClient.invalidateQueries({ queryKey: ["comments"] }); queryClient.invalidateQueries({ queryKey: ["work-comments"] }); queryClient.invalidateQueries({ queryKey: ["translation-comments"] }); queryClient.invalidateQueries({ queryKey: ["comment-replies"] }); }, }); } export function useDeleteComment() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (commentId: string) => commentApiClient.deleteComment(commentId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["comments"] }); queryClient.invalidateQueries({ queryKey: ["work-comments"] }); queryClient.invalidateQueries({ queryKey: ["translation-comments"] }); queryClient.invalidateQueries({ queryKey: ["comment-replies"] }); }, }); }