import { FC, useMemo } from 'react' import { MainDocument } from '../../../../../types/project-settings' import { Ranges } from '../context/ranges-context' import { ReviewPanelComment } from './review-panel-comment' import { ReviewPanelChange } from './review-panel-change' import { isCommentOperation, isDeleteChange, isInsertChange, } from '@/utils/operations' import { Change, CommentOperation, DeleteOperation, EditOperation, } from '../../../../../types/change' import { canAggregate } from '../utils/can-aggregate' import MaterialIcon from '@/shared/components/material-icon' import useOverviewFileCollapsed from '../hooks/use-overview-file-collapsed' import { useThreadsContext } from '../context/threads-context' export const ReviewPanelOverviewFile: FC<{ doc: MainDocument ranges: Ranges }> = ({ doc, ranges }) => { const { collapsed, toggleCollapsed } = useOverviewFileCollapsed(doc.doc.id) const threads = useThreadsContext() const { aggregates, changes } = useMemo(() => { const changes: Change[] = [] const aggregates: Map> = new Map() let precedingChange: Change | null = null for (const change of ranges.changes) { if ( precedingChange && isInsertChange(precedingChange) && isDeleteChange(change) && canAggregate(change, precedingChange) ) { aggregates.set(precedingChange.id, change) } else { changes.push(change) } precedingChange = change } return { aggregates, changes } }, [ranges]) const entries = useMemo(() => { const unresolvedComments = ranges.comments.filter(comment => { const thread = threads?.[comment.op.t] return thread && thread.messages.length > 0 && !thread.resolved }) return [...changes, ...unresolvedComments].sort((a, b) => a.op.p - b.op.p) }, [changes, ranges.comments, threads]) if (entries.length === 0) { return null } return ( <>
{!collapsed && (
{entries.map(entry => isCommentOperation(entry.op) ? ( } docId={doc.doc.id} hoverRanges={false} /> ) : ( } aggregate={aggregates.get(entry.id)} editable={false} docId={doc.doc.id} hoverRanges={false} /> ) )}
)}
) }