Spaces:
Running
Running
Download frontend/components/ChatWindow/MessageBubble.tsx from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 13.9 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/ChatWindow/MessageBubble.tsx
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/frontend/components/ChatWindow/MessageBubble.tsx
-
curl -L -o MessageBubble.tsx https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/ChatWindow/MessageBubble.tsx
13.9 kB
| '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<HTMLAudioElement>(null) | |
| if (!message.media_url) return null | |
| const mediaType = message.media_type?.toLowerCase() || '' | |
| // Audio player | |
| if (mediaType.includes('audio') || mediaType === 'voice') { | |
| return ( | |
| <div className="mt-2"> | |
| <div className={cn( | |
| "flex items-center gap-2 p-2 rounded-xl", | |
| isMe ? "bg-white/10" : "bg-black/20" | |
| )}> | |
| <button | |
| onClick={() => { | |
| if (audioRef.current) { | |
| if (isPlaying) { | |
| audioRef.current.pause() | |
| } else { | |
| audioRef.current.play() | |
| } | |
| setIsPlaying(!isPlaying) | |
| } | |
| }} | |
| className={cn( | |
| "w-10 h-10 rounded-full flex items-center justify-center transition-colors", | |
| isMe | |
| ? "bg-white/20 hover:bg-white/30" | |
| : "bg-indigo-500/20 hover:bg-indigo-500/30" | |
| )} | |
| > | |
| {isPlaying ? ( | |
| <Pause className="w-5 h-5" /> | |
| ) : ( | |
| <Play className="w-5 h-5 ml-0.5" /> | |
| )} | |
| </button> | |
| <div className="flex-1"> | |
| <div className="text-xs opacity-70">Голосовое сообщение</div> | |
| <audio | |
| ref={audioRef} | |
| src={message.media_url} | |
| onEnded={() => setIsPlaying(false)} | |
| className="hidden" | |
| /> | |
| {/* Waveform visualization placeholder */} | |
| <div className="h-6 flex items-center gap-0.5 mt-1"> | |
| {[...Array(20)].map((_, i) => ( | |
| <div | |
| key={i} | |
| className={cn( | |
| "w-1 rounded-full transition-all", | |
| isMe ? "bg-white/40" : "bg-indigo-400/40" | |
| )} | |
| style={{ | |
| height: `${8 + Math.random() * 16}px`, | |
| opacity: isPlaying ? 0.8 : 0.4 | |
| }} | |
| /> | |
| ))} | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| ) | |
| } | |
| // ... 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 ( | |
| <div | |
| className="mt-2" | |
| onTouchStart={(e) => e.stopPropagation()} | |
| onTouchMove={(e) => e.stopPropagation()} | |
| onTouchEnd={(e) => e.stopPropagation()} | |
| > | |
| <button | |
| type="button" | |
| className="block w-full text-left rounded-xl overflow-hidden cursor-pointer hover:opacity-90 transition-opacity touch-manipulation" | |
| onPointerDown={(e) => e.stopPropagation()} | |
| onTouchEnd={(e) => { | |
| e.stopPropagation(); | |
| handleOpen(e); | |
| }} | |
| onClick={handleOpen} | |
| > | |
| <img | |
| src={message.media_url} | |
| alt={message.media_name || 'Image'} | |
| className="w-full max-h-64 object-cover pointer-events-none" | |
| /> | |
| </button> | |
| </div> | |
| ) | |
| } | |
| // Video | |
| if (mediaType.includes('video')) { | |
| return ( | |
| <div className="mt-2"> | |
| <video | |
| src={message.media_url} | |
| controls | |
| className="rounded-xl max-w-full max-h-64" | |
| /> | |
| </div> | |
| ) | |
| } | |
| // Document/File | |
| return ( | |
| <div className="mt-2"> | |
| <a | |
| href={message.media_url} | |
| target="_blank" | |
| rel="noopener noreferrer" | |
| className={cn( | |
| "flex items-center gap-3 p-3 rounded-xl transition-colors", | |
| isMe | |
| ? "bg-white/10 hover:bg-white/20" | |
| : "bg-black/20 hover:bg-black/30" | |
| )} | |
| > | |
| <div className={cn( | |
| "w-10 h-10 rounded-lg flex items-center justify-center", | |
| isMe ? "bg-white/20" : "bg-indigo-500/20" | |
| )}> | |
| <FileText className="w-5 h-5" /> | |
| </div> | |
| <div className="flex-1 min-w-0"> | |
| <div className="text-sm font-medium truncate"> | |
| {message.media_name || 'Файл'} | |
| </div> | |
| <div className="text-xs opacity-60"> | |
| Нажмите для скачивания | |
| </div> | |
| </div> | |
| <Download className="w-4 h-4 opacity-60" /> | |
| </a> | |
| </div> | |
| ) | |
| } | |
| 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<NodeJS.Timeout | null>(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 ( | |
| <div> | |
| {showDate && ( | |
| <div className="flex items-center justify-center my-6"> | |
| <span className="text-[10px] font-bold text-slate-400 bg-white/10 px-3 py-1 rounded-full uppercase tracking-wide"> | |
| {dateLabel} | |
| </span> | |
| </div> | |
| )} | |
| <div | |
| className={cn( | |
| "flex w-full max-w-3xl mx-auto group relative", | |
| isMe ? "justify-end" : "justify-start", | |
| isSequence ? "mt-1" : "mt-4" | |
| )} | |
| onContextMenu={handleContextMenu} | |
| onTouchStart={handleTouchStart} | |
| onTouchMove={handleTouchMove} | |
| onTouchEnd={handleTouchEnd} | |
| > | |
| <div className={cn( | |
| "flex gap-2 sm:gap-3 max-w-[90%] sm:max-w-[75%]", | |
| isMe ? "flex-row-reverse" : "flex-row" | |
| )}> | |
| {/* Avatar */} | |
| <div className={cn( | |
| "w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0 mt-auto mb-1 transition-opacity", | |
| isMe ? "bg-indigo-600 text-white" : "bg-surface-elevated text-slate-400 border border-white/10", | |
| isSequence ? "opacity-0 w-0 overflow-hidden ml-0 mr-0" : "opacity-100" | |
| )}> | |
| {isMe ? (isAI ? <Bot className="w-4 h-4" /> : <User className="w-4 h-4" />) : <User className="w-4 h-4" />} | |
| </div> | |
| {/* Message Bubble */} | |
| <div className={cn( | |
| "px-4 py-2.5 text-sm leading-relaxed shadow-sm relative break-words select-none", | |
| "touch-callout-none", // We'll add this to globals.css | |
| isMe | |
| ? "bg-gradient-to-r from-indigo-600 to-violet-600 text-white rounded-2xl rounded-br-none shadow-[0_4px_15px_-3px_rgba(99,102,241,0.3)]" | |
| : "bg-[#1E2030] text-slate-200 border border-white/5 rounded-2xl rounded-bl-none", | |
| isSequence && isMe && "rounded-tr-md rounded-br-md", | |
| isSequence && !isMe && "rounded-tl-md rounded-bl-md", | |
| // If only media and no text, remove padding | |
| hasMedia && !hasText && "p-2" | |
| )}> | |
| {/* Text content */} | |
| {hasText && message.content} | |
| {/* Media content (audio, image, video, file) */} | |
| <MediaContent message={message} isMe={isMe} onImageClick={onImageClick} /> | |
| </div> | |
| {!isSequence && ( | |
| <div className={cn( | |
| "flex items-center gap-1 px-1", | |
| isMe ? "justify-end" : "justify-start" | |
| )}> | |
| <span className="text-[10px] text-slate-400"> | |
| {formatDate(message.created_at)} | |
| </span> | |
| {/* Delete button - only for own non-AI messages */} | |
| {isMe && !isAI && onDelete && ( | |
| <button | |
| onClick={() => onDelete(message.id)} | |
| className="opacity-0 group-hover:opacity-100 p-1 text-slate-500 hover:text-red-400 rounded transition-all" | |
| title="Удалить сообщение" | |
| > | |
| <Trash2 className="w-3 h-3" /> | |
| </button> | |
| )} | |
| </div> | |
| )} | |
| </div> | |
| </div> | |
| {/* Context Menu Popup */} | |
| {showContextMenu && canDelete && ( | |
| <> | |
| {/* Backdrop */} | |
| <div | |
| className="fixed inset-0 z-[100] bg-black/30" | |
| onClick={() => setShowContextMenu(false)} | |
| /> | |
| {/* Menu */} | |
| <div className={cn( | |
| "absolute z-[101] bg-[#1A1B26] border border-white/10 rounded-xl shadow-2xl py-2 min-w-[140px]", | |
| isMe ? "right-0" : "left-0", | |
| "bottom-full mb-2" | |
| )}> | |
| <button | |
| onClick={handleDelete} | |
| className="w-full px-4 py-3 text-sm text-red-400 hover:bg-red-500/10 active:bg-red-500/20 flex items-center gap-3 transition-colors" | |
| > | |
| <Trash2 className="w-5 h-5" /> | |
| Удалить | |
| </button> | |
| </div> | |
| </> | |
| )} | |
| </div> | |
| ) | |
| } | |