Spaces:
Running on Zero
Running on Zero
File size: 7,936 Bytes
d53374c 5d0b2ac d53374c 59f3833 d53374c 5d0b2ac d53374c 5d0b2ac d53374c 5d0b2ac d53374c 59f3833 d53374c 5d0b2ac d53374c 5d0b2ac d53374c 5d0b2ac d53374c 5d0b2ac d53374c 5d0b2ac 89e7f71 5d0b2ac 89e7f71 5d0b2ac d53374c 89e7f71 d53374c 89e7f71 d53374c | 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 | 'use client';
import React, { useState, useRef, useEffect } from 'react';
import {
Mic,
MicOff,
Send,
Sparkles,
Layers,
Zap,
ArrowUp,
Globe,
Radio,
Volume2
} from 'lucide-react';
import { sounds } from '@/src/lib/soundEffects';
interface ChatInputBarProps {
onSendMessage: (text: string) => void;
isProcessing: boolean;
isRecording: boolean;
audioLevel?: number;
liveTranscript?: string;
onToggleRecording: () => void;
onStartRecording?: () => void;
onStopRecording?: () => void;
selectedLanguage: string;
}
export function ChatInputBar({
onSendMessage,
isProcessing,
isRecording,
audioLevel = 0,
liveTranscript = '',
onToggleRecording,
onStartRecording,
onStopRecording,
selectedLanguage,
}: ChatInputBarProps) {
const [text, setText] = useState('');
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const wasRecordingRef = useRef<boolean>(false);
// Sync live real-time speech recognition transcript into the searchbox
useEffect(() => {
if (isRecording && liveTranscript) {
setText(liveTranscript);
}
}, [isRecording, liveTranscript]);
// Auto-resize textarea
useEffect(() => {
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
textareaRef.current.style.height = `${Math.min(120, textareaRef.current.scrollHeight)}px`;
}
}, [text]);
// Instantly clear searchbox when recording stops
useEffect(() => {
if (wasRecordingRef.current && !isRecording) {
setText('');
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
}
}
wasRecordingRef.current = isRecording;
}, [isRecording]);
const handleSubmit = (e?: React.FormEvent) => {
if (e) e.preventDefault();
if (!text.trim() || isProcessing) return;
sounds.playBlip();
onSendMessage(text.trim());
setText('');
if (textareaRef.current) {
textareaRef.current.style.height = 'auto';
}
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit();
}
};
const handleMicClick = () => {
sounds.playBlip();
onToggleRecording();
};
return (
<div className="w-full max-w-3xl mx-auto px-4 pb-4 md:pb-6">
{/* Main Floating Input Capsule */}
<div
className={`w-full rounded-2xl md:rounded-3xl border transition-all duration-300 relative shadow-2xl backdrop-blur-2xl ${
isRecording
? 'bg-zinc-900/95 border-emerald-500/60 shadow-emerald-950/40 ring-4 ring-emerald-500/20 shadow-black/80'
: 'bg-zinc-900/90 border-zinc-800/90 focus-within:border-emerald-500/50 focus-within:ring-2 focus-within:ring-emerald-500/10 shadow-black/60'
}`}
>
<form onSubmit={handleSubmit} className="p-3 md:p-3.5 flex flex-col gap-2">
{/* Live Recording Ambient Audio Indicator */}
{isRecording && (
<div className="flex items-center justify-between bg-zinc-950/70 border border-emerald-500/25 rounded-xl px-3.5 py-2 animate-in fade-in duration-300 backdrop-blur-md">
<div className="flex items-center gap-2.5">
<div className="relative flex items-center justify-center">
<span className="w-2 h-2 rounded-full bg-emerald-400 animate-pulse" />
<span className="absolute w-4 h-4 rounded-full bg-emerald-500/30 animate-ping" />
</div>
<span className="text-xs font-sans font-medium text-zinc-200">
Listening to your voice...
</span>
</div>
{/* Dynamic Gradient Acoustic Visualizer Wave */}
<div className="flex items-center gap-2">
<div className="flex items-center gap-1 h-5 px-2.5 py-0.5 bg-emerald-950/50 rounded-full border border-emerald-500/20">
{[25, 60, 95, 45, 80, 100, 70, 40, 85, 30].map((baseHeight, idx) => (
<div
key={idx}
className="w-0.5 bg-gradient-to-t from-emerald-400 to-cyan-300 rounded-full transition-all duration-75"
style={{
height: `${Math.max(20, baseHeight * Math.max(0.25, audioLevel))}%`,
}}
/>
))}
</div>
<span className="text-[10px] font-mono text-zinc-500 hidden sm:inline">
Auto-sends on pause
</span>
</div>
</div>
)}
{/* Textarea Input (Always visible, streams speech in real time) */}
<div className="flex items-end gap-2">
<textarea
ref={textareaRef}
rows={1}
value={text}
onChange={(e) => setText(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={
isRecording
? ' Listening... '
: isProcessing
? ' Sonic is thinking...'
: 'Ask Sonic anything...'
}
disabled={isProcessing}
className="w-full bg-transparent text-zinc-100 placeholder-zinc-500 text-sm md:text-base focus:outline-none resize-none max-h-32 font-sans py-1 leading-relaxed"
/>
</div>
{/* Bottom Dock Controls */}
<div className="flex items-center justify-between pt-2 border-t border-zinc-800/60 text-xs font-mono">
<div className="flex items-center gap-2">
{/* Voice Query Toggle / Push Button */}
<button
type="button"
onClick={handleMicClick}
disabled={isProcessing}
className={`px-3 py-1.5 rounded-xl text-xs font-bold transition-all flex items-center gap-2 cursor-pointer select-none shadow-md ${
isRecording
? 'bg-rose-500 hover:bg-rose-600 text-white animate-pulse shadow-rose-500/50 ring-2 ring-white/30'
: 'bg-zinc-800 hover:bg-emerald-600 text-zinc-200 hover:text-white border border-zinc-700'
}`}
title={isRecording ? 'Click to stop and send' : 'Click to start voice recording'}
>
{isRecording ? (
<>
<MicOff className="w-3.5 h-3.5 animate-bounce" />
<span>Stop & Send</span>
</>
) : (
<>
<Mic className="w-3.5 h-3.5 text-emerald-400" />
<span>Voice Query</span>
</>
)}
</button>
{/* <div className="hidden sm:flex items-center gap-1 text-[11px] text-zinc-500">
<span>Press</span>
<kbd className="px-1.5 py-0.5 rounded bg-zinc-800 text-zinc-400 border border-zinc-700 text-[10px]">
Enter ↵
</kbd>
<span>to send</span>
</div> */}
</div>
{/* Send Button */}
<button
type="submit"
disabled={!text.trim() || isProcessing}
className="w-8 h-8 rounded-xl bg-emerald-500 hover:bg-emerald-400 disabled:opacity-30 disabled:hover:bg-emerald-500 text-zinc-950 flex items-center justify-center transition-all shadow-md shadow-emerald-500/20 cursor-pointer active:scale-95 disabled:cursor-not-allowed"
title="Send Query"
>
{isProcessing ? (
<Sparkles className="w-4 h-4 animate-spin text-zinc-950" />
) : (
<ArrowUp className="w-4 h-4 text-zinc-950 stroke-[2.5]" />
)}
</button>
</div>
</form>
</div>
</div>
);
}
|