Krish-05 commited on
Commit
26263e6
·
verified ·
1 Parent(s): 7cef6fc

Update frontend/src/components/chat/ChatInputArea.jsx

Browse files
frontend/src/components/chat/ChatInputArea.jsx CHANGED
@@ -1,47 +1,28 @@
1
  // frontend/src/components/chat/ChatInputArea.jsx
2
- import React, { useState, useEffect } from 'react';
3
- // Import the new library: VoiceVisualizer component and its hook
4
  import { VoiceVisualizer, useVoiceVisualizer } from 'react-voice-visualizer';
5
 
6
-
7
  const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
8
  const [message, setMessage] = useState('');
9
- // isRecording state can now be derived from the useVoiceVisualizer hook
10
- // const [isRecording, setIsRecording] = useState(false); // No longer directly managed here
11
- // const [recordedBlob, setRecordedBlob] = useState(null); // No longer directly managed here
12
 
13
- // Correct BASE_URL to point to Nginx's /api endpoint
14
- // Nginx will then proxy /api/transcribe-audio to FastAPI's /transcribe-audio
15
  const BASE_URL = '/api';
16
 
17
- // Initialize the voice visualizer hook
 
18
  const {
19
  startRecording,
20
  stopRecording,
21
- recordingBlob, // This will hold the recorded audio blob
22
- isRecording, // This state is managed by the hook
23
- // Other properties like playbackState, audioLevel, etc., are available if needed
24
- } = useVoiceVisualizer({
25
- // Optional: Configure VoiceVisualizer properties here if desired
26
- // E.g., fftSize: 1024,
27
- // On recording end, the blob will be available in 'recordingBlob'
28
- });
29
 
30
- // Use useEffect to watch for when recordingBlob becomes available
31
- // This replaces the onStop prop of ReactMic
32
  useEffect(() => {
33
  if (recordingBlob) {
34
  console.log('Recorded blob is: ', recordingBlob);
35
- // Pass the blob directly to the parent handler
36
  onSendVoiceMessage(recordingBlob);
37
- // You might want to clear recordingBlob here if you only want to send it once
38
- // useVoiceVisualizer might have a reset function, or you might manage it
39
- // if you need to re-record without refreshing. For now, we'll let it
40
- // be handled by the next startRecording clearing it.
41
  }
42
  }, [recordingBlob, onSendVoiceMessage]);
43
 
44
-
45
  const handleTextChange = (e) => {
46
  setMessage(e.target.value);
47
  };
@@ -54,16 +35,14 @@ const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
54
  }
55
  };
56
 
57
- // --- Voice Recording Handlers ---
58
- // These now simply call the functions provided by useVoiceVisualizer
59
  const handleStartRecording = () => {
60
  if (isLoading) return;
61
- startRecording(); // Call the hook's startRecording function
62
  console.log("Recording started...");
63
  };
64
 
65
  const handleStopRecording = () => {
66
- stopRecording(); // Call the hook's stopRecording function
67
  console.log("Recording stopped.");
68
  };
69
 
@@ -81,14 +60,12 @@ const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
81
  Send
82
  </button>
83
 
84
- {/* Microphone icon and recording area */}
85
  <div className="voice-input-container">
86
  <button
87
  type="button"
88
  className={`voice-record-btn ${isRecording ? 'recording' : ''}`}
89
  onMouseDown={handleStartRecording}
90
  onMouseUp={handleStopRecording}
91
- // For mobile touch events
92
  onTouchStart={handleStartRecording}
93
  onTouchEnd={handleStopRecording}
94
  disabled={isLoading}
@@ -106,17 +83,11 @@ const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
106
  </svg>
107
  )}
108
  </button>
109
- {/* Render the VoiceVisualizer component */}
110
  <VoiceVisualizer
111
- ref={null} // VoiceVisualizer uses ref for direct DOM interaction, but not always needed for basic use.
112
- className="sound-wave" // Apply your existing CSS class
113
- mainBarColor="#4CAF50" // Example color
114
- secondaryBarColor="#81C784" // Example color
115
- // If you want it to be transparent like ReactMic was, you'd need to adjust your CSS
116
- // or see if the component offers specific props for background/stroke.
117
- // For now, these colors will show the visualization.
118
- // The `hook` prop passes the state from `useVoiceVisualizer` to the component
119
- hook={useVoiceVisualizer()} // Pass the hook directly to the component
120
  />
121
  </div>
122
  </form>
 
1
  // frontend/src/components/chat/ChatInputArea.jsx
2
+ import React, { useState, useEffect } from 'react';
 
3
  import { VoiceVisualizer, useVoiceVisualizer } from 'react-voice-visualizer';
4
 
 
5
  const ChatInputArea = ({ onSendMessage, onSendVoiceMessage, isLoading }) => {
6
  const [message, setMessage] = useState('');
 
 
 
7
 
 
 
8
  const BASE_URL = '/api';
9
 
10
+ const voiceVisualizerHook = useVoiceVisualizer({});
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
  };
 
35
  }
36
  };
37
 
 
 
38
  const handleStartRecording = () => {
39
  if (isLoading) return;
40
+ startRecording();
41
  console.log("Recording started...");
42
  };
43
 
44
  const handleStopRecording = () => {
45
+ stopRecording();
46
  console.log("Recording stopped.");
47
  };
48
 
 
60
  Send
61
  </button>
62
 
 
63
  <div className="voice-input-container">
64
  <button
65
  type="button"
66
  className={`voice-record-btn ${isRecording ? 'recording' : ''}`}
67
  onMouseDown={handleStartRecording}
68
  onMouseUp={handleStopRecording}
 
69
  onTouchStart={handleStartRecording}
70
  onTouchEnd={handleStopRecording}
71
  disabled={isLoading}
 
83
  </svg>
84
  )}
85
  </button>
 
86
  <VoiceVisualizer
87
+ className="sound-wave"
88
+ mainBarColor="#4CAF50"
89
+ secondaryBarColor="#81C784"
90
+ hook={voiceVisualizerHook}
 
 
 
 
 
91
  />
92
  </div>
93
  </form>