2024-09-16 04:40:59 -04:00
|
|
|
import { FC, FormEventHandler, useCallback, useState, useRef } from 'react'
|
2024-08-12 05:50:54 -04:00
|
|
|
import {
|
|
|
|
useCodeMirrorStateContext,
|
|
|
|
useCodeMirrorViewContext,
|
2024-09-27 06:31:31 -04:00
|
|
|
} from '@/features/source-editor/components/codemirror-context'
|
2024-08-12 05:50:54 -04:00
|
|
|
import { EditorSelection } from '@codemirror/state'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { useThreadsActionsContext } from '../context/threads-context'
|
2024-09-16 04:40:59 -04:00
|
|
|
import { removeNewCommentRangeEffect } from '@/features/source-editor/extensions/add-comment'
|
|
|
|
import useSubmittableTextInput from '../hooks/use-submittable-text-input'
|
|
|
|
import AutoExpandingTextArea from '@/shared/components/auto-expanding-text-area'
|
|
|
|
import { Button } from 'react-bootstrap'
|
|
|
|
import { ReviewPanelEntry } from './review-panel-entry'
|
|
|
|
import { ThreadId } from '../../../../../types/review-panel/review-panel'
|
|
|
|
import { Decoration } from '@codemirror/view'
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
export const ReviewPanelAddComment: FC<{
|
2024-09-26 10:13:54 -04:00
|
|
|
docId: string
|
2024-09-16 04:40:59 -04:00
|
|
|
from: number
|
|
|
|
to: number
|
|
|
|
value: Decoration
|
|
|
|
top: number | undefined
|
2024-09-26 10:13:54 -04:00
|
|
|
}> = ({ from, to, value, top, docId }) => {
|
2024-08-12 05:50:54 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
const view = useCodeMirrorViewContext()
|
2024-09-16 04:40:59 -04:00
|
|
|
const state = useCodeMirrorStateContext()
|
2024-08-12 05:50:54 -04:00
|
|
|
const { addComment } = useThreadsActionsContext()
|
|
|
|
const [error, setError] = useState<Error>()
|
2024-09-16 04:40:59 -04:00
|
|
|
const [submitting, setSubmitting] = useState(false)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const handleClose = useCallback(() => {
|
|
|
|
view.dispatch({
|
|
|
|
effects: removeNewCommentRangeEffect.of(value),
|
|
|
|
})
|
|
|
|
}, [view, value])
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const submitForm = useCallback(
|
|
|
|
message => {
|
|
|
|
setSubmitting(true)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const content = view.state.sliceDoc(from, to)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
addComment(from, content, message)
|
|
|
|
.catch(setError)
|
|
|
|
.finally(() => setSubmitting(false))
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
view.dispatch({
|
|
|
|
selection: EditorSelection.cursor(view.state.selection.main.anchor),
|
|
|
|
})
|
2024-09-16 04:40:59 -04:00
|
|
|
|
|
|
|
handleClose()
|
2024-08-12 05:50:54 -04:00
|
|
|
},
|
2024-09-16 04:40:59 -04:00
|
|
|
[addComment, view, handleClose, from, to]
|
2024-08-12 05:50:54 -04:00
|
|
|
)
|
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const { handleChange, handleKeyPress, content } =
|
|
|
|
useSubmittableTextInput(submitForm)
|
|
|
|
|
|
|
|
const handleBlur = useCallback(() => {
|
|
|
|
if (content === '') {
|
|
|
|
handleClose()
|
|
|
|
}
|
|
|
|
}, [content, handleClose])
|
|
|
|
|
|
|
|
const handleSubmit = useCallback<FormEventHandler>(
|
|
|
|
event => {
|
|
|
|
event.preventDefault()
|
|
|
|
submitForm(content)
|
|
|
|
},
|
|
|
|
[submitForm, content]
|
|
|
|
)
|
|
|
|
|
|
|
|
// We only ever want to focus the element once
|
|
|
|
const hasBeenFocused = useRef(false)
|
|
|
|
|
|
|
|
// Auto-focus the textarea once the element has been correctly positioned.
|
|
|
|
// We cannot use the autofocus attribute as we need to wait until the parent element
|
|
|
|
// has been positioned (with the "top" attribute) to avoid scrolling to the initial
|
|
|
|
// position of the element
|
|
|
|
const observerCallback = useCallback(mutationList => {
|
|
|
|
if (hasBeenFocused.current) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const mutation of mutationList) {
|
|
|
|
if (mutation.target.style.top) {
|
|
|
|
const textArea = mutation.target.getElementsByTagName('textarea')[0]
|
|
|
|
if (textArea) {
|
|
|
|
textArea.focus()
|
|
|
|
hasBeenFocused.current = true
|
|
|
|
}
|
|
|
|
}
|
2024-08-12 05:50:54 -04:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const handleElement = useCallback(
|
|
|
|
(element: HTMLElement | null) => {
|
|
|
|
if (element) {
|
|
|
|
element.dispatchEvent(new Event('review-panel:position'))
|
|
|
|
|
|
|
|
const observer = new MutationObserver(observerCallback)
|
|
|
|
const entryWrapper = element.closest('.review-panel-entry')
|
|
|
|
if (entryWrapper) {
|
|
|
|
observer.observe(entryWrapper, {
|
|
|
|
attributes: true,
|
|
|
|
attributeFilter: ['style'],
|
|
|
|
})
|
|
|
|
return () => observer.disconnect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[observerCallback]
|
|
|
|
)
|
2024-08-12 05:50:54 -04:00
|
|
|
|
|
|
|
return (
|
2024-09-16 04:40:59 -04:00
|
|
|
<ReviewPanelEntry
|
2024-09-26 10:13:54 -04:00
|
|
|
docId={docId}
|
2024-09-16 04:40:59 -04:00
|
|
|
top={top}
|
|
|
|
position={from}
|
|
|
|
op={{
|
|
|
|
p: from,
|
|
|
|
c: state.sliceDoc(from, to),
|
|
|
|
t: value.spec.id as ThreadId,
|
|
|
|
}}
|
|
|
|
selectLineOnFocus={false}
|
2024-08-12 05:50:54 -04:00
|
|
|
>
|
2024-09-16 04:40:59 -04:00
|
|
|
<form
|
|
|
|
className="review-panel-entry-content"
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
ref={handleElement}
|
|
|
|
>
|
|
|
|
<AutoExpandingTextArea
|
|
|
|
name="message"
|
|
|
|
className="review-panel-add-comment-textarea"
|
|
|
|
onChange={handleChange}
|
|
|
|
onKeyDown={handleKeyPress}
|
|
|
|
placeholder={t('add_your_comment_here')}
|
|
|
|
value={content}
|
|
|
|
disabled={submitting}
|
|
|
|
/>
|
|
|
|
<div className="review-panel-add-comment-buttons">
|
|
|
|
<Button
|
|
|
|
bsSize="sm"
|
|
|
|
bsStyle={null}
|
|
|
|
className="review-panel-add-comment-cancel-button"
|
|
|
|
disabled={submitting}
|
|
|
|
onClick={handleClose}
|
|
|
|
>
|
|
|
|
{t('cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
bsSize="sm"
|
|
|
|
className="btn-primary"
|
|
|
|
disabled={content === '' || submitting}
|
|
|
|
>
|
|
|
|
{t('comment')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{error && <div>{error.message}</div>}
|
|
|
|
</form>
|
|
|
|
</ReviewPanelEntry>
|
2024-08-12 05:50:54 -04:00
|
|
|
)
|
|
|
|
}
|