'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(null) const [selectedFile, setSelectedFile] = useState(null) const [isRecording, setIsRecording] = useState(false) const [pendingAudio, setPendingAudio] = useState(null) const [isPlayingPreview, setIsPlayingPreview] = useState(false) const mediaRecorderRef = useRef(null) const streamRef = useRef(null) const chunksRef = useRef([]) const audioPreviewRef = useRef(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) => { 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((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
{/* File Preview */} {selectedFile && ( {selectedFile.name} )} {/* Audio Preview */} {pendingAudio && !isRecording && ( 🎤 Готовое сообщение )} {/* Audio Recording UI */} {isRecording && (
ЗАПИСЬ...
{/* Send Button inside Recording UI */}
)}