2025-06-10 15:28:23 +05:30
|
|
|
import React, { useEffect } from 'react'
|
|
|
|
|
import CommentInstance from './commentInstance/commentInstance'
|
|
|
|
|
import { useCommentStore } from '../../../../store/collaboration/useCommentStore'
|
2025-06-23 09:37:53 +05:30
|
|
|
import { getAllThreads } from '../../../../services/factoryBuilder/comments/getAllThreads';
|
|
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import { getUserData } from '../../../../functions/getUserData';
|
|
|
|
|
import { getRelativeTime } from '../../../../components/ui/collaboration/function/getRelativeTime';
|
2025-06-24 11:56:26 +05:30
|
|
|
import { useVersionContext } from '../../../builder/version/versionContext';
|
2025-06-23 09:37:53 +05:30
|
|
|
|
2025-06-10 15:28:23 +05:30
|
|
|
function CommentInstances() {
|
2025-06-23 09:37:53 +05:30
|
|
|
const { comments, setComments } = useCommentStore();
|
|
|
|
|
const { projectId } = useParams();
|
2025-06-24 11:56:26 +05:30
|
|
|
const { userId } = getUserData();
|
|
|
|
|
const { selectedVersionStore } = useVersionContext();
|
|
|
|
|
const { selectedVersion } = selectedVersionStore();
|
2025-06-23 10:54:17 +05:30
|
|
|
|
2025-09-02 15:21:13 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
// console.log("comments", comments);
|
|
|
|
|
}, [comments])
|
2025-06-23 09:37:53 +05:30
|
|
|
|
2025-09-02 15:21:13 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!projectId || !selectedVersion) return;
|
|
|
|
|
getAllThreads(projectId, selectedVersion?.versionId).then((fetchedComments) => {
|
|
|
|
|
const formattedThreads = Array.isArray(fetchedComments.data)
|
|
|
|
|
? fetchedComments.data.map((thread: any) => ({
|
2025-06-23 09:37:53 +05:30
|
|
|
...thread,
|
|
|
|
|
comments: Array.isArray(thread.comments)
|
|
|
|
|
? thread.comments.map((val: any) => ({
|
|
|
|
|
replyId: val._id ?? "",
|
|
|
|
|
creatorId: userId,
|
|
|
|
|
createdAt: getRelativeTime(val.createdAt),
|
|
|
|
|
lastUpdatedAt: "1 hr ago",
|
|
|
|
|
comment: val.comment,
|
|
|
|
|
_id: val._id ?? "",
|
|
|
|
|
}))
|
|
|
|
|
: [],
|
|
|
|
|
}))
|
|
|
|
|
: [];
|
|
|
|
|
setComments(formattedThreads);
|
2025-09-02 15:21:13 +05:30
|
|
|
}).catch((err) => {
|
|
|
|
|
console.error("Failed to fetch threads:", err);
|
|
|
|
|
})
|
2025-06-23 09:37:53 +05:30
|
|
|
}, []);
|
2025-06-10 15:28:23 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2025-06-23 09:37:53 +05:30
|
|
|
{comments?.map((comment: CommentSchema) => (
|
|
|
|
|
<React.Fragment key={comment.threadId}>
|
2025-06-10 15:28:23 +05:30
|
|
|
<CommentInstance comment={comment} />
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default CommentInstances
|