'use client' import { cn, formatDate } from '@/lib/utils' import { Bot, User, FileText, Download, Play, Pause, Trash2, X } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import type { Message } from '@/types' import { useState, useRef, useCallback } from 'react' import { ImageViewer } from '@/components/ui/ImageViewer' interface MessageBubbleProps { message: Message isMe: boolean isAI: boolean isSequence: boolean showDate: boolean dateLabel: string onDelete?: (messageId: string) => void onImageClick?: (src: string, alt: string) => void } // ImageModal removed in favor of global ImageViewer /** * Renders media content (audio, image, video, file) in message bubble */ function MediaContent({ message, isMe, onImageClick }: { message: Message; isMe: boolean; onImageClick?: (src: string, alt: string) => void }) { const [isPlaying, setIsPlaying] = useState(false) const audioRef = useRef(null) if (!message.media_url) return null const mediaType = message.media_type?.toLowerCase() || '' // Audio player if (mediaType.includes('audio') || mediaType === 'voice') { return (
Голосовое сообщение
) } // ... inside MediaContent function ... // Image if (mediaType.includes('image')) { const handleOpen = (e: any) => { e.stopPropagation(); e.preventDefault(); onImageClick?.(message.media_url!, message.media_name || 'Image'); }; return (
e.stopPropagation()} onTouchMove={(e) => e.stopPropagation()} onTouchEnd={(e) => e.stopPropagation()} >
) } // Video if (mediaType.includes('video')) { return (
) } // Document/File return (
{message.media_name || 'Файл'}
Нажмите для скачивания
) } export function MessageBubble({ message, isMe, isAI, isSequence, showDate, dateLabel, onDelete, onImageClick, }: MessageBubbleProps) { const hasMedia = !!message.media_url const hasText = !!message.content?.trim() // Context menu state const [showContextMenu, setShowContextMenu] = useState(false) const longPressTimer = useRef(null) const touchStartPos = useRef<{ x: number, y: number } | null>(null) // Can delete: only own human messages const canDelete = isMe && !isAI && onDelete const handleContextMenu = useCallback((e: React.MouseEvent) => { if (!canDelete) return e.preventDefault() setShowContextMenu(true) }, [canDelete]) const handleTouchStart = useCallback((e: React.TouchEvent) => { if (!canDelete) return const touch = e.touches[0] touchStartPos.current = { x: touch.clientX, y: touch.clientY } longPressTimer.current = setTimeout(() => { // Haptic feedback for mobile if (navigator.vibrate) { navigator.vibrate(50) } setShowContextMenu(true) }, 400) // 400ms for faster long press response }, [canDelete]) const handleTouchMove = useCallback((e: React.TouchEvent) => { if (!touchStartPos.current) return const touch = e.touches[0] const dx = Math.abs(touch.clientX - touchStartPos.current.x) const dy = Math.abs(touch.clientY - touchStartPos.current.y) // Cancel long press if moved more than 10px if (dx > 10 || dy > 10) { if (longPressTimer.current) clearTimeout(longPressTimer.current) } }, []) const handleTouchEnd = useCallback(() => { if (longPressTimer.current) clearTimeout(longPressTimer.current) touchStartPos.current = null }, []) const handleDelete = useCallback(() => { if (onDelete) { onDelete(message.id) } setShowContextMenu(false) }, [onDelete, message.id]) return (
{showDate && (
{dateLabel}
)}
{/* Avatar */}
{isMe ? (isAI ? : ) : }
{/* Message Bubble */}
{/* Text content */} {hasText && message.content} {/* Media content (audio, image, video, file) */}
{!isSequence && (
{formatDate(message.created_at)} {/* Delete button - only for own non-AI messages */} {isMe && !isAI && onDelete && ( )}
)}
{/* Context Menu Popup */} {showContextMenu && canDelete && ( <> {/* Backdrop */}
setShowContextMenu(false)} /> {/* Menu */}
)}
) }