'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) { stopRecording() 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 startRecording = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }) streamRef.current = stream const mediaRecorder = new MediaRecorder(stream) mediaRecorderRef.current = mediaRecorder chunksRef.current = [] mediaRecorder.ondataavailable = (e) => { if (e.data.size > 0) { chunksRef.current.push(e.data) } } mediaRecorder.onstop = () => { // Save to pending instead of sending if (chunksRef.current.length > 0) { const blob = new Blob(chunksRef.current, { type: 'audio/webm' }) setPendingAudio(blob) } // Cleanup stream streamRef.current?.getTracks().forEach(track => track.stop()) streamRef.current = null } mediaRecorder.start() setIsRecording(true) } catch (err) { console.error('Error accessing microphone:', err) alert('Не удалось получить доступ к микрофону') } } const stopRecording = () => { if (mediaRecorderRef.current && isRecording) { mediaRecorderRef.current.stop() setIsRecording(false) } } const cancelRecording = () => { if (mediaRecorderRef.current && isRecording) { chunksRef.current = [] mediaRecorderRef.current.stop() setIsRecording(false) } // Also cleanup stream immediately streamRef.current?.getTracks().forEach(track => track.stop()) streamRef.current = null } 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 (
{/* File Preview */} {selectedFile && (
{selectedFile.name}
)} {/* Audio Preview */} {pendingAudio && !isRecording && (
🎤 Голосовое сообщение
)} {/* Audio Recording UI */} {isRecording && (
Запись...
{/* Cancel Button */} {/* Send Button */}
)}