Spaces:
Sleeping
Sleeping
added log to find bob
Browse files
frontend/src/components/chat/ChatInputArea.jsx
CHANGED
|
@@ -1,50 +1,59 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import { VoiceVisualizer, useVoiceVisualizer } from 'react-voice-visualizer';
|
| 3 |
|
| 4 |
const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
|
| 5 |
const [message, setMessage] = useState('');
|
| 6 |
|
| 7 |
-
//
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
const {
|
| 13 |
startRecording,
|
| 14 |
stopRecording,
|
| 15 |
recordingBlob,
|
| 16 |
isRecording,
|
|
|
|
|
|
|
|
|
|
| 17 |
} = voiceVisualizerHook;
|
| 18 |
|
|
|
|
| 19 |
useEffect(() => {
|
|
|
|
| 20 |
if (recordingBlob) {
|
| 21 |
console.log('Recorded blob is: ', recordingBlob);
|
| 22 |
onSendVoiceMessage(recordingBlob);
|
| 23 |
}
|
| 24 |
-
}, [recordingBlob, onSendVoiceMessage]);
|
| 25 |
|
| 26 |
-
const handleTextChange = (e) => {
|
| 27 |
setMessage(e.target.value);
|
| 28 |
-
};
|
| 29 |
|
| 30 |
-
const handleSubmit = (e) => {
|
| 31 |
e.preventDefault();
|
| 32 |
if (message.trim()) {
|
| 33 |
onSendMessage(message);
|
| 34 |
setMessage('');
|
| 35 |
}
|
| 36 |
-
};
|
| 37 |
|
| 38 |
-
const handleStartRecording = () => {
|
| 39 |
if (isLoading) return; // Prevent recording if an action is already in progress
|
|
|
|
| 40 |
startRecording();
|
| 41 |
-
console.log("Recording started
|
| 42 |
-
};
|
| 43 |
|
| 44 |
-
const handleStopRecording = () => {
|
|
|
|
| 45 |
stopRecording();
|
| 46 |
-
console.log("Recording stopped
|
| 47 |
-
};
|
| 48 |
|
| 49 |
return (
|
| 50 |
<div className="chat-input-area">
|
|
@@ -53,13 +62,12 @@ const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
|
|
| 53 |
type="text"
|
| 54 |
value={message}
|
| 55 |
onChange={handleTextChange}
|
| 56 |
-
placeholder={isRecording ? "Recording..." : "Type your message or hold for voice..."}
|
| 57 |
disabled={isLoading || isRecording}
|
| 58 |
/>
|
| 59 |
<button type="submit" disabled={isLoading || isRecording || !message.trim()}>
|
| 60 |
Send
|
| 61 |
</button>
|
| 62 |
-
|
| 63 |
<div className="voice-input-container">
|
| 64 |
<button
|
| 65 |
type="button"
|
|
@@ -83,13 +91,14 @@ const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
|
|
| 83 |
</svg>
|
| 84 |
)}
|
| 85 |
</button>
|
| 86 |
-
{/* Only show visualizer if recording
|
| 87 |
{isRecording && (
|
| 88 |
<VoiceVisualizer
|
| 89 |
className="sound-wave"
|
| 90 |
mainBarColor="#4CAF50"
|
| 91 |
secondaryBarColor="#81C784"
|
| 92 |
hook={voiceVisualizerHook}
|
|
|
|
| 93 |
/>
|
| 94 |
)}
|
| 95 |
</div>
|
|
|
|
| 1 |
+
// src/components/chat/ChatInputArea.jsx
|
| 2 |
+
import React, { useState, useEffect, useCallback } from 'react';
|
| 3 |
import { VoiceVisualizer, useVoiceVisualizer } from 'react-voice-visualizer';
|
| 4 |
|
| 5 |
const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
|
| 6 |
const [message, setMessage] = useState('');
|
| 7 |
|
| 8 |
+
// Configure useVoiceVisualizer with a specific MIME type for better compatibility
|
| 9 |
+
const voiceVisualizerHook = useVoiceVisualizer({
|
| 10 |
+
mediaRecorderOptions: { mimeType: 'audio/webm' },
|
| 11 |
+
// You can add other options here, e.g., maxRecordingTime: 60000 (for 60 seconds)
|
| 12 |
+
});
|
| 13 |
|
| 14 |
const {
|
| 15 |
startRecording,
|
| 16 |
stopRecording,
|
| 17 |
recordingBlob,
|
| 18 |
isRecording,
|
| 19 |
+
// You can also get other states if needed:
|
| 20 |
+
// isStopped,
|
| 21 |
+
// error,
|
| 22 |
} = voiceVisualizerHook;
|
| 23 |
|
| 24 |
+
// This useEffect will run whenever recordingBlob changes.
|
| 25 |
useEffect(() => {
|
| 26 |
+
debugger; // <--- KEEP THIS HERE FOR YOUR TESTING, REMOVE ONCE IT WORKS
|
| 27 |
if (recordingBlob) {
|
| 28 |
console.log('Recorded blob is: ', recordingBlob);
|
| 29 |
onSendVoiceMessage(recordingBlob);
|
| 30 |
}
|
| 31 |
+
}, [recordingBlob, onSendVoiceMessage]); // onSendVoiceMessage is stable due to useCallback in App.jsx
|
| 32 |
|
| 33 |
+
const handleTextChange = useCallback((e) => {
|
| 34 |
setMessage(e.target.value);
|
| 35 |
+
}, []);
|
| 36 |
|
| 37 |
+
const handleSubmit = useCallback((e) => {
|
| 38 |
e.preventDefault();
|
| 39 |
if (message.trim()) {
|
| 40 |
onSendMessage(message);
|
| 41 |
setMessage('');
|
| 42 |
}
|
| 43 |
+
}, [message, onSendMessage]);
|
| 44 |
|
| 45 |
+
const handleStartRecording = useCallback(() => {
|
| 46 |
if (isLoading) return; // Prevent recording if an action is already in progress
|
| 47 |
+
console.log("Attempting to start recording...");
|
| 48 |
startRecording();
|
| 49 |
+
console.log("Recording started status:", isRecording); // This might be true/false depending on async nature
|
| 50 |
+
}, [isLoading, startRecording, isRecording]);
|
| 51 |
|
| 52 |
+
const handleStopRecording = useCallback(() => {
|
| 53 |
+
console.log("Attempting to stop recording...");
|
| 54 |
stopRecording();
|
| 55 |
+
console.log("Recording stopped status:", isRecording); // This might be true/false depending on async nature
|
| 56 |
+
}, [stopRecording, isRecording]);
|
| 57 |
|
| 58 |
return (
|
| 59 |
<div className="chat-input-area">
|
|
|
|
| 62 |
type="text"
|
| 63 |
value={message}
|
| 64 |
onChange={handleTextChange}
|
| 65 |
+
placeholder={isRecording ? "Recording..." : (isLoading ? "Please wait..." : "Type your message or hold for voice...")}
|
| 66 |
disabled={isLoading || isRecording}
|
| 67 |
/>
|
| 68 |
<button type="submit" disabled={isLoading || isRecording || !message.trim()}>
|
| 69 |
Send
|
| 70 |
</button>
|
|
|
|
| 71 |
<div className="voice-input-container">
|
| 72 |
<button
|
| 73 |
type="button"
|
|
|
|
| 91 |
</svg>
|
| 92 |
)}
|
| 93 |
</button>
|
| 94 |
+
{/* Only show visualizer if actively recording to save resources */}
|
| 95 |
{isRecording && (
|
| 96 |
<VoiceVisualizer
|
| 97 |
className="sound-wave"
|
| 98 |
mainBarColor="#4CAF50"
|
| 99 |
secondaryBarColor="#81C784"
|
| 100 |
hook={voiceVisualizerHook}
|
| 101 |
+
// Optional: width="100%" height="50px"
|
| 102 |
/>
|
| 103 |
)}
|
| 104 |
</div>
|