import { useState, useRef, useEffect, useCallback, useMemo } from 'react'; import { Button } from '@/components/ui/button'; import { FileSearchTextarea } from '@/components/ui/file-search-textarea'; import { useReview, type ReviewDraft } from '@/contexts/ReviewProvider'; import { Scope, useKeyExit, useKeySubmitComment } from '@/keyboard'; import { useHotkeysContext } from 'react-hotkeys-hook'; interface CommentWidgetLineProps { draft: ReviewDraft; widgetKey: string; onSave: () => void; onCancel: () => void; projectId?: string; } export function CommentWidgetLine({ draft, widgetKey, onSave, onCancel, projectId, }: CommentWidgetLineProps) { const { setDraft, addComment } = useReview(); const [value, setValue] = useState(draft.text); const textareaRef = useRef(null); const { enableScope, disableScope } = useHotkeysContext(); useEffect(() => { textareaRef.current?.focus(); }, []); useEffect(() => { enableScope(Scope.EDIT_COMMENT); return () => { disableScope(Scope.EDIT_COMMENT); }; }, [enableScope, disableScope]); const handleCancel = useCallback(() => { setDraft(widgetKey, null); onCancel(); }, [setDraft, widgetKey, onCancel]); const handleSave = useCallback(() => { if (value.trim()) { addComment({ filePath: draft.filePath, side: draft.side, lineNumber: draft.lineNumber, text: value.trim(), codeLine: draft.codeLine, }); } setDraft(widgetKey, null); onSave(); }, [value, draft, setDraft, widgetKey, onSave, addComment]); const handleSubmitShortcut = useCallback( (e?: KeyboardEvent) => { e?.preventDefault(); handleSave(); }, [handleSave] ); const exitOptions = useMemo( () => ({ scope: Scope.EDIT_COMMENT, }), [] ); useKeyExit(handleCancel, exitOptions); useKeySubmitComment(handleSubmitShortcut, { scope: Scope.EDIT_COMMENT, enableOnFormTags: ['textarea', 'TEXTAREA'], when: value.trim() !== '', preventDefault: true, }); return (
); }