import React, { useState, useRef, useEffect } from 'react'; import { Annotation, AnnotationType, Block, type CodeAnnotation, type EditorAnnotation } from '../types'; import { isCurrentUser } from '../utils/identity'; import { ImageThumbnail } from './ImageThumbnail'; import { EditorAnnotationCard } from './EditorAnnotationCard'; import { useIsMobile } from '../hooks/useIsMobile'; import { OverlayScrollArea } from './OverlayScrollArea'; interface PanelProps { isOpen: boolean; annotations: Annotation[]; blocks: Block[]; onSelect: (id: string) => void; onDelete: (id: string) => void; onEdit?: (id: string, updates: Partial) => void; selectedId: string | null; codeAnnotations?: CodeAnnotation[]; onSelectCodeAnnotation?: (id: string) => void; onDeleteCodeAnnotation?: (id: string) => void; onEditCodeAnnotation?: (id: string, updates: Partial) => void; sharingEnabled?: boolean; width?: number; editorAnnotations?: EditorAnnotation[]; onDeleteEditorAnnotation?: (id: string) => void; onClose?: () => void; onQuickCopy?: () => Promise; onShare?: () => void; otherFileAnnotations?: { count: number; files: number }; onOtherFileAnnotationsClick?: () => void; } export const AnnotationPanel: React.FC = ({ isOpen, annotations, blocks, onSelect, onDelete, onEdit, selectedId, codeAnnotations = [], onSelectCodeAnnotation, onDeleteCodeAnnotation, onEditCodeAnnotation, sharingEnabled = true, width, editorAnnotations, onDeleteEditorAnnotation, onClose, onQuickCopy, onShare, otherFileAnnotations, onOtherFileAnnotationsClick, }) => { const isMobile = useIsMobile(); const [copiedText, setCopiedText] = useState(false); const listRef = useRef(null); const sortedAnnotations = [...annotations].sort((a, b) => a.createdA - b.createdA); const sortedCodeAnnotations = [...codeAnnotations].sort((a, b) => a.createdAt - b.createdAt); const timelineEntries = [ ...sortedAnnotations.map(annotation => ({ kind: 'plan' as const, ts: annotation.createdA, annotation })), ...sortedCodeAnnotations.map(annotation => ({ kind: 'code' as const, ts: annotation.createdAt, annotation })), ].sort((a, b) => a.ts - b.ts); const totalCount = annotations.length + codeAnnotations.length + (editorAnnotations?.length ?? 0); // Scroll selected annotation card into view useEffect(() => { if (!selectedId || !listRef.current) return; const card = listRef.current.querySelector(`[data-annotation-id="${selectedId}"]`); if (card) { card.scrollIntoView({ behavior: 'smooth', block: 'center' }); } }, [selectedId]); if (!isOpen) return null; const panel = ( ); if (isMobile) { return ( <>
{panel} ); } return panel; }; function formatTimestamp(ts: number): string { const now = Date.now(); const diff = now - ts; const seconds = Math.floor(diff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); if (seconds < 60) return 'now'; if (minutes < 60) return `${minutes}m`; if (hours < 24) return `${hours}h`; if (days < 7) return `${days}d`; return new Date(ts).toLocaleDateString(undefined, { month: 'short', day: 'numeric' }); } const AnnotationCard: React.FC<{ annotation: Annotation; isSelected: boolean; onSelect: () => void; onDelete: () => void; onEdit?: (updates: Partial) => void; }> = ({ annotation, isSelected, onSelect, onDelete, onEdit }) => { const [isEditing, setIsEditing] = useState(false); const [editText, setEditText] = useState(annotation.text || ''); const textareaRef = useRef(null); useEffect(() => { if (isEditing && textareaRef.current) { textareaRef.current.focus(); textareaRef.current.select(); } }, [isEditing]); // Update editText when annotation.text changes useEffect(() => { if (!isEditing) { setEditText(annotation.text || ''); } }, [annotation.text, isEditing]); const handleStartEdit = (e: React.MouseEvent) => { e.stopPropagation(); setEditText(annotation.text || ''); setIsEditing(true); }; const handleSaveEdit = () => { if (onEdit) { onEdit({ text: editText }); } setIsEditing(false); }; const handleCancelEdit = () => { setEditText(annotation.text || ''); setIsEditing(false); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && (e.metaKey || e.ctrlKey) && !e.nativeEvent.isComposing) { e.preventDefault(); handleSaveEdit(); } else if (e.key === 'Escape') { e.preventDefault(); handleCancelEdit(); } }; const typeConfig = { [AnnotationType.DELETION]: { label: 'Delete', color: 'text-destructive', bg: 'bg-destructive/10', icon: ( ) }, [AnnotationType.COMMENT]: { label: 'Comment', color: 'text-accent', bg: 'bg-accent/10', icon: ( ) }, [AnnotationType.GLOBAL_COMMENT]: { label: 'Global', color: 'text-secondary', bg: 'bg-secondary/10', icon: ( ) } }; // Fallback for unknown types (forward compatibility) const config = typeConfig[annotation.type] || { label: 'Note', color: 'text-muted-foreground', bg: 'bg-muted/50', icon: ( ) }; return (
{/* Author */} {annotation.author && (
{annotation.author}{isCurrentUser(annotation.author) && ' (me)'}
)} {/* Type Badge + Timestamp + Actions */}
{config.icon} {config.label}
{annotation.diffContext && ( diff )} {formatTimestamp(annotation.createdA)}
{onEdit && annotation.type !== AnnotationType.DELETION && !isEditing && ( )}
{/* Global Comment - show text directly */} {annotation.type === AnnotationType.GLOBAL_COMMENT ? ( isEditing ? (