Spaces:
Running
Running
File size: 15,102 Bytes
526e04d 1ad4362 615c63b d9f3520 526e04d 615c63b 526e04d 615c63b 1ad4362 615c63b db2dc5e 615c63b 1ad4362 615c63b 526e04d cb528a9 615c63b cb528a9 60f2b59 cb528a9 4147439 cb528a9 4147439 d4172a5 cb528a9 4147439 cb528a9 d4172a5 cb528a9 615c63b d4172a5 615c63b db2dc5e 615c63b cb528a9 615c63b 4147439 615c63b cb528a9 615c63b d4172a5 cb528a9 d4172a5 db2dc5e 615c63b cb528a9 615c63b cb528a9 d4172a5 cb528a9 db2dc5e d4172a5 615c63b 526e04d 1ad4362 cb528a9 1ad4362 cb528a9 1ad4362 cb528a9 1ad4362 526e04d d33ea59 740eb5c 526e04d 615c63b cb528a9 615c63b 1ad4362 cb528a9 1ad4362 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 cb528a9 4147439 615c63b 526e04d cb528a9 526e04d 615c63b cb528a9 55839d5 dca4b49 cb528a9 55839d5 526e04d 55839d5 d57223b cb528a9 d57223b cb528a9 55839d5 cb528a9 55839d5 615c63b 526e04d dca4b49 abb8c3e 526e04d abb8c3e 615c63b 526e04d d57223b 526e04d cb528a9 d57223b cb528a9 526e04d d9f3520 dca4b49 cb528a9 d9f3520 526e04d cb528a9 d0f3c0f 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 | 'use client'
import { cn } from '@/lib/utils'
import { Loader2, Paperclip, Send, Mic, X, Play, Pause } from 'lucide-react'
import { useRef, useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
interface MessageInputProps {
value: string
onChange: (value: string) => void
onSubmit: (e: React.FormEvent, file?: File | null, audioBlob?: Blob | null) => void
isSending: boolean
disabled?: boolean
}
export function MessageInput({
value,
onChange,
onSubmit,
isSending,
disabled,
}: MessageInputProps) {
const fileInputRef = useRef<HTMLInputElement>(null)
const [selectedFile, setSelectedFile] = useState<File | null>(null)
const [isRecording, setIsRecording] = useState(false)
const [pendingAudio, setPendingAudio] = useState<Blob | null>(null)
const [isPlayingPreview, setIsPlayingPreview] = useState(false)
const mediaRecorderRef = useRef<MediaRecorder | null>(null)
const streamRef = useRef<MediaStream | null>(null)
const chunksRef = useRef<Blob[]>([])
const audioPreviewRef = useRef<HTMLAudioElement | null>(null)
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
handleSubmit(e as unknown as React.FormEvent)
}
}
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
if (isRecording) {
handleSendVoice(e)
return
}
onSubmit(e, selectedFile, pendingAudio)
setSelectedFile(null)
setPendingAudio(null)
}
const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files && e.target.files[0]) {
setSelectedFile(e.target.files[0])
setPendingAudio(null) // Clear audio if file selected
}
}
const isCancelledRef = useRef(false)
// Voice Recording Logic with Promise-based handling (to avoid race conditions)
const stopRecordingPromise = () => {
return new Promise<Blob | null>((resolve) => {
if (!mediaRecorderRef.current || !isRecording) {
resolve(null)
return
}
const mediaRecorder = mediaRecorderRef.current
const chunks: Blob[] = []
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) chunks.push(e.data)
}
mediaRecorder.onstop = () => {
if (isCancelledRef.current) {
isCancelledRef.current = false
resolve(null)
} else {
const blob = new Blob(chunks, { type: 'audio/webm' })
resolve(blob)
}
streamRef.current?.getTracks().forEach(track => track.stop())
streamRef.current = null
}
mediaRecorder.stop()
setIsRecording(false)
})
}
const startRecording = async () => {
isCancelledRef.current = false
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
streamRef.current = stream
const mediaRecorder = new MediaRecorder(stream)
mediaRecorderRef.current = mediaRecorder
chunksRef.current = []
// We handle data in the stop promise mostly, but this is backup
mediaRecorder.ondataavailable = (e) => {
if (e.data.size > 0) chunksRef.current.push(e.data)
}
// Normal stop (saving to pending)
mediaRecorder.onstop = () => {
if (streamRef.current) {
streamRef.current.getTracks().forEach(track => track.stop())
streamRef.current = null
}
}
mediaRecorder.start()
setIsRecording(true)
} catch (err) {
console.error('Error accessing microphone:', err)
alert('Не удалось получить доступ к микрофону')
}
}
const handleSendVoice = async (e: React.BaseSyntheticEvent) => {
e.preventDefault()
e.stopPropagation()
if (!isRecording) return
isCancelledRef.current = false
const audioBlob = await stopRecordingPromise()
if (audioBlob) {
onSubmit(e as any, null, audioBlob)
}
}
const cancelRecording = () => {
if (mediaRecorderRef.current && isRecording) {
isCancelledRef.current = true
mediaRecorderRef.current.stop()
setIsRecording(false)
}
streamRef.current?.getTracks().forEach(track => track.stop())
streamRef.current = null
chunksRef.current = [] // Clear chunks
}
const clearPendingAudio = () => {
setPendingAudio(null)
if (audioPreviewRef.current) {
audioPreviewRef.current.pause()
audioPreviewRef.current = null
}
setIsPlayingPreview(false)
}
const toggleAudioPreview = () => {
if (!pendingAudio) return
if (!audioPreviewRef.current) {
audioPreviewRef.current = new Audio(URL.createObjectURL(pendingAudio))
audioPreviewRef.current.onended = () => setIsPlayingPreview(false)
}
if (isPlayingPreview) {
audioPreviewRef.current.pause()
setIsPlayingPreview(false)
} else {
audioPreviewRef.current.play()
setIsPlayingPreview(true)
}
}
return (
// Adjusted padding: pb-6 (safe area) + bottom nav is distinct
<div className="px-2 sm:px-3 pb-6 md:pb-6 pt-2 bg-[#050510]/60 backdrop-blur-xl border-t border-white/5 flex-shrink-0 relative z-30">
<div className="max-w-4xl mx-auto relative">
{/* File Preview */}
{selectedFile && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
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"
>
<Paperclip className="w-4 h-4 text-indigo-400 shrink-0" />
<span className="text-xs text-white truncate">{selectedFile.name}</span>
<button onClick={() => setSelectedFile(null)} className="p-1 hover:bg-white/10 rounded-full">
<X className="w-3 h-3 text-slate-400" />
</button>
</motion.div>
)}
{/* Audio Preview */}
{pendingAudio && !isRecording && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
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"
>
<button
onClick={toggleAudioPreview}
className="w-8 h-8 rounded-full bg-indigo-500/20 flex items-center justify-center text-indigo-400 hover:bg-indigo-500/30"
>
{isPlayingPreview ? <Pause className="w-4 h-4" /> : <Play className="w-4 h-4 ml-0.5" />}
</button>
<span className="text-xs text-indigo-300 font-medium">🎤 Готовое сообщение</span>
<button onClick={clearPendingAudio} className="p-1 hover:bg-white/10 rounded-full">
<X className="w-3 h-3 text-slate-400" />
</button>
</motion.div>
)}
{/* Audio Recording UI */}
<AnimatePresence>
{isRecording && (
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
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"
>
<div className="flex items-center gap-3 text-red-400">
<div className="flex gap-1 items-end h-4">
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0s]" style={{ height: '60%' }} />
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0.2s]" style={{ height: '100%' }} />
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0.4s]" style={{ height: '40%' }} />
<div className="w-1 bg-red-500 rounded-full animate-[voice-wave_1s_infinite_0.1s]" style={{ height: '80%' }} />
</div>
<span className="text-sm font-bold tracking-tight">ЗАПИСЬ...</span>
</div>
<div className="flex items-center gap-3">
<button
type="button"
onClick={cancelRecording}
className="p-2.5 bg-white/5 text-slate-400 rounded-full hover:bg-white/10 hover:text-white transition-all active:scale-90"
title="Отменить"
>
<X className="w-5 h-5" />
</button>
{/* Send Button inside Recording UI */}
<button
type="button"
onClick={handleSendVoice}
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)]"
title="Готово"
>
<Send className="w-5 h-5" />
</button>
</div>
</motion.div>
)}
</AnimatePresence>
<form
onSubmit={handleSubmit}
className="flex items-center gap-1.5 px-0.5 transition-all"
>
<input
type="file"
ref={fileInputRef}
onChange={handleFileSelect}
className="hidden"
accept="image/*,.pdf,.txt,.doc,.docx"
/>
<div className="flex items-center">
<button
type="button"
onClick={() => fileInputRef.current?.click()}
className={cn(
"p-2.5 rounded-full transition-all active:scale-90",
selectedFile ? "text-indigo-400 bg-indigo-500/10" : "text-slate-400/60 hover:text-white hover:bg-white/5"
)}
>
<Paperclip className="w-5 h-5" />
</button>
<button
type="button"
onMouseDown={(e) => { e.preventDefault(); startRecording(); }}
onMouseUp={handleSendVoice}
onTouchStart={(e) => { e.preventDefault(); startRecording(); }}
onTouchEnd={handleSendVoice}
className={cn(
"p-2.5 rounded-full transition-all active:scale-95 touch-none select-none",
isRecording ? "text-red-500 bg-red-500/10" : "text-slate-400/60 hover:text-white hover:bg-white/5"
)}
disabled={!!selectedFile}
>
<Mic className="w-5 h-5" />
</button>
</div>
<textarea
value={value}
onChange={(e) => onChange(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Написать..."
className="flex-1 bg-white/5 border border-white/10 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-relaxed text-white placeholder:text-slate-600 overflow-y-auto transition-all shadow-inner appearance-none"
rows={1}
style={{
minHeight: '44px',
display: 'flex',
alignItems: 'center'
}}
disabled={disabled || isRecording}
/>
<button
type="submit"
disabled={(!value.trim() && !selectedFile && !pendingAudio) || isSending || disabled}
className={cn(
"p-2.5 rounded-full transition-all duration-300 flex items-center justify-center",
((value.trim() || selectedFile || pendingAudio) && !isSending && !disabled)
? "text-indigo-400 bg-indigo-500/10 shadow-[0_0_15px_rgba(99,102,241,0.1)] active:scale-90"
: "text-slate-700 pointer-events-none"
)}
>
{isSending ? (
<Loader2 className="w-5 h-5 animate-spin" />
) : (
<Send className={cn(
"w-5 h-5",
((value.trim() || selectedFile || pendingAudio) && !isSending && !disabled) && "fill-indigo-400/10"
)} />
)}
</button>
</form>
<div className="hidden md:block text-center mt-2">
<p className="text-[10px] text-slate-400 font-medium">
Enter для отправки • Shift + Enter для переноса
</p>
</div>
</div>
</div>
)
}
|