import { ThreadData } from "@blocknote/core/comments"; import { useComponentsContext } from "../../editor/ComponentsContext.js"; import { useDictionary } from "../../i18n/dictionary.js"; import { Comment } from "./Comment.js"; import { useUsers } from "./useUsers.js"; export type CommentsProps = { thread: ThreadData; maxCommentsBeforeCollapse?: number; }; export const Comments = ({ thread, maxCommentsBeforeCollapse, }: CommentsProps) => { const Components = useComponentsContext()!; const dict = useDictionary(); const users = useUsers(thread.resolvedBy ? [thread.resolvedBy] : []); // Maps all comments to elements. const comments = thread.comments.map((comment, index) => ( )); // Adds "resolved by" comment if needed. if (thread.resolved && thread.resolvedUpdatedAt && thread.resolvedBy) { const resolvedByUser = users.get(thread.resolvedBy); if (!resolvedByUser) { throw new Error( `User ${thread.resolvedBy} resolved thread ${thread.id}, but their data could not be found.`, ); } const resolvedCommentIndex = thread.comments.findLastIndex( (comment) => thread.resolvedUpdatedAt!.getTime() > comment.createdAt.getTime(), ) + 1; comments.splice( resolvedCommentIndex, 0,
{dict.comments.sidebar.marked_as_resolved}
, ); } // Collapses replies if needed. if ( maxCommentsBeforeCollapse && comments.length > maxCommentsBeforeCollapse ) { comments.splice( 1, comments.length - 2, {dict.comments.sidebar.more_replies(thread.comments.length - 2)} , ); } return comments; };