Spaces:
Running
Running
File size: 13,876 Bytes
526e04d db343ea 526e04d 1ad4362 c0e9c2b 526e04d db2dc5e 8e132dd 526e04d cb528a9 db343ea 5b6905f 8e132dd 5b6905f cb528a9 5b6905f 502b64f 5b6905f e4d1303 65ad247 502b64f e4d1303 502b64f 65ad247 502b64f 65ad247 8e132dd 5b6905f 526e04d db2dc5e a2b2ffe 526e04d 5b6905f 1ad4362 3425727 1ad4362 3425727 1ad4362 526e04d 65ad247 526e04d 1ad4362 526e04d 1ad4362 526e04d d14e1f7 526e04d dc7e33a 526e04d abb8c3e 5b6905f abb8c3e 526e04d abb8c3e 1ad4362 abb8c3e 1ad4362 abb8c3e 1ad4362 abb8c3e 65ad247 abb8c3e 3425727 abb8c3e 3425727 abb8c3e 3425727 abb8c3e 3425727 abb8c3e 3425727 abb8c3e 526e04d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | '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>
)
}
|