2024-09-16 04:40:59 -04:00
|
|
|
import { Dispatch, FC, SetStateAction, useEffect, useState } from 'react'
|
|
|
|
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 {
|
|
|
|
addCommentStateField,
|
|
|
|
buildAddNewCommentRangeEffect,
|
|
|
|
} from '@/features/source-editor/extensions/add-comment'
|
|
|
|
import { getTooltip } from '@codemirror/view'
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
const tooltipState = state.field(addCommentStateField, false)?.tooltip
|
|
|
|
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-09-16 04:40:59 -04:00
|
|
|
|
|
|
|
const handleClick = () => {
|
2024-10-10 09:13:01 -04:00
|
|
|
setReviewPanelOpen(true)
|
|
|
|
setView('cur_file')
|
2024-09-16 04:40:59 -04:00
|
|
|
|
|
|
|
view.dispatch({
|
|
|
|
effects: buildAddNewCommentRangeEffect(state.selection.main),
|
|
|
|
})
|
|
|
|
setShow(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
|
|
|
<MaterialIcon type="chat" />
|
|
|
|
{t('add_comment')}
|
|
|
|
</button>
|
|
|
|
</div>
|
2024-09-16 04:40:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-10-10 08:50:27 -04:00
|
|
|
export default ReviewTooltipMenu
|