'use client'; import { useState, useCallback, useRef, useEffect } from 'react'; import { IconPlus } from '@tabler/icons-react'; import { Button } from '@kandev/ui/button'; import { Textarea } from '@kandev/ui/textarea'; import { cn } from '@/lib/utils'; type EditorCommentPopoverProps = { selectedText: string; lineRange: { start: number; end: number }; position: { x: number; y: number }; onSubmit: (comment: string) => void; onClose: () => void; }; export function EditorCommentPopover({ selectedText, lineRange, position, onSubmit, onClose, }: EditorCommentPopoverProps) { const [comment, setComment] = useState(''); const textareaRef = useRef(null); const popoverRef = useRef(null); // Focus textarea on mount useEffect(() => { textareaRef.current?.focus(); }, []); // Close on click outside useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (popoverRef.current && !popoverRef.current.contains(e.target as Node)) { onClose(); } }; // Delay adding listener to avoid immediate close from selection click const timer = setTimeout(() => { document.addEventListener('mousedown', handleClickOutside); }, 100); return () => { clearTimeout(timer); document.removeEventListener('mousedown', handleClickOutside); }; }, [onClose]); // Close on Escape useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.stopPropagation(); onClose(); } }; document.addEventListener('keydown', handleKeyDown, true); return () => document.removeEventListener('keydown', handleKeyDown, true); }, [onClose]); const handleSubmit = useCallback(() => { if (!comment.trim()) return; onSubmit(comment.trim()); }, [comment, onSubmit]); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); handleSubmit(); } }, [handleSubmit] ); const isDisabled = !comment.trim(); // Truncate code preview const previewText = selectedText.length > 100 ? selectedText.slice(0, 100).trim() + '…' : selectedText; // Format line range const lineRangeText = lineRange.start === lineRange.end ? `Line ${lineRange.start}` : `Lines ${lineRange.start}-${lineRange.end}`; // Calculate position - anchor at bottom-right of selection, adjust if would overflow const popoverWidth = 384; // w-96 = 24rem = 384px const popoverHeight = 220; // approximate height of popover const gap = 8; // Start at bottom-right of selection let left = position.x + gap; let top = position.y + gap; // If would overflow right, shift left if (left + popoverWidth > window.innerWidth - 16) { left = Math.max(16, window.innerWidth - popoverWidth - 16); } // If would overflow bottom, position above the selection instead if (top + popoverHeight > window.innerHeight - 16) { top = Math.max(16, position.y - popoverHeight - gap); } return (
{/* Line range badge */}
{lineRangeText}
{/* Code preview */}
        {previewText}
      
{/* Comment input */}