2024-10-21 04:55:41 -04:00
|
|
|
import {
|
|
|
|
Dispatch,
|
|
|
|
FC,
|
|
|
|
SetStateAction,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
2024-10-23 06:14:16 -04:00
|
|
|
useMemo,
|
2024-10-21 04:55:41 -04:00
|
|
|
useState,
|
|
|
|
} from 'react'
|
2024-09-16 04:40:59 -04:00
|
|
|
import ReactDOM from 'react-dom'
|
|
|
|
import MaterialIcon from '@/shared/components/material-icon'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import {
|
|
|
|
useCodeMirrorStateContext,
|
|
|
|
useCodeMirrorViewContext,
|
2024-09-27 06:31:31 -04:00
|
|
|
} from '@/features/source-editor/components/codemirror-context'
|
2024-09-16 04:40:59 -04:00
|
|
|
import {
|
|
|
|
buildAddNewCommentRangeEffect,
|
2024-10-23 06:14:16 -04:00
|
|
|
reviewTooltipStateField,
|
|
|
|
} from '@/features/source-editor/extensions/review-tooltip'
|
2024-10-25 09:04:55 -04:00
|
|
|
import { EditorView, getTooltip } from '@codemirror/view'
|
2024-09-16 04:40:59 -04:00
|
|
|
import useViewerPermissions from '@/shared/hooks/use-viewer-permissions'
|
|
|
|
import usePreviousValue from '@/shared/hooks/use-previous-value'
|
2024-10-10 09:13:01 -04:00
|
|
|
import { useLayoutContext } from '@/shared/context/layout-context'
|
|
|
|
import { useReviewPanelViewActionsContext } from '../context/review-panel-view-context'
|
2024-10-23 06:14:16 -04:00
|
|
|
import {
|
|
|
|
useRangesActionsContext,
|
|
|
|
useRangesContext,
|
|
|
|
} from '../context/ranges-context'
|
|
|
|
import { isInsertOperation } from '@/utils/operations'
|
2024-10-25 09:04:55 -04:00
|
|
|
import { isCursorNearViewportEdge } from '@/features/source-editor/utils/is-cursor-near-edge'
|
2024-09-16 04:40:59 -04:00
|
|
|
|
2024-10-10 08:50:27 -04:00
|
|
|
const ReviewTooltipMenu: FC = () => {
|
2024-09-16 04:40:59 -04:00
|
|
|
const state = useCodeMirrorStateContext()
|
|
|
|
const view = useCodeMirrorViewContext()
|
|
|
|
const isViewer = useViewerPermissions()
|
|
|
|
const [show, setShow] = useState(true)
|
|
|
|
|
2024-10-23 06:14:16 -04:00
|
|
|
const tooltipState = state.field(reviewTooltipStateField, false)?.tooltip
|
2024-09-16 04:40:59 -04:00
|
|
|
const previousTooltipState = usePreviousValue(tooltipState)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (tooltipState !== null && previousTooltipState === null) {
|
|
|
|
setShow(true)
|
|
|
|
}
|
|
|
|
}, [tooltipState, previousTooltipState])
|
|
|
|
|
|
|
|
if (isViewer || !show || !tooltipState) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const tooltipView = getTooltip(view, tooltipState)
|
|
|
|
|
|
|
|
if (!tooltipView) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReactDOM.createPortal(
|
2024-10-10 08:50:27 -04:00
|
|
|
<ReviewTooltipMenuContent setShow={setShow} />,
|
2024-09-16 04:40:59 -04:00
|
|
|
tooltipView.dom
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-10 08:50:27 -04:00
|
|
|
const ReviewTooltipMenuContent: FC<{
|
2024-09-16 04:40:59 -04:00
|
|
|
setShow: Dispatch<SetStateAction<boolean>>
|
|
|
|
}> = ({ setShow }) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const view = useCodeMirrorViewContext()
|
|
|
|
const state = useCodeMirrorStateContext()
|
2024-10-10 09:13:01 -04:00
|
|
|
const { setReviewPanelOpen } = useLayoutContext()
|
|
|
|
const { setView } = useReviewPanelViewActionsContext()
|
2024-10-23 06:14:16 -04:00
|
|
|
const ranges = useRangesContext()
|
|
|
|
const { acceptChanges, rejectChanges } = useRangesActionsContext()
|
2024-09-16 04:40:59 -04:00
|
|
|
|
2024-10-21 04:55:41 -04:00
|
|
|
const addComment = useCallback(() => {
|
2024-10-10 09:13:01 -04:00
|
|
|
setReviewPanelOpen(true)
|
|
|
|
setView('cur_file')
|
2024-09-16 04:40:59 -04:00
|
|
|
|
2024-10-25 09:04:55 -04:00
|
|
|
const commentPos = state.selection.main.anchor
|
|
|
|
const effects = isCursorNearViewportEdge(view, commentPos)
|
|
|
|
? [
|
|
|
|
buildAddNewCommentRangeEffect(state.selection.main),
|
|
|
|
EditorView.scrollIntoView(commentPos, { y: 'center' }),
|
|
|
|
]
|
|
|
|
: [buildAddNewCommentRangeEffect(state.selection.main)]
|
|
|
|
|
|
|
|
view.dispatch({ effects })
|
2024-09-16 04:40:59 -04:00
|
|
|
setShow(false)
|
2024-10-21 04:55:41 -04:00
|
|
|
}, [setReviewPanelOpen, setView, setShow, view, state.selection.main])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('add-new-review-comment', addComment)
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('add-new-review-comment', addComment)
|
|
|
|
}
|
|
|
|
}, [addComment])
|
2024-09-16 04:40:59 -04:00
|
|
|
|
2024-10-23 06:14:16 -04:00
|
|
|
const changeIdsInSelection = useMemo(() => {
|
|
|
|
return (ranges?.changes ?? [])
|
|
|
|
.filter(({ op }) => {
|
|
|
|
const opFrom = op.p
|
|
|
|
const opLength = isInsertOperation(op) ? op.i.length : 0
|
|
|
|
const opTo = opFrom + opLength
|
|
|
|
const selection = state.selection.main
|
|
|
|
return opFrom >= selection.from && opTo <= selection.to
|
|
|
|
})
|
|
|
|
.map(({ id }) => id)
|
|
|
|
}, [ranges, state.selection.main])
|
|
|
|
|
|
|
|
const acceptChangesHandler = useCallback(() => {
|
|
|
|
acceptChanges(...changeIdsInSelection)
|
|
|
|
}, [acceptChanges, changeIdsInSelection])
|
|
|
|
|
|
|
|
const rejectChangesHandler = useCallback(() => {
|
|
|
|
rejectChanges(...changeIdsInSelection)
|
|
|
|
}, [rejectChanges, changeIdsInSelection])
|
|
|
|
|
|
|
|
const showChangesButtons = changeIdsInSelection.length > 0
|
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
return (
|
2024-10-10 08:50:27 -04:00
|
|
|
<div className="review-tooltip-menu">
|
|
|
|
<button
|
|
|
|
className="review-tooltip-menu-button review-tooltip-add-comment-button"
|
2024-10-21 04:55:41 -04:00
|
|
|
onClick={addComment}
|
2024-10-10 08:50:27 -04:00
|
|
|
>
|
|
|
|
<MaterialIcon type="chat" />
|
|
|
|
{t('add_comment')}
|
|
|
|
</button>
|
2024-10-23 06:14:16 -04:00
|
|
|
{showChangesButtons && (
|
|
|
|
<>
|
|
|
|
<div className="review-tooltip-menu-divider" />
|
|
|
|
<button
|
|
|
|
className="review-tooltip-menu-button"
|
|
|
|
onClick={acceptChangesHandler}
|
|
|
|
>
|
|
|
|
<MaterialIcon type="check" />
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
className="review-tooltip-menu-button"
|
|
|
|
onClick={rejectChangesHandler}
|
|
|
|
>
|
|
|
|
<MaterialIcon type="clear" />
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)}
|
2024-10-10 08:50:27 -04:00
|
|
|
</div>
|
2024-09-16 04:40:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-10 08:50:27 -04:00
|
|
|
export default ReviewTooltipMenu
|