'use client'; import { useRef, useEffect } from 'react'; import { IconTrash } from '@tabler/icons-react'; import { Button } from '@kandev/ui/button'; import { cn } from '@/lib/utils'; import type { DiffComment } from '@/lib/diff/types'; type CommentViewPopoverProps = { comments: DiffComment[]; position: { x: number; y: number }; onDelete: (commentId: string) => void; onClose: () => void; }; export function CommentViewPopover({ comments, position, onDelete, onClose, }: CommentViewPopoverProps) { const popoverRef = useRef(null); // 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 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]); // Calculate position const popoverWidth = 350; const popoverHeight = 200; const gap = 8; 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 if (top + popoverHeight > window.innerHeight - 16) { top = Math.max(16, position.y - popoverHeight - gap); } // Format line range const formatLineRange = (start: number, end: number) => { return start === end ? `L${start}` : `L${start}-${end}`; }; return (
{/* Comments list */}
{comments.map((comment) => (
{/* Line range and delete */}
{formatLineRange(comment.startLine, comment.endLine)}
{/* Code snippet with scroll */} {comment.codeContent && (
                {comment.codeContent}
              
)} {/* Annotation */}

{comment.annotation}

))}
); }