Spaces:
Running
Running
axeronis commited on
Commit ·
cb528a9
1
Parent(s): 5439e6f
fix(ui): integrate ImageViewer, refactor voice logic, fix input padding
Browse files- frontend/components/ChatWindow.tsx +20 -7
- frontend/components/ChatWindow/ChatHeader.tsx +46 -28
- frontend/components/ChatWindow/MessageBubble.tsx +6 -27
- frontend/components/ChatWindow/MessageInput.tsx +130 -103
- frontend/components/MobileBottomNav.tsx +2 -2
- frontend/components/MobileNav.tsx +13 -3
- frontend/components/ui/ImageViewer.tsx +132 -0
frontend/components/ChatWindow.tsx
CHANGED
|
@@ -365,11 +365,24 @@ export function ChatWindow({
|
|
| 365 |
// ============================================================================
|
| 366 |
|
| 367 |
return (
|
| 368 |
-
<div
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
style={{
|
| 371 |
-
paddingTop: 'var(--header-height-mobile)',
|
| 372 |
-
paddingBottom: 'var(--footer-height-mobile)'
|
| 373 |
}}
|
| 374 |
>
|
| 375 |
{/* Background Atmosphere - Synced with Main Layout */}
|
|
@@ -395,10 +408,10 @@ export function ChatWindow({
|
|
| 395 |
|
| 396 |
{/* Messages Area */}
|
| 397 |
<div
|
| 398 |
-
className="flex-1 overflow-y-auto px-4 md:px-6 space-y-6 scrollbar-
|
| 399 |
style={{
|
| 400 |
WebkitOverflowScrolling: 'touch',
|
| 401 |
-
|
| 402 |
}}
|
| 403 |
>
|
| 404 |
{/* Top spacer to show content blurring behind header when scrolling */}
|
|
@@ -480,6 +493,6 @@ export function ChatWindow({
|
|
| 480 |
confirmText="Обучить AI"
|
| 481 |
isLoading={isTraining}
|
| 482 |
/>
|
| 483 |
-
</div>
|
| 484 |
)
|
| 485 |
}
|
|
|
|
| 365 |
// ============================================================================
|
| 366 |
|
| 367 |
return (
|
| 368 |
+
<motion.div
|
| 369 |
+
drag="x"
|
| 370 |
+
dragConstraints={{ left: 0, right: 0 }}
|
| 371 |
+
dragElastic={0.1}
|
| 372 |
+
onDragEnd={(_, info) => {
|
| 373 |
+
const threshold = 50 // Responsive threshold
|
| 374 |
+
if (info.offset.x > threshold) {
|
| 375 |
+
// Swipe Right -> Back to list
|
| 376 |
+
onBack?.()
|
| 377 |
+
} else if (info.offset.x < -threshold) {
|
| 378 |
+
// Swipe Left -> Open AI Analysis
|
| 379 |
+
onOpenMobileDrawer?.()
|
| 380 |
+
}
|
| 381 |
+
}}
|
| 382 |
+
className="flex flex-col h-full bg-transparent font-['Inter'] relative overflow-hidden touch-pan-y"
|
| 383 |
style={{
|
| 384 |
+
// paddingTop: 'var(--header-height-mobile)', // REMOVED: layout.tsx already adds this
|
| 385 |
+
// paddingBottom: 'var(--footer-height-mobile)' // REMOVED: layout.tsx already adds this
|
| 386 |
}}
|
| 387 |
>
|
| 388 |
{/* Background Atmosphere - Synced with Main Layout */}
|
|
|
|
| 408 |
|
| 409 |
{/* Messages Area */}
|
| 410 |
<div
|
| 411 |
+
className="flex-1 overflow-y-auto px-4 md:px-6 space-y-6 scrollbar-thin scrollbar-thumb-white/10"
|
| 412 |
style={{
|
| 413 |
WebkitOverflowScrolling: 'touch',
|
| 414 |
+
willChange: 'transform'
|
| 415 |
}}
|
| 416 |
>
|
| 417 |
{/* Top spacer to show content blurring behind header when scrolling */}
|
|
|
|
| 493 |
confirmText="Обучить AI"
|
| 494 |
isLoading={isTraining}
|
| 495 |
/>
|
| 496 |
+
</motion.div>
|
| 497 |
)
|
| 498 |
}
|
frontend/components/ChatWindow/ChatHeader.tsx
CHANGED
|
@@ -33,9 +33,9 @@ export function ChatHeader({
|
|
| 33 |
onTrainAI,
|
| 34 |
}: ChatHeaderProps) {
|
| 35 |
return (
|
| 36 |
-
<div className="h-[52px] md:h-16 shrink-0 flex items-center justify-between px-3 md:px-6 relative z-[50] bg-[#050510]/
|
| 37 |
{/* Aurora Background Atmosphere - Synced */}
|
| 38 |
-
<div className="absolute inset-0 pointer-events-none overflow-hidden -z-10 bg-gradient-to-r from-indigo-500/
|
| 39 |
|
| 40 |
{/* Left: Lead Info */}
|
| 41 |
<div className="flex items-center gap-2 md:gap-3 overflow-hidden">
|
|
@@ -43,7 +43,7 @@ export function ChatHeader({
|
|
| 43 |
<div className="md:hidden">
|
| 44 |
<button
|
| 45 |
onClick={onBack}
|
| 46 |
-
className="p-
|
| 47 |
>
|
| 48 |
<ArrowLeft className="w-5 h-5" />
|
| 49 |
</button>
|
|
@@ -86,41 +86,59 @@ export function ChatHeader({
|
|
| 86 |
Записать
|
| 87 |
</button>
|
| 88 |
|
| 89 |
-
{/* AI Toggle Switch -
|
| 90 |
<div className="flex items-center gap-2">
|
| 91 |
<button
|
| 92 |
onClick={onToggleAI}
|
| 93 |
disabled={isTogglingAI}
|
| 94 |
className={cn(
|
| 95 |
-
"relative w-
|
| 96 |
isAIActive
|
| 97 |
-
? "bg-indigo-600/
|
| 98 |
-
: "bg-
|
| 99 |
isTogglingAI && "opacity-80"
|
| 100 |
)}
|
| 101 |
title={isAIActive ? 'AI Активен' : 'AI Отключен'}
|
| 102 |
>
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
</div>
|
| 125 |
</button>
|
| 126 |
</div>
|
|
|
|
| 33 |
onTrainAI,
|
| 34 |
}: ChatHeaderProps) {
|
| 35 |
return (
|
| 36 |
+
<div className="h-[52px] md:h-16 shrink-0 flex items-center justify-between px-3 md:px-6 relative z-[50] bg-[#050510]/60 backdrop-blur-xl border-b border-white/5">
|
| 37 |
{/* Aurora Background Atmosphere - Synced */}
|
| 38 |
+
<div className="absolute inset-0 pointer-events-none overflow-hidden -z-10 bg-gradient-to-r from-indigo-500/5 to-purple-500/5 opacity-30 blur-xl" />
|
| 39 |
|
| 40 |
{/* Left: Lead Info */}
|
| 41 |
<div className="flex items-center gap-2 md:gap-3 overflow-hidden">
|
|
|
|
| 43 |
<div className="md:hidden">
|
| 44 |
<button
|
| 45 |
onClick={onBack}
|
| 46 |
+
className="p-1.5 -ml-1 text-slate-400 hover:text-white hover:bg-white/10 rounded-full transition-colors"
|
| 47 |
>
|
| 48 |
<ArrowLeft className="w-5 h-5" />
|
| 49 |
</button>
|
|
|
|
| 86 |
Записать
|
| 87 |
</button>
|
| 88 |
|
| 89 |
+
{/* AI Toggle Switch - FIXED UI */}
|
| 90 |
<div className="flex items-center gap-2">
|
| 91 |
<button
|
| 92 |
onClick={onToggleAI}
|
| 93 |
disabled={isTogglingAI}
|
| 94 |
className={cn(
|
| 95 |
+
"relative w-11 h-6 rounded-full transition-all duration-500 flex-shrink-0 border",
|
| 96 |
isAIActive
|
| 97 |
+
? "bg-indigo-600/30 border-indigo-500/50 shadow-[0_0_15px_rgba(99,102,241,0.2)]"
|
| 98 |
+
: "bg-white/5 border-white/20",
|
| 99 |
isTogglingAI && "opacity-80"
|
| 100 |
)}
|
| 101 |
title={isAIActive ? 'AI Активен' : 'AI Отключен'}
|
| 102 |
>
|
| 103 |
+
{/* AI Toggle Switch - FIXED UI Phase 3 */}
|
| 104 |
+
<div className="flex items-center gap-2">
|
| 105 |
+
<button
|
| 106 |
+
onClick={onToggleAI}
|
| 107 |
+
disabled={isTogglingAI}
|
| 108 |
+
className={cn(
|
| 109 |
+
"relative w-14 h-7 rounded-full transition-all duration-300 flex-shrink-0 border",
|
| 110 |
+
isAIActive
|
| 111 |
+
? "bg-indigo-500/20 border-indigo-500/50"
|
| 112 |
+
: "bg-slate-800/80 border-slate-700",
|
| 113 |
+
isTogglingAI && "opacity-80"
|
| 114 |
+
)}
|
| 115 |
+
title={isAIActive ? 'AI Активен' : 'AI Отключен'}
|
| 116 |
+
>
|
| 117 |
+
{/* Track Background Active Glow */}
|
| 118 |
+
<div className={cn(
|
| 119 |
+
"absolute inset-0 rounded-full transition-opacity duration-300",
|
| 120 |
+
isAIActive ? "bg-indigo-600/20 opacity-100" : "opacity-0"
|
| 121 |
+
)} />
|
| 122 |
+
|
| 123 |
+
{/* Thumb */}
|
| 124 |
+
<motion.div
|
| 125 |
+
animate={{
|
| 126 |
+
x: isAIActive ? 28 : 2,
|
| 127 |
+
scale: isTogglingAI ? 0.9 : 1,
|
| 128 |
+
}}
|
| 129 |
+
transition={{ type: "spring", stiffness: 500, damping: 28 }}
|
| 130 |
+
className={cn(
|
| 131 |
+
"absolute top-[3px] w-[20px] h-[20px] rounded-full flex items-center justify-center transition-colors duration-300 z-10 shadow-md",
|
| 132 |
+
isAIActive
|
| 133 |
+
? "bg-white shadow-[0_0_10px_rgba(255,255,255,0.4)]"
|
| 134 |
+
: "bg-slate-400"
|
| 135 |
+
)}
|
| 136 |
+
>
|
| 137 |
+
{isTogglingAI && (
|
| 138 |
+
<Loader2 className="w-3 h-3 animate-spin text-indigo-500" />
|
| 139 |
+
)}
|
| 140 |
+
</motion.div>
|
| 141 |
+
</button>
|
| 142 |
</div>
|
| 143 |
</button>
|
| 144 |
</div>
|
frontend/components/ChatWindow/MessageBubble.tsx
CHANGED
|
@@ -16,32 +16,7 @@ interface MessageBubbleProps {
|
|
| 16 |
onDelete?: (messageId: string) => void
|
| 17 |
}
|
| 18 |
|
| 19 |
-
/
|
| 20 |
-
* Image Modal Component for viewing images inside the platform
|
| 21 |
-
*/
|
| 22 |
-
function ImageModal({ src, alt, isOpen, onClose }: { src: string; alt: string; isOpen: boolean; onClose: () => void }) {
|
| 23 |
-
if (!isOpen) return null
|
| 24 |
-
|
| 25 |
-
return (
|
| 26 |
-
<div
|
| 27 |
-
className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm"
|
| 28 |
-
onClick={onClose}
|
| 29 |
-
>
|
| 30 |
-
<button
|
| 31 |
-
className="absolute top-4 right-4 p-2 rounded-full bg-white/10 hover:bg-white/20 transition-colors"
|
| 32 |
-
onClick={onClose}
|
| 33 |
-
>
|
| 34 |
-
<X className="w-6 h-6 text-white" />
|
| 35 |
-
</button>
|
| 36 |
-
<img
|
| 37 |
-
src={src}
|
| 38 |
-
alt={alt}
|
| 39 |
-
className="max-w-[90vw] max-h-[90vh] object-contain rounded-lg"
|
| 40 |
-
onClick={(e) => e.stopPropagation()}
|
| 41 |
-
/>
|
| 42 |
-
</div>
|
| 43 |
-
)
|
| 44 |
-
}
|
| 45 |
|
| 46 |
/**
|
| 47 |
* Renders media content (audio, image, video, file) in message bubble
|
|
@@ -117,6 +92,10 @@ function MediaContent({ message, isMe }: { message: Message; isMe: boolean }) {
|
|
| 117 |
)
|
| 118 |
}
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
// Image
|
| 121 |
if (mediaType.includes('image')) {
|
| 122 |
return (
|
|
@@ -129,7 +108,7 @@ function MediaContent({ message, isMe }: { message: Message; isMe: boolean }) {
|
|
| 129 |
onClick={() => setIsImageModalOpen(true)}
|
| 130 |
/>
|
| 131 |
</div>
|
| 132 |
-
<
|
| 133 |
src={message.media_url!}
|
| 134 |
alt={message.media_name || 'Image'}
|
| 135 |
isOpen={isImageModalOpen}
|
|
|
|
| 16 |
onDelete?: (messageId: string) => void
|
| 17 |
}
|
| 18 |
|
| 19 |
+
// ImageModal removed in favor of global ImageViewer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Renders media content (audio, image, video, file) in message bubble
|
|
|
|
| 92 |
)
|
| 93 |
}
|
| 94 |
|
| 95 |
+
import { ImageViewer } from '@/components/ui/ImageViewer'
|
| 96 |
+
|
| 97 |
+
// ... inside MediaContent function ...
|
| 98 |
+
|
| 99 |
// Image
|
| 100 |
if (mediaType.includes('image')) {
|
| 101 |
return (
|
|
|
|
| 108 |
onClick={() => setIsImageModalOpen(true)}
|
| 109 |
/>
|
| 110 |
</div>
|
| 111 |
+
<ImageViewer
|
| 112 |
src={message.media_url!}
|
| 113 |
alt={message.media_name || 'Image'}
|
| 114 |
isOpen={isImageModalOpen}
|
frontend/components/ChatWindow/MessageInput.tsx
CHANGED
|
@@ -33,83 +33,105 @@ export function MessageInput({
|
|
| 33 |
const handleKeyDown = (e: React.KeyboardEvent) => {
|
| 34 |
if (e.key === 'Enter' && !e.shiftKey) {
|
| 35 |
e.preventDefault()
|
| 36 |
-
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
-
const
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
const startRecording = async () => {
|
| 56 |
try {
|
| 57 |
-
// Haptic feedback if available
|
| 58 |
-
if (window.navigator && window.navigator.vibrate) {
|
| 59 |
-
window.navigator.vibrate(10);
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
| 63 |
streamRef.current = stream
|
| 64 |
const mediaRecorder = new MediaRecorder(stream)
|
| 65 |
mediaRecorderRef.current = mediaRecorder
|
| 66 |
chunksRef.current = []
|
| 67 |
|
|
|
|
| 68 |
mediaRecorder.ondataavailable = (e) => {
|
| 69 |
if (e.data.size > 0) chunksRef.current.push(e.data)
|
| 70 |
}
|
| 71 |
|
|
|
|
| 72 |
mediaRecorder.onstop = () => {
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
}
|
| 77 |
-
streamRef.current?.getTracks().forEach(track => track.stop())
|
| 78 |
-
streamRef.current = null
|
| 79 |
}
|
| 80 |
|
| 81 |
mediaRecorder.start()
|
| 82 |
setIsRecording(true)
|
| 83 |
} catch (err) {
|
| 84 |
console.error('Error accessing microphone:', err)
|
|
|
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
-
const
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
streamRef.current = null
|
| 98 |
-
}
|
| 99 |
-
mediaRecorderRef.current.stop()
|
| 100 |
-
setIsRecording(false)
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
const cancelRecording = () => {
|
| 105 |
if (mediaRecorderRef.current && isRecording) {
|
| 106 |
-
|
| 107 |
-
mediaRecorderRef.current.onstop = null
|
| 108 |
-
mediaRecorderRef.current.stop()
|
| 109 |
setIsRecording(false)
|
| 110 |
}
|
| 111 |
streamRef.current?.getTracks().forEach(track => track.stop())
|
| 112 |
streamRef.current = null
|
|
|
|
| 113 |
}
|
| 114 |
|
| 115 |
const clearPendingAudio = () => {
|
|
@@ -123,10 +145,12 @@ export function MessageInput({
|
|
| 123 |
|
| 124 |
const toggleAudioPreview = () => {
|
| 125 |
if (!pendingAudio) return
|
|
|
|
| 126 |
if (!audioPreviewRef.current) {
|
| 127 |
audioPreviewRef.current = new Audio(URL.createObjectURL(pendingAudio))
|
| 128 |
audioPreviewRef.current.onended = () => setIsPlayingPreview(false)
|
| 129 |
}
|
|
|
|
| 130 |
if (isPlayingPreview) {
|
| 131 |
audioPreviewRef.current.pause()
|
| 132 |
setIsPlayingPreview(false)
|
|
@@ -137,81 +161,79 @@ export function MessageInput({
|
|
| 137 |
}
|
| 138 |
|
| 139 |
return (
|
| 140 |
-
|
|
|
|
| 141 |
<div className="max-w-4xl mx-auto relative">
|
| 142 |
{/* File Preview */}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
>
|
| 151 |
-
|
| 152 |
-
<
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
</motion.div>
|
| 157 |
-
)}
|
| 158 |
-
</AnimatePresence>
|
| 159 |
|
| 160 |
{/* Audio Preview */}
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
| 168 |
>
|
| 169 |
-
<
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
<X className="w-3.5 h-3.5 text-white/70" />
|
| 178 |
-
</button>
|
| 179 |
-
</motion.div>
|
| 180 |
-
)}
|
| 181 |
-
</AnimatePresence>
|
| 182 |
|
| 183 |
{/* Audio Recording UI */}
|
| 184 |
<AnimatePresence>
|
| 185 |
{isRecording && (
|
| 186 |
<motion.div
|
| 187 |
-
initial={{ opacity: 0, scale: 0.
|
| 188 |
-
animate={{ opacity: 1, scale: 1
|
| 189 |
-
exit={{ opacity: 0, scale: 0.
|
| 190 |
-
className="absolute inset-0 bg-
|
| 191 |
>
|
| 192 |
-
<div className="flex items-center gap-3 text-
|
| 193 |
-
<div className="flex gap-1 items-end h-
|
| 194 |
-
<div className="w-1
|
| 195 |
-
<div className="w-1
|
| 196 |
-
<div className="w-1
|
| 197 |
-
<div className="w-1
|
| 198 |
</div>
|
| 199 |
-
<span className="text-
|
| 200 |
</div>
|
| 201 |
-
<div className="flex items-center gap-
|
| 202 |
<button
|
| 203 |
type="button"
|
| 204 |
onClick={cancelRecording}
|
| 205 |
-
className="p-2.5 bg-white/
|
|
|
|
| 206 |
>
|
| 207 |
<X className="w-5 h-5" />
|
| 208 |
</button>
|
|
|
|
| 209 |
<button
|
| 210 |
type="button"
|
| 211 |
-
onClick={
|
| 212 |
-
className="p-2.5 bg-
|
|
|
|
| 213 |
>
|
| 214 |
-
<Send className="w-5 h-5
|
| 215 |
</button>
|
| 216 |
</div>
|
| 217 |
</motion.div>
|
|
@@ -219,8 +241,8 @@ export function MessageInput({
|
|
| 219 |
</AnimatePresence>
|
| 220 |
|
| 221 |
<form
|
| 222 |
-
onSubmit={
|
| 223 |
-
className="flex items-center gap-1.5 px-0.5"
|
| 224 |
>
|
| 225 |
<input
|
| 226 |
type="file"
|
|
@@ -230,13 +252,13 @@ export function MessageInput({
|
|
| 230 |
accept="image/*,.pdf,.txt,.doc,.docx"
|
| 231 |
/>
|
| 232 |
|
| 233 |
-
<div className="flex items-center
|
| 234 |
<button
|
| 235 |
type="button"
|
| 236 |
onClick={() => fileInputRef.current?.click()}
|
| 237 |
className={cn(
|
| 238 |
"p-2.5 rounded-full transition-all active:scale-90",
|
| 239 |
-
selectedFile ? "text-indigo-400 bg-indigo-500/10" : "text-slate-
|
| 240 |
)}
|
| 241 |
>
|
| 242 |
<Paperclip className="w-5 h-5" />
|
|
@@ -245,12 +267,12 @@ export function MessageInput({
|
|
| 245 |
<button
|
| 246 |
type="button"
|
| 247 |
onMouseDown={(e) => { e.preventDefault(); startRecording(); }}
|
| 248 |
-
onMouseUp={
|
| 249 |
onTouchStart={(e) => { e.preventDefault(); startRecording(); }}
|
| 250 |
-
onTouchEnd={
|
| 251 |
className={cn(
|
| 252 |
-
"p-2.5 rounded-full transition-all active:scale-95",
|
| 253 |
-
isRecording ? "text-red-
|
| 254 |
)}
|
| 255 |
disabled={!!selectedFile}
|
| 256 |
>
|
|
@@ -263,7 +285,7 @@ export function MessageInput({
|
|
| 263 |
onChange={(e) => onChange(e.target.value)}
|
| 264 |
onKeyDown={handleKeyDown}
|
| 265 |
placeholder="Написать..."
|
| 266 |
-
className="flex-1 bg-white/
|
| 267 |
rows={1}
|
| 268 |
style={{ minHeight: '44px' }}
|
| 269 |
disabled={disabled || isRecording}
|
|
@@ -273,10 +295,10 @@ export function MessageInput({
|
|
| 273 |
type="submit"
|
| 274 |
disabled={(!value.trim() && !selectedFile && !pendingAudio) || isSending || disabled}
|
| 275 |
className={cn(
|
| 276 |
-
"p-2.5 rounded-full transition-all duration-300 flex items-center justify-center
|
| 277 |
((value.trim() || selectedFile || pendingAudio) && !isSending && !disabled)
|
| 278 |
-
? "text-indigo-400 bg-indigo-500/
|
| 279 |
-
: "text-slate-
|
| 280 |
)}
|
| 281 |
>
|
| 282 |
{isSending ? (
|
|
@@ -284,11 +306,16 @@ export function MessageInput({
|
|
| 284 |
) : (
|
| 285 |
<Send className={cn(
|
| 286 |
"w-5 h-5",
|
| 287 |
-
((value.trim() || selectedFile || pendingAudio) && !isSending && !disabled) && "fill-indigo-400"
|
| 288 |
)} />
|
| 289 |
)}
|
| 290 |
</button>
|
| 291 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
</div>
|
| 293 |
</div>
|
| 294 |
)
|
|
|
|
| 33 |
const handleKeyDown = (e: React.KeyboardEvent) => {
|
| 34 |
if (e.key === 'Enter' && !e.shiftKey) {
|
| 35 |
e.preventDefault()
|
| 36 |
+
handleSubmit(e as unknown as React.FormEvent)
|
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
+
const handleSubmit = (e: React.FormEvent) => {
|
| 41 |
+
e.preventDefault()
|
| 42 |
+
if (isRecording) {
|
| 43 |
+
stopRecording()
|
| 44 |
+
return
|
| 45 |
+
}
|
| 46 |
+
onSubmit(e, selectedFile, pendingAudio)
|
| 47 |
+
setSelectedFile(null)
|
| 48 |
+
setPendingAudio(null)
|
| 49 |
+
}
|
| 50 |
|
| 51 |
+
const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
|
| 52 |
+
if (e.target.files && e.target.files[0]) {
|
| 53 |
+
setSelectedFile(e.target.files[0])
|
| 54 |
+
setPendingAudio(null) // Clear audio if file selected
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
|
| 58 |
+
// Voice Recording Logic with Promise-based handling (to avoid race conditions)
|
| 59 |
+
const stopRecordingPromise = () => {
|
| 60 |
+
return new Promise<Blob | null>((resolve) => {
|
| 61 |
+
if (!mediaRecorderRef.current || !isRecording) {
|
| 62 |
+
resolve(null)
|
| 63 |
+
return
|
| 64 |
+
}
|
| 65 |
|
| 66 |
+
const mediaRecorder = mediaRecorderRef.current
|
| 67 |
+
const chunks: Blob[] = []
|
| 68 |
+
|
| 69 |
+
mediaRecorder.ondataavailable = (e) => {
|
| 70 |
+
if (e.data.size > 0) chunks.push(e.data)
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
mediaRecorder.onstop = () => {
|
| 74 |
+
const blob = new Blob(chunks, { type: 'audio/webm' })
|
| 75 |
+
streamRef.current?.getTracks().forEach(track => track.stop())
|
| 76 |
+
streamRef.current = null
|
| 77 |
+
resolve(blob)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
mediaRecorder.stop()
|
| 81 |
+
setIsRecording(false)
|
| 82 |
+
})
|
| 83 |
}
|
| 84 |
|
| 85 |
const startRecording = async () => {
|
| 86 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
|
| 88 |
streamRef.current = stream
|
| 89 |
const mediaRecorder = new MediaRecorder(stream)
|
| 90 |
mediaRecorderRef.current = mediaRecorder
|
| 91 |
chunksRef.current = []
|
| 92 |
|
| 93 |
+
// We handle data in the stop promise mostly, but this is backup
|
| 94 |
mediaRecorder.ondataavailable = (e) => {
|
| 95 |
if (e.data.size > 0) chunksRef.current.push(e.data)
|
| 96 |
}
|
| 97 |
|
| 98 |
+
// Normal stop (saving to pending)
|
| 99 |
mediaRecorder.onstop = () => {
|
| 100 |
+
// If not handled by the specific promise call, fallback to pending logic
|
| 101 |
+
// But we usually handle it inside the specific action
|
| 102 |
+
if (streamRef.current) { // If stream still exists, it means we didn't cleanup yet
|
| 103 |
+
streamRef.current.getTracks().forEach(track => track.stop())
|
| 104 |
}
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
|
| 107 |
mediaRecorder.start()
|
| 108 |
setIsRecording(true)
|
| 109 |
} catch (err) {
|
| 110 |
console.error('Error accessing microphone:', err)
|
| 111 |
+
alert('Не удалось получить доступ к микрофону')
|
| 112 |
}
|
| 113 |
}
|
| 114 |
|
| 115 |
+
const handleSendVoice = async (e: React.BaseSyntheticEvent) => {
|
| 116 |
+
e.preventDefault()
|
| 117 |
+
e.stopPropagation()
|
| 118 |
+
|
| 119 |
+
if (!isRecording) return
|
| 120 |
+
|
| 121 |
+
const audioBlob = await stopRecordingPromise()
|
| 122 |
+
if (audioBlob) {
|
| 123 |
+
onSubmit(e as any, null, audioBlob)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
const cancelRecording = () => {
|
| 128 |
if (mediaRecorderRef.current && isRecording) {
|
| 129 |
+
mediaRecorderRef.current.stop() // this triggers onstop but we won't use the result if we cleared chunks
|
|
|
|
|
|
|
| 130 |
setIsRecording(false)
|
| 131 |
}
|
| 132 |
streamRef.current?.getTracks().forEach(track => track.stop())
|
| 133 |
streamRef.current = null
|
| 134 |
+
chunksRef.current = [] // Clear chunks
|
| 135 |
}
|
| 136 |
|
| 137 |
const clearPendingAudio = () => {
|
|
|
|
| 145 |
|
| 146 |
const toggleAudioPreview = () => {
|
| 147 |
if (!pendingAudio) return
|
| 148 |
+
|
| 149 |
if (!audioPreviewRef.current) {
|
| 150 |
audioPreviewRef.current = new Audio(URL.createObjectURL(pendingAudio))
|
| 151 |
audioPreviewRef.current.onended = () => setIsPlayingPreview(false)
|
| 152 |
}
|
| 153 |
+
|
| 154 |
if (isPlayingPreview) {
|
| 155 |
audioPreviewRef.current.pause()
|
| 156 |
setIsPlayingPreview(false)
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
return (
|
| 164 |
+
// Added larger bottom padding (pb-24) to ensure input clears the mobile bottom nav (approx 60-80px)
|
| 165 |
+
<div className="px-2 sm:px-3 pb-24 md:pb-6 pt-2 bg-[#050510]/60 backdrop-blur-xl border-t border-white/5 flex-shrink-0 relative z-30">
|
| 166 |
<div className="max-w-4xl mx-auto relative">
|
| 167 |
{/* File Preview */}
|
| 168 |
+
{selectedFile && (
|
| 169 |
+
<motion.div
|
| 170 |
+
initial={{ opacity: 0, y: 10 }}
|
| 171 |
+
animate={{ opacity: 1, y: 0 }}
|
| 172 |
+
className="mb-2 p-2 bg-[#050510]/80 backdrop-blur-xl rounded-xl border border-white/10 flex items-center gap-2 w-fit max-w-[calc(100%-20px)] shadow-lg"
|
| 173 |
+
>
|
| 174 |
+
<Paperclip className="w-4 h-4 text-indigo-400 shrink-0" />
|
| 175 |
+
<span className="text-xs text-white truncate">{selectedFile.name}</span>
|
| 176 |
+
<button onClick={() => setSelectedFile(null)} className="p-1 hover:bg-white/10 rounded-full">
|
| 177 |
+
<X className="w-3 h-3 text-slate-400" />
|
| 178 |
+
</button>
|
| 179 |
+
</motion.div>
|
| 180 |
+
)}
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
{/* Audio Preview */}
|
| 183 |
+
{pendingAudio && !isRecording && (
|
| 184 |
+
<motion.div
|
| 185 |
+
initial={{ opacity: 0, y: 10 }}
|
| 186 |
+
animate={{ opacity: 1, y: 0 }}
|
| 187 |
+
className="mb-2 p-2 bg-[#050510]/80 backdrop-blur-xl rounded-xl border border-indigo-500/30 flex items-center gap-2 w-fit shadow-lg"
|
| 188 |
+
>
|
| 189 |
+
<button
|
| 190 |
+
onClick={toggleAudioPreview}
|
| 191 |
+
className="w-8 h-8 rounded-full bg-indigo-500/20 flex items-center justify-center text-indigo-400 hover:bg-indigo-500/30"
|
| 192 |
>
|
| 193 |
+
{isPlayingPreview ? <Pause className="w-4 h-4" /> : <Play className="w-4 h-4 ml-0.5" />}
|
| 194 |
+
</button>
|
| 195 |
+
<span className="text-xs text-indigo-300 font-medium">🎤 Готовое сообщение</span>
|
| 196 |
+
<button onClick={clearPendingAudio} className="p-1 hover:bg-white/10 rounded-full">
|
| 197 |
+
<X className="w-3 h-3 text-slate-400" />
|
| 198 |
+
</button>
|
| 199 |
+
</motion.div>
|
| 200 |
+
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
{/* Audio Recording UI */}
|
| 203 |
<AnimatePresence>
|
| 204 |
{isRecording && (
|
| 205 |
<motion.div
|
| 206 |
+
initial={{ opacity: 0, scale: 0.95 }}
|
| 207 |
+
animate={{ opacity: 1, scale: 1 }}
|
| 208 |
+
exit={{ opacity: 0, scale: 0.95 }}
|
| 209 |
+
className="absolute inset-0 bg-[#050510]/95 backdrop-blur-2xl rounded-2xl border border-red-500/30 flex items-center justify-between px-4 z-40 shadow-2xl"
|
| 210 |
>
|
| 211 |
+
<div className="flex items-center gap-3 text-red-400">
|
| 212 |
+
<div className="flex gap-1 items-end h-4">
|
| 213 |
+
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0s]" style={{ height: '60%' }} />
|
| 214 |
+
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0.2s]" style={{ height: '100%' }} />
|
| 215 |
+
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0.4s]" style={{ height: '40%' }} />
|
| 216 |
+
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0.1s]" style={{ height: '80%' }} />
|
| 217 |
</div>
|
| 218 |
+
<span className="text-sm font-bold tracking-tight">ЗАПИСЬ...</span>
|
| 219 |
</div>
|
| 220 |
+
<div className="flex items-center gap-3">
|
| 221 |
<button
|
| 222 |
type="button"
|
| 223 |
onClick={cancelRecording}
|
| 224 |
+
className="p-2.5 bg-white/5 text-slate-400 rounded-full hover:bg-white/10 hover:text-white transition-all active:scale-90"
|
| 225 |
+
title="Отменить"
|
| 226 |
>
|
| 227 |
<X className="w-5 h-5" />
|
| 228 |
</button>
|
| 229 |
+
{/* Send Button inside Recording UI */}
|
| 230 |
<button
|
| 231 |
type="button"
|
| 232 |
+
onClick={handleSendVoice}
|
| 233 |
+
className="p-2.5 bg-red-500/20 text-red-500 rounded-full hover:bg-red-500/30 transition-all active:scale-90 shadow-[0_0_15px_rgba(239,68,68,0.2)]"
|
| 234 |
+
title="Готово"
|
| 235 |
>
|
| 236 |
+
<Send className="w-5 h-5" />
|
| 237 |
</button>
|
| 238 |
</div>
|
| 239 |
</motion.div>
|
|
|
|
| 241 |
</AnimatePresence>
|
| 242 |
|
| 243 |
<form
|
| 244 |
+
onSubmit={handleSubmit}
|
| 245 |
+
className="flex items-center gap-1.5 px-0.5 transition-all"
|
| 246 |
>
|
| 247 |
<input
|
| 248 |
type="file"
|
|
|
|
| 252 |
accept="image/*,.pdf,.txt,.doc,.docx"
|
| 253 |
/>
|
| 254 |
|
| 255 |
+
<div className="flex items-center">
|
| 256 |
<button
|
| 257 |
type="button"
|
| 258 |
onClick={() => fileInputRef.current?.click()}
|
| 259 |
className={cn(
|
| 260 |
"p-2.5 rounded-full transition-all active:scale-90",
|
| 261 |
+
selectedFile ? "text-indigo-400 bg-indigo-500/10" : "text-slate-400/60 hover:text-white hover:bg-white/5"
|
| 262 |
)}
|
| 263 |
>
|
| 264 |
<Paperclip className="w-5 h-5" />
|
|
|
|
| 267 |
<button
|
| 268 |
type="button"
|
| 269 |
onMouseDown={(e) => { e.preventDefault(); startRecording(); }}
|
| 270 |
+
onMouseUp={handleSendVoice}
|
| 271 |
onTouchStart={(e) => { e.preventDefault(); startRecording(); }}
|
| 272 |
+
onTouchEnd={handleSendVoice}
|
| 273 |
className={cn(
|
| 274 |
+
"p-2.5 rounded-full transition-all active:scale-95 touch-none select-none",
|
| 275 |
+
isRecording ? "text-red-500 bg-red-500/10" : "text-slate-400/60 hover:text-white hover:bg-white/5"
|
| 276 |
)}
|
| 277 |
disabled={!!selectedFile}
|
| 278 |
>
|
|
|
|
| 285 |
onChange={(e) => onChange(e.target.value)}
|
| 286 |
onKeyDown={handleKeyDown}
|
| 287 |
placeholder="Написать..."
|
| 288 |
+
className="flex-1 bg-white/5 border border-white/5 rounded-[22px] focus:ring-1 focus:ring-indigo-500/30 focus:border-indigo-500/30 focus:bg-white/10 resize-none max-h-32 py-2.5 px-4 text-[16px] leading-tight text-white placeholder:text-slate-600 overflow-y-auto transition-all shadow-inner"
|
| 289 |
rows={1}
|
| 290 |
style={{ minHeight: '44px' }}
|
| 291 |
disabled={disabled || isRecording}
|
|
|
|
| 295 |
type="submit"
|
| 296 |
disabled={(!value.trim() && !selectedFile && !pendingAudio) || isSending || disabled}
|
| 297 |
className={cn(
|
| 298 |
+
"p-2.5 rounded-full transition-all duration-300 flex items-center justify-center",
|
| 299 |
((value.trim() || selectedFile || pendingAudio) && !isSending && !disabled)
|
| 300 |
+
? "text-indigo-400 bg-indigo-500/10 shadow-[0_0_15px_rgba(99,102,241,0.1)] active:scale-90"
|
| 301 |
+
: "text-slate-700 pointer-events-none"
|
| 302 |
)}
|
| 303 |
>
|
| 304 |
{isSending ? (
|
|
|
|
| 306 |
) : (
|
| 307 |
<Send className={cn(
|
| 308 |
"w-5 h-5",
|
| 309 |
+
((value.trim() || selectedFile || pendingAudio) && !isSending && !disabled) && "fill-indigo-400/10"
|
| 310 |
)} />
|
| 311 |
)}
|
| 312 |
</button>
|
| 313 |
</form>
|
| 314 |
+
<div className="hidden md:block text-center mt-2">
|
| 315 |
+
<p className="text-[10px] text-slate-400 font-medium">
|
| 316 |
+
Enter для отправки • Shift + Enter для переноса
|
| 317 |
+
</p>
|
| 318 |
+
</div>
|
| 319 |
</div>
|
| 320 |
</div>
|
| 321 |
)
|
frontend/components/MobileBottomNav.tsx
CHANGED
|
@@ -53,8 +53,8 @@ export function MobileBottomNav() {
|
|
| 53 |
initial={{ y: '100%' }}
|
| 54 |
animate={{ y: 0 }}
|
| 55 |
exit={{ y: '100%' }}
|
| 56 |
-
transition={{ type: 'spring', damping: 20, stiffness:
|
| 57 |
-
className="fixed bottom-0 left-0 right-0 z-
|
| 58 |
>
|
| 59 |
<div className="flex items-center justify-around h-[50px] px-2 relative">
|
| 60 |
{/* Главная */}
|
|
|
|
| 53 |
initial={{ y: '100%' }}
|
| 54 |
animate={{ y: 0 }}
|
| 55 |
exit={{ y: '100%' }}
|
| 56 |
+
transition={{ type: 'spring', damping: 20, stiffness: 200 }}
|
| 57 |
+
className="fixed bottom-0 left-0 right-0 z-40 bg-gradient-to-t from-black via-[#050510]/95 to-[#050510]/80 backdrop-blur-xl border-t border-white/5 pb-[env(safe-area-inset-bottom,0px)] md:hidden"
|
| 58 |
>
|
| 59 |
<div className="flex items-center justify-around h-[50px] px-2 relative">
|
| 60 |
{/* Главная */}
|
frontend/components/MobileNav.tsx
CHANGED
|
@@ -57,7 +57,7 @@ export function MobileHeader({ onOpen }: { onOpen?: () => void }) {
|
|
| 57 |
const isUnlimited = limitDialogs === null || limitDialogs === undefined
|
| 58 |
|
| 59 |
return (
|
| 60 |
-
<div className="flex flex-col fixed top-
|
| 61 |
<div className="flex items-center justify-between px-3 h-[52px] text-white w-full relative">
|
| 62 |
{/* Left: Logo + Brand Name */}
|
| 63 |
<Link href="/settings/brain" className="flex items-center gap-2.5 active:scale-95 transition-transform">
|
|
@@ -138,11 +138,18 @@ export function MobileSidebarWrapper({ children }: { children: React.ReactNode }
|
|
| 138 |
useEffect(() => {
|
| 139 |
if (isOpen) {
|
| 140 |
document.body.style.overflow = 'hidden'
|
|
|
|
|
|
|
|
|
|
| 141 |
} else {
|
| 142 |
document.body.style.overflow = ''
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
return () => {
|
| 145 |
document.body.style.overflow = ''
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
}, [isOpen])
|
| 148 |
|
|
@@ -168,7 +175,7 @@ export function MobileSidebarWrapper({ children }: { children: React.ReactNode }
|
|
| 168 |
animate={{ opacity: 1 }}
|
| 169 |
exit={{ opacity: 0 }}
|
| 170 |
onClick={closeSidebar}
|
| 171 |
-
className="fixed inset-0 bg-black/
|
| 172 |
/>
|
| 173 |
|
| 174 |
{/* Sidebar Drawer */}
|
|
@@ -177,7 +184,10 @@ export function MobileSidebarWrapper({ children }: { children: React.ReactNode }
|
|
| 177 |
animate={{ x: 0 }}
|
| 178 |
exit={{ x: '-100%' }}
|
| 179 |
transition={{ type: 'spring', damping: 25, stiffness: 200 }}
|
| 180 |
-
className="fixed left-0 top-0 bottom-0 w-72
|
|
|
|
|
|
|
|
|
|
| 181 |
>
|
| 182 |
{/* Aurora Effect Background */}
|
| 183 |
<div className="absolute inset-0 overflow-hidden pointer-events-none z-0">
|
|
|
|
| 57 |
const isUnlimited = limitDialogs === null || limitDialogs === undefined
|
| 58 |
|
| 59 |
return (
|
| 60 |
+
<div className="flex flex-col fixed top-[-1px] left-0 right-0 z-[60] bg-gradient-to-b from-black via-[#050510]/95 to-[#050510]/80 backdrop-blur-xl border-b border-white/5 pt-[calc(env(safe-area-inset-top,0px)+1px)] md:hidden">
|
| 61 |
<div className="flex items-center justify-between px-3 h-[52px] text-white w-full relative">
|
| 62 |
{/* Left: Logo + Brand Name */}
|
| 63 |
<Link href="/settings/brain" className="flex items-center gap-2.5 active:scale-95 transition-transform">
|
|
|
|
| 138 |
useEffect(() => {
|
| 139 |
if (isOpen) {
|
| 140 |
document.body.style.overflow = 'hidden'
|
| 141 |
+
// Also try to lock the main content if possible
|
| 142 |
+
const mainContent = document.querySelector('main')
|
| 143 |
+
if (mainContent) mainContent.style.overflow = 'hidden'
|
| 144 |
} else {
|
| 145 |
document.body.style.overflow = ''
|
| 146 |
+
const mainContent = document.querySelector('main')
|
| 147 |
+
if (mainContent) mainContent.style.overflow = ''
|
| 148 |
}
|
| 149 |
return () => {
|
| 150 |
document.body.style.overflow = ''
|
| 151 |
+
const mainContent = document.querySelector('main')
|
| 152 |
+
if (mainContent) mainContent.style.overflow = ''
|
| 153 |
}
|
| 154 |
}, [isOpen])
|
| 155 |
|
|
|
|
| 175 |
animate={{ opacity: 1 }}
|
| 176 |
exit={{ opacity: 0 }}
|
| 177 |
onClick={closeSidebar}
|
| 178 |
+
className="fixed inset-0 bg-black/30 backdrop-blur-sm z-40"
|
| 179 |
/>
|
| 180 |
|
| 181 |
{/* Sidebar Drawer */}
|
|
|
|
| 184 |
animate={{ x: 0 }}
|
| 185 |
exit={{ x: '-100%' }}
|
| 186 |
transition={{ type: 'spring', damping: 25, stiffness: 200 }}
|
| 187 |
+
className="fixed left-0 top-0 bottom-0 w-72 backdrop-blur-3xl z-50 shadow-2xl overflow-hidden border-r border-white/10"
|
| 188 |
+
style={{
|
| 189 |
+
background: 'rgba(5, 5, 16, 0.85)',
|
| 190 |
+
}}
|
| 191 |
>
|
| 192 |
{/* Aurora Effect Background */}
|
| 193 |
<div className="absolute inset-0 overflow-hidden pointer-events-none z-0">
|
frontend/components/ui/ImageViewer.tsx
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use client'
|
| 2 |
+
|
| 3 |
+
import { X, ZoomIn, ZoomOut, Download } from 'lucide-react'
|
| 4 |
+
import { motion, AnimatePresence } from 'framer-motion'
|
| 5 |
+
import { useState, useRef, useEffect } from 'react'
|
| 6 |
+
import { cn } from '@/lib/utils'
|
| 7 |
+
|
| 8 |
+
interface ImageViewerProps {
|
| 9 |
+
src: string
|
| 10 |
+
alt: string
|
| 11 |
+
isOpen: boolean
|
| 12 |
+
onClose: () => void
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
export function ImageViewer({ src, alt, isOpen, onClose }: ImageViewerProps) {
|
| 16 |
+
const [scale, setScale] = useState(1)
|
| 17 |
+
const [position, setPosition] = useState({ x: 0, y: 0 })
|
| 18 |
+
const [isDragging, setIsDragging] = useState(false)
|
| 19 |
+
const containerRef = useRef<HTMLDivElement>(null)
|
| 20 |
+
|
| 21 |
+
// Reset zoom on close
|
| 22 |
+
useEffect(() => {
|
| 23 |
+
if (!isOpen) {
|
| 24 |
+
setScale(1)
|
| 25 |
+
setPosition({ x: 0, y: 0 })
|
| 26 |
+
}
|
| 27 |
+
}, [isOpen])
|
| 28 |
+
|
| 29 |
+
const handleZoomIn = () => setScale(prev => Math.min(prev + 0.5, 4))
|
| 30 |
+
const handleZoomOut = () => setScale(prev => Math.max(prev - 0.5, 1))
|
| 31 |
+
|
| 32 |
+
const handleDownload = async () => {
|
| 33 |
+
try {
|
| 34 |
+
const response = await fetch(src)
|
| 35 |
+
const blob = await response.blob()
|
| 36 |
+
const url = window.URL.createObjectURL(blob)
|
| 37 |
+
const a = document.createElement('a')
|
| 38 |
+
a.href = url
|
| 39 |
+
a.download = alt || 'image.jpg'
|
| 40 |
+
document.body.appendChild(a)
|
| 41 |
+
a.click()
|
| 42 |
+
window.URL.revokeObjectURL(url)
|
| 43 |
+
document.body.removeChild(a)
|
| 44 |
+
} catch (err) {
|
| 45 |
+
console.error('Download failed', err)
|
| 46 |
+
window.open(src, '_blank')
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
return (
|
| 51 |
+
<AnimatePresence>
|
| 52 |
+
{isOpen && (
|
| 53 |
+
<motion.div
|
| 54 |
+
initial={{ opacity: 0 }}
|
| 55 |
+
animate={{ opacity: 1 }}
|
| 56 |
+
exit={{ opacity: 0 }}
|
| 57 |
+
transition={{ duration: 0.2 }}
|
| 58 |
+
className="fixed inset-0 z-[100] bg-black/95 backdrop-blur-md flex flex-col"
|
| 59 |
+
onClick={onClose}
|
| 60 |
+
>
|
| 61 |
+
{/* Header Controls */}
|
| 62 |
+
<div className="flex items-center justify-between p-4 z-50">
|
| 63 |
+
<button
|
| 64 |
+
onClick={(e) => { e.stopPropagation(); onClose(); }}
|
| 65 |
+
className="p-2 rounded-full bg-white/10 hover:bg-white/20 text-white transition-colors"
|
| 66 |
+
>
|
| 67 |
+
<X className="w-6 h-6" />
|
| 68 |
+
</button>
|
| 69 |
+
|
| 70 |
+
<div className="flex items-center gap-4 bg-black/40 rounded-full px-4 py-2 border border-white/10 backdrop-blur-sm">
|
| 71 |
+
<button
|
| 72 |
+
onClick={(e) => { e.stopPropagation(); handleZoomOut(); }}
|
| 73 |
+
className="text-white hover:text-indigo-400 disabled:opacity-50"
|
| 74 |
+
disabled={scale <= 1}
|
| 75 |
+
>
|
| 76 |
+
<ZoomOut className="w-5 h-5" />
|
| 77 |
+
</button>
|
| 78 |
+
<span className="text-sm font-medium text-slate-300 min-w-[3ch] text-center">
|
| 79 |
+
{Math.round(scale * 100)}%
|
| 80 |
+
</span>
|
| 81 |
+
<button
|
| 82 |
+
onClick={(e) => { e.stopPropagation(); handleZoomIn(); }}
|
| 83 |
+
className="text-white hover:text-indigo-400 disabled:opacity-50"
|
| 84 |
+
disabled={scale >= 4}
|
| 85 |
+
>
|
| 86 |
+
<ZoomIn className="w-5 h-5" />
|
| 87 |
+
</button>
|
| 88 |
+
</div>
|
| 89 |
+
|
| 90 |
+
<button
|
| 91 |
+
onClick={(e) => { e.stopPropagation(); handleDownload(); }}
|
| 92 |
+
className="p-2 rounded-full bg-white/10 hover:bg-white/20 text-white transition-colors"
|
| 93 |
+
>
|
| 94 |
+
<Download className="w-6 h-6" />
|
| 95 |
+
</button>
|
| 96 |
+
</div>
|
| 97 |
+
|
| 98 |
+
{/* Image Container */}
|
| 99 |
+
<div
|
| 100 |
+
className="flex-1 overflow-hidden flex items-center justify-center relative touch-none"
|
| 101 |
+
ref={containerRef}
|
| 102 |
+
onClick={onClose} // Clicking background closes
|
| 103 |
+
>
|
| 104 |
+
<motion.div
|
| 105 |
+
drag={scale > 1}
|
| 106 |
+
dragConstraints={containerRef}
|
| 107 |
+
dragElastic={0.1}
|
| 108 |
+
onDragStart={() => setIsDragging(true)}
|
| 109 |
+
onDragEnd={() => setIsDragging(false)}
|
| 110 |
+
animate={{ scale, x: position.x, y: position.y }}
|
| 111 |
+
transition={{ type: "spring", stiffness: 300, damping: 30 }}
|
| 112 |
+
className="relative cursor-grab active:cursor-grabbing"
|
| 113 |
+
onClick={(e) => e.stopPropagation()} // Clicking image doesn't close
|
| 114 |
+
>
|
| 115 |
+
<img
|
| 116 |
+
src={src}
|
| 117 |
+
alt={alt}
|
| 118 |
+
className="max-w-full max-h-[85vh] object-contain rounded-md shadow-2xl"
|
| 119 |
+
draggable={false}
|
| 120 |
+
/>
|
| 121 |
+
</motion.div>
|
| 122 |
+
</div>
|
| 123 |
+
|
| 124 |
+
{/* Footer / Caption */}
|
| 125 |
+
<div className="p-6 text-center z-50 pointer-events-none">
|
| 126 |
+
<p className="text-white/80 text-sm font-medium truncate max-w-sm mx-auto">{alt}</p>
|
| 127 |
+
</div>
|
| 128 |
+
</motion.div>
|
| 129 |
+
)}
|
| 130 |
+
</AnimatePresence>
|
| 131 |
+
)
|
| 132 |
+
}
|