Spaces:
Running
Running
File size: 5,990 Bytes
d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 c38a89e d3c7f96 | 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 | import { useState, useRef } from "react";
export default function InputBar({ onSubmit, disabled }) {
const [image, setImage] = useState(null);
const [audio, setAudio] = useState(null);
const [text, setText] = useState("");
const [isRecording, setIsRecording] = useState(false);
const mediaRecorderRef = useRef(null);
const audioChunksRef = useRef([]);
const fileInputRef = useRef(null);
const recordingTimeoutRef = useRef(null);
function handleImageSelect(e) {
const file = e.target.files?.[0];
if (!file) return;
setImage({ url: URL.createObjectURL(file), file });
}
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const recorder = new MediaRecorder(stream);
audioChunksRef.current = [];
recorder.ondataavailable = (e) => {
if (e.data.size > 0) audioChunksRef.current.push(e.data);
};
recorder.onstop = () => {
clearTimeout(recordingTimeoutRef.current);
const blob = new Blob(audioChunksRef.current, { type: "audio/webm" });
setAudio({ url: URL.createObjectURL(blob), blob });
stream.getTracks().forEach((t) => t.stop());
};
recordingTimeoutRef.current = setTimeout(() => {
if (recorder.state === "recording") recorder.stop();
}, 30_000);
mediaRecorderRef.current = recorder;
recorder.start();
setIsRecording(true);
} catch (err) {
console.error("Mic access denied:", err);
}
}
function stopRecording() {
mediaRecorderRef.current?.stop();
setIsRecording(false);
}
function handleSubmit() {
if (!image && !audio && !text.trim()) return;
onSubmit({
imageUrl: image?.url || null,
audioUrl: audio?.url || null,
text: text.trim() || null,
});
setImage(null);
setAudio(null);
setText("");
}
const hasInput = image || audio || text.trim();
return (
<div className="px-4 py-3 border-t border-[var(--color-outline)]">
{/* Previews */}
{(image || audio) && (
<div className="flex gap-2 mb-2">
{image && (
<div className="relative">
<img src={image.url} alt="Upload" className="h-16 w-16 object-cover rounded-xl border border-[var(--color-outline)]" />
<button
onClick={() => { URL.revokeObjectURL(image.url); setImage(null); }}
className="absolute -top-1.5 -right-1.5 w-5 h-5 bg-[var(--color-surface-high)] rounded-full text-xs flex items-center justify-center hover:bg-[var(--color-surface)] cursor-pointer"
>
×
</button>
</div>
)}
{audio && (
<div className="flex items-center gap-2 px-2 py-1.5 bg-[var(--color-surface)] rounded-xl">
<audio controls src={audio.url} className="h-7 max-w-[200px]" />
<button
onClick={() => { URL.revokeObjectURL(audio.url); setAudio(null); }}
className="text-xs text-[var(--color-text-secondary)] hover:text-[var(--color-text)] cursor-pointer"
>
×
</button>
</div>
)}
</div>
)}
{/* Input row */}
<div className="flex items-center gap-2">
<input ref={fileInputRef} type="file" accept="image/*" capture="environment" onChange={handleImageSelect} className="hidden" />
<button
onClick={() => fileInputRef.current?.click()}
disabled={disabled}
className="p-2 rounded-full text-[var(--color-text-secondary)] hover:bg-[var(--color-surface)] disabled:opacity-30 transition-colors cursor-pointer"
title="Add image"
>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="m21 15-5-5L5 21"/></svg>
</button>
<button
onClick={isRecording ? stopRecording : startRecording}
disabled={disabled}
className={`p-2 rounded-full disabled:opacity-30 transition-colors cursor-pointer ${
isRecording
? "text-[var(--color-red)] bg-red-500/10 animate-pulse"
: "text-[var(--color-text-secondary)] hover:bg-[var(--color-surface)]"
}`}
title={isRecording ? "Stop recording" : "Record audio"}
>
{isRecording ? (
<svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><rect x="6" y="6" width="12" height="12" rx="2"/></svg>
) : (
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" x2="12" y1="19" y2="22"/></svg>
)}
</button>
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
onKeyDown={(e) => { if (e.key === "Enter" && hasInput && !disabled) handleSubmit(); }}
placeholder="Message Gemma 4..."
disabled={disabled}
className="flex-1 bg-[var(--color-surface)] border border-[var(--color-outline)] rounded-xl px-4 py-2.5 text-sm text-[var(--color-text)] placeholder:text-[var(--color-text-secondary)]/50 focus:border-[var(--color-blue)]/50 focus:outline-none disabled:opacity-50"
/>
<button
onClick={handleSubmit}
disabled={disabled || !hasInput}
className="px-5 py-2.5 bg-[var(--color-blue)] hover:bg-[var(--color-blue)]/90 text-white text-sm font-medium rounded-xl transition-colors disabled:opacity-30 disabled:cursor-not-allowed cursor-pointer"
>
Send
</button>
</div>
</div>
);
}
|