2024-08-12 05:50:54 -04:00
|
|
|
import {
|
|
|
|
FC,
|
|
|
|
memo,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useRef,
|
|
|
|
useState,
|
2024-09-16 04:40:59 -04:00
|
|
|
useMemo,
|
2024-08-12 05:50:54 -04:00
|
|
|
} from 'react'
|
|
|
|
import { ReviewPanelAddComment } from './review-panel-add-comment'
|
|
|
|
import { ReviewPanelChange } from './review-panel-change'
|
|
|
|
import { ReviewPanelComment } from './review-panel-comment'
|
|
|
|
import {
|
|
|
|
Change,
|
|
|
|
CommentOperation,
|
|
|
|
DeleteOperation,
|
|
|
|
EditOperation,
|
|
|
|
} from '../../../../../types/change'
|
|
|
|
import {
|
|
|
|
useCodeMirrorStateContext,
|
|
|
|
useCodeMirrorViewContext,
|
|
|
|
} from '@/features/source-editor/components/codemirror-editor'
|
|
|
|
import { useRangesContext } from '../context/ranges-context'
|
|
|
|
import { useThreadsContext } from '../context/threads-context'
|
|
|
|
import { isDeleteChange, isInsertChange } from '@/utils/operations'
|
|
|
|
import { positionItems } from '../utils/position-items'
|
|
|
|
import { canAggregate } from '../utils/can-aggregate'
|
|
|
|
import ReviewPanelEmptyState from './review-panel-empty-state'
|
2024-08-15 06:22:14 -04:00
|
|
|
import useEventListener from '@/shared/hooks/use-event-listener'
|
2024-08-22 05:44:22 -04:00
|
|
|
import { hasActiveRange } from '@/features/review-panel-new/utils/has-active-range'
|
2024-09-16 04:40:59 -04:00
|
|
|
import { addCommentStateField } from '@/features/source-editor/extensions/add-comment'
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
type AggregatedRanges = {
|
2024-08-12 05:50:54 -04:00
|
|
|
changes: Change<EditOperation>[]
|
|
|
|
comments: Change<CommentOperation>[]
|
2024-08-22 05:44:22 -04:00
|
|
|
aggregates: Map<string, Change<DeleteOperation>>
|
2024-08-12 05:50:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const ReviewPanelCurrentFile: FC = () => {
|
|
|
|
const view = useCodeMirrorViewContext()
|
|
|
|
const ranges = useRangesContext()
|
|
|
|
const threads = useThreadsContext()
|
|
|
|
const state = useCodeMirrorStateContext()
|
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
const [aggregatedRanges, setAggregatedRanges] = useState<AggregatedRanges>()
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
const containerRef = useRef<HTMLDivElement | null>(null)
|
2024-09-19 06:08:13 -04:00
|
|
|
const previousFocusedItem = useRef(new Map<string, number>())
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
const updatePositions = useCallback(() => {
|
2024-09-19 06:08:13 -04:00
|
|
|
const docId = ranges?.docId
|
|
|
|
|
|
|
|
if (containerRef.current && docId) {
|
|
|
|
const positioningRes = positionItems(
|
2024-08-12 05:50:54 -04:00
|
|
|
containerRef.current,
|
2024-09-19 06:08:13 -04:00
|
|
|
previousFocusedItem.current.get(docId) || 0,
|
|
|
|
docId
|
2024-08-12 05:50:54 -04:00
|
|
|
)
|
|
|
|
|
2024-09-19 06:08:13 -04:00
|
|
|
if (positioningRes) {
|
|
|
|
previousFocusedItem.current.set(
|
|
|
|
positioningRes.docId,
|
|
|
|
positioningRes.activeItemIndex
|
|
|
|
)
|
2024-08-12 05:50:54 -04:00
|
|
|
}
|
|
|
|
}
|
2024-09-19 06:08:13 -04:00
|
|
|
}, [ranges?.docId])
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const timer = window.setTimeout(() => {
|
|
|
|
updatePositions()
|
2024-08-15 06:07:40 -04:00
|
|
|
}, 50)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.clearTimeout(timer)
|
|
|
|
}
|
2024-08-15 06:22:14 -04:00
|
|
|
}, [state, updatePositions])
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-08-20 03:36:18 -04:00
|
|
|
const handleContainer = useCallback(
|
|
|
|
(element: HTMLDivElement | null) => {
|
|
|
|
containerRef.current = element
|
|
|
|
if (containerRef.current) {
|
|
|
|
containerRef.current.addEventListener(
|
|
|
|
'review-panel:position',
|
|
|
|
updatePositions
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[updatePositions]
|
|
|
|
)
|
|
|
|
|
2024-08-12 05:50:54 -04:00
|
|
|
useEffect(() => {
|
2024-08-20 03:36:18 -04:00
|
|
|
return () => {
|
|
|
|
if (containerRef.current) {
|
|
|
|
containerRef.current.removeEventListener(
|
|
|
|
'review-panel:position',
|
|
|
|
updatePositions
|
|
|
|
)
|
2024-08-12 05:50:54 -04:00
|
|
|
}
|
|
|
|
}
|
2024-08-20 03:36:18 -04:00
|
|
|
}, [updatePositions])
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
const buildAggregatedRanges = useCallback(() => {
|
2024-08-12 05:50:54 -04:00
|
|
|
if (ranges) {
|
2024-08-22 05:44:22 -04:00
|
|
|
const output: AggregatedRanges = {
|
|
|
|
aggregates: new Map(),
|
|
|
|
changes: [],
|
|
|
|
comments: [],
|
|
|
|
}
|
|
|
|
|
|
|
|
let precedingChange: Change<EditOperation> | null = null
|
|
|
|
|
|
|
|
for (const change of ranges.changes) {
|
|
|
|
if (
|
|
|
|
precedingChange &&
|
|
|
|
isInsertChange(precedingChange) &&
|
|
|
|
isDeleteChange(change) &&
|
|
|
|
canAggregate(change, precedingChange)
|
|
|
|
) {
|
|
|
|
output.aggregates.set(precedingChange.id, change)
|
|
|
|
} else {
|
|
|
|
output.changes.push(change)
|
|
|
|
}
|
|
|
|
|
|
|
|
precedingChange = change
|
|
|
|
}
|
|
|
|
|
|
|
|
if (threads) {
|
|
|
|
for (const comment of ranges.comments) {
|
|
|
|
if (!threads[comment.op.t]?.resolved) {
|
|
|
|
output.comments.push(comment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setAggregatedRanges(output)
|
|
|
|
}
|
|
|
|
}, [threads, ranges])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
buildAggregatedRanges()
|
|
|
|
}, [buildAggregatedRanges])
|
|
|
|
|
|
|
|
useEventListener('editor:viewport-changed', buildAggregatedRanges)
|
|
|
|
|
|
|
|
const [positions, setPositions] = useState<Map<string, number>>(new Map())
|
|
|
|
|
|
|
|
const positionsRef = useRef<Map<string, number>>(new Map())
|
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const addCommentRanges = state.field(addCommentStateField).ranges
|
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (aggregatedRanges) {
|
2024-08-12 05:50:54 -04:00
|
|
|
view.requestMeasure({
|
|
|
|
key: 'review-panel-position',
|
2024-08-22 05:44:22 -04:00
|
|
|
read(view) {
|
|
|
|
const contentRect = view.contentDOM.getBoundingClientRect()
|
|
|
|
const docLength = view.state.doc.length
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const screenPosition = (position: number): number | undefined => {
|
|
|
|
const pos = Math.min(position, docLength) // TODO: needed?
|
2024-08-22 05:44:22 -04:00
|
|
|
const coords = view.coordsAtPos(pos)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
return coords ? Math.round(coords.top - contentRect.top) : undefined
|
2024-08-12 05:50:54 -04:00
|
|
|
}
|
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
for (const change of aggregatedRanges.changes) {
|
2024-09-16 04:40:59 -04:00
|
|
|
const position = screenPosition(change.op.p)
|
2024-08-22 05:44:22 -04:00
|
|
|
if (position) {
|
|
|
|
positionsRef.current.set(change.id, position)
|
2024-08-12 05:50:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
for (const comment of aggregatedRanges.comments) {
|
2024-09-16 04:40:59 -04:00
|
|
|
const position = screenPosition(comment.op.p)
|
2024-08-22 05:44:22 -04:00
|
|
|
if (position) {
|
|
|
|
positionsRef.current.set(comment.id, position)
|
|
|
|
}
|
|
|
|
}
|
2024-09-16 04:40:59 -04:00
|
|
|
|
|
|
|
const cursor = addCommentRanges.iter()
|
|
|
|
|
|
|
|
while (cursor.value) {
|
|
|
|
const { from } = cursor
|
|
|
|
const position = screenPosition(from)
|
|
|
|
|
|
|
|
if (position) {
|
|
|
|
positionsRef.current.set(
|
|
|
|
`new-comment-${cursor.value.spec.id}`,
|
|
|
|
position
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.next()
|
|
|
|
}
|
2024-08-12 05:50:54 -04:00
|
|
|
},
|
2024-08-22 05:44:22 -04:00
|
|
|
write() {
|
2024-09-16 04:40:59 -04:00
|
|
|
setPositions(new Map(positionsRef.current))
|
2024-08-12 05:50:54 -04:00
|
|
|
window.setTimeout(() => {
|
|
|
|
containerRef.current?.dispatchEvent(
|
|
|
|
new Event('review-panel:position')
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-09-16 04:40:59 -04:00
|
|
|
}, [view, aggregatedRanges, addCommentRanges])
|
2024-08-15 06:22:14 -04:00
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
const showEmptyState = useMemo(
|
|
|
|
() => hasActiveRange(ranges, threads) === false,
|
|
|
|
[ranges, threads]
|
|
|
|
)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const addCommentEntries = useMemo(() => {
|
|
|
|
const cursor = addCommentRanges.iter()
|
|
|
|
|
|
|
|
const entries = []
|
|
|
|
|
|
|
|
while (cursor.value) {
|
|
|
|
const id = `new-comment-${cursor.value.spec.id}`
|
|
|
|
if (!positions.has(id)) {
|
|
|
|
cursor.next()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
const { from, to } = cursor
|
|
|
|
|
|
|
|
entries.push({
|
|
|
|
id,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
value: cursor.value,
|
|
|
|
top: positions.get(id),
|
|
|
|
})
|
|
|
|
|
|
|
|
cursor.next()
|
|
|
|
}
|
|
|
|
return entries
|
|
|
|
}, [addCommentRanges, positions])
|
|
|
|
|
2024-08-22 05:44:22 -04:00
|
|
|
if (!aggregatedRanges) {
|
2024-08-12 05:50:54 -04:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-09-19 06:07:33 -04:00
|
|
|
<>
|
2024-08-12 05:50:54 -04:00
|
|
|
{showEmptyState && <ReviewPanelEmptyState />}
|
|
|
|
|
2024-09-19 06:07:33 -04:00
|
|
|
<div ref={handleContainer}>
|
|
|
|
{addCommentEntries.map(entry => {
|
|
|
|
const { id, from, to, value, top } = entry
|
|
|
|
return (
|
|
|
|
<ReviewPanelAddComment
|
|
|
|
key={id}
|
|
|
|
from={from}
|
|
|
|
to={to}
|
|
|
|
value={value}
|
|
|
|
top={top}
|
2024-08-22 05:44:22 -04:00
|
|
|
/>
|
|
|
|
)
|
2024-09-19 06:07:33 -04:00
|
|
|
})}
|
|
|
|
|
|
|
|
{aggregatedRanges.changes.map(
|
|
|
|
change =>
|
|
|
|
positions.has(change.id) && (
|
|
|
|
<ReviewPanelChange
|
|
|
|
key={change.id}
|
|
|
|
change={change}
|
|
|
|
top={positions.get(change.id)}
|
|
|
|
aggregate={aggregatedRanges.aggregates.get(change.id)}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
|
|
|
|
{aggregatedRanges.comments.map(
|
|
|
|
comment =>
|
|
|
|
positions.has(comment.id) && (
|
|
|
|
<ReviewPanelComment
|
|
|
|
key={comment.id}
|
|
|
|
comment={comment}
|
|
|
|
top={positions.get(comment.id)}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
2024-08-12 05:50:54 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default memo(ReviewPanelCurrentFile)
|