2026-01-16 13:13:37 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useAtom } from "jotai";
|
|
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import {
|
|
|
|
|
createCommentMutationAtom,
|
|
|
|
|
createReplyMutationAtom,
|
|
|
|
|
deleteCommentMutationAtom,
|
|
|
|
|
updateCommentMutationAtom,
|
|
|
|
|
} from "@/atoms/chat-comments/comments-mutation.atoms";
|
|
|
|
|
import { membersAtom } from "@/atoms/members/members-query.atoms";
|
2026-01-16 15:53:33 +02:00
|
|
|
import { currentUserAtom } from "@/atoms/user/user-query.atoms";
|
2026-01-16 13:13:37 +02:00
|
|
|
import { useComments } from "@/hooks/use-comments";
|
|
|
|
|
import { CommentPanel } from "../comment-panel/comment-panel";
|
|
|
|
|
import type { CommentPanelContainerProps } from "./types";
|
|
|
|
|
import { transformComment, transformMember } from "./utils";
|
|
|
|
|
|
|
|
|
|
export function CommentPanelContainer({
|
|
|
|
|
messageId,
|
|
|
|
|
isOpen,
|
2026-01-19 14:37:38 +02:00
|
|
|
maxHeight,
|
2026-01-19 16:50:51 -08:00
|
|
|
variant = "desktop",
|
2026-01-16 13:13:37 +02:00
|
|
|
}: CommentPanelContainerProps) {
|
|
|
|
|
const { data: commentsData, isLoading: isCommentsLoading } = useComments({
|
|
|
|
|
messageId,
|
|
|
|
|
enabled: isOpen,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [{ data: membersData, isLoading: isMembersLoading }] = useAtom(membersAtom);
|
2026-01-16 15:53:33 +02:00
|
|
|
const [{ data: currentUser }] = useAtom(currentUserAtom);
|
2026-01-16 13:13:37 +02:00
|
|
|
|
|
|
|
|
const [{ mutate: createComment, isPending: isCreating }] = useAtom(createCommentMutationAtom);
|
|
|
|
|
const [{ mutate: createReply, isPending: isCreatingReply }] = useAtom(createReplyMutationAtom);
|
|
|
|
|
const [{ mutate: updateComment, isPending: isUpdating }] = useAtom(updateCommentMutationAtom);
|
|
|
|
|
const [{ mutate: deleteComment, isPending: isDeleting }] = useAtom(deleteCommentMutationAtom);
|
|
|
|
|
|
|
|
|
|
const commentThreads = useMemo(() => {
|
|
|
|
|
if (!commentsData?.comments) return [];
|
|
|
|
|
return commentsData.comments.map(transformComment);
|
|
|
|
|
}, [commentsData]);
|
|
|
|
|
|
|
|
|
|
const members = useMemo(() => {
|
|
|
|
|
if (!membersData) return [];
|
2026-01-16 15:53:33 +02:00
|
|
|
const allMembers = membersData.map(transformMember);
|
|
|
|
|
// Filter out current user from mention picker
|
|
|
|
|
if (currentUser?.id) {
|
|
|
|
|
return allMembers.filter((member) => member.id !== currentUser.id);
|
|
|
|
|
}
|
|
|
|
|
return allMembers;
|
|
|
|
|
}, [membersData, currentUser?.id]);
|
2026-01-16 13:13:37 +02:00
|
|
|
|
|
|
|
|
const isSubmitting = isCreating || isCreatingReply || isUpdating || isDeleting;
|
|
|
|
|
|
|
|
|
|
const handleCreateComment = (content: string) => {
|
|
|
|
|
createComment({ message_id: messageId, content });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleCreateReply = (commentId: number, content: string) => {
|
|
|
|
|
createReply({ comment_id: commentId, content, message_id: messageId });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleEditComment = (commentId: number, content: string) => {
|
|
|
|
|
updateComment({ comment_id: commentId, content, message_id: messageId });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDeleteComment = (commentId: number) => {
|
|
|
|
|
deleteComment({ comment_id: commentId, message_id: messageId });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!isOpen) return null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<CommentPanel
|
|
|
|
|
threads={commentThreads}
|
|
|
|
|
members={members}
|
|
|
|
|
membersLoading={isMembersLoading}
|
|
|
|
|
isLoading={isCommentsLoading}
|
|
|
|
|
onCreateComment={handleCreateComment}
|
|
|
|
|
onCreateReply={handleCreateReply}
|
|
|
|
|
onEditComment={handleEditComment}
|
|
|
|
|
onDeleteComment={handleDeleteComment}
|
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
|
maxHeight={maxHeight}
|
2026-01-19 16:50:51 -08:00
|
|
|
variant={variant}
|
2026-01-16 13:13:37 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|