import { FC, FormEventHandler, useCallback, useState } from 'react' import { useCodeMirrorStateContext, useCodeMirrorViewContext, } from '@/features/source-editor/components/codemirror-editor' import { EditorSelection } from '@codemirror/state' import { useTranslation } from 'react-i18next' import { useThreadsActionsContext } from '../context/threads-context' export const ReviewPanelAddComment: FC = () => { const { t } = useTranslation() const view = useCodeMirrorViewContext() // eslint-disable-next-line no-unused-vars const _state = useCodeMirrorStateContext() const { addComment } = useThreadsActionsContext() const [error, setError] = useState() const [showForm, setShowForm] = useState(false) const handleSubmit = useCallback( event => { event.preventDefault() const { from, to } = view.state.selection.main const content = view.state.sliceDoc(from, to) const formData = new FormData(event.target as HTMLFormElement) const message = formData.get('message') as string addComment(from, content, message).catch(setError) view.dispatch({ selection: EditorSelection.cursor(view.state.selection.main.anchor), }) }, [addComment, view] ) const handleElement = useCallback((element: HTMLElement | null) => { if (element) { element.dispatchEvent(new Event('review-panel:position')) } }, []) if (!showForm) { return } return (
{/* eslint-disable-next-line jsx-a11y/no-autofocus */}