omninexus-ai / components /MoltBotView.tsx
aditya9708's picture
Auto-deploy from AI Studio
e4c7604 verified
Raw History Blame Contribute Delete
6.68 kB
import React, { useState, useRef } from 'react';
import { GeminiService } from '../geminiService';
import Markdown from 'react-markdown';
const MoltBotView: React.FC = () => {
const [messages, setMessages] = useState<{ role: 'user' | 'model', content: string, image?: string }[]>([]);
const [input, setInput] = useState('');
const [image, setImage] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleImageUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
const reader = new FileReader();
reader.onloadend = () => {
setImage(reader.result as string);
};
reader.readAsDataURL(file);
}
};
const handleSend = async () => {
if (!input.trim() && !image) return;
const userMsg = { role: 'user' as const, content: input, image: image || undefined };
setMessages(prev => [...prev, userMsg]);
setInput('');
setImage(null);
setIsLoading(true);
try {
let responseText = '';
if (userMsg.image) {
// Multimodal call
const base64Data = userMsg.image.split(',')[1];
const mimeType = userMsg.image.split(';')[0].split(':')[1];
const result = await GeminiService.vision(userMsg.content || "What is in this image?", base64Data, mimeType);
responseText = result.text;
} else {
// Chat call
const result = await GeminiService.chat(userMsg.content);
responseText = result.text;
}
setMessages(prev => [...prev, { role: 'model', content: responseText }]);
} catch (error) {
setMessages(prev => [...prev, { role: 'model', content: "Bhai, error aa gaya! Shayad API key ya network ka issue hai." }]);
} finally {
setIsLoading(false);
}
};
return (
<div className="flex flex-col h-full bg-[#020617]">
<div className="flex-1 overflow-y-auto p-6 space-y-6 no-scrollbar">
{messages.length === 0 && (
<div className="h-full flex flex-col items-center justify-center text-center opacity-40">
<div className="w-20 h-20 bg-orange-500/20 rounded-full flex items-center justify-center mb-4">
<svg className="w-10 h-10 text-orange-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" /></svg>
</div>
<h3 className="text-xl font-black text-white uppercase italic">MoltBot Multimodal</h3>
<p className="text-xs font-bold text-slate-500 max-w-xs mt-2 uppercase tracking-widest">Upload images and ask questions. I can see and reason.</p>
</div>
)}
{messages.map((m, i) => (
<div key={i} className={`flex ${m.role === 'user' ? 'justify-end' : 'justify-start'}`}>
<div className={`max-w-[80%] rounded-3xl px-6 py-4 ${m.role === 'user' ? 'bg-orange-600 text-white' : 'bg-[#0a0f1e] border border-white/5 text-slate-200'}`}>
{m.image && (
<img src={m.image} alt="User upload" className="w-full max-w-sm rounded-xl mb-3 border border-white/10" />
)}
<div className="markdown-body text-sm leading-relaxed">
<Markdown>{m.content}</Markdown>
</div>
</div>
</div>
))}
{isLoading && (
<div className="flex justify-start">
<div className="bg-[#0a0f1e] border border-white/5 rounded-3xl px-6 py-4 flex items-center gap-3">
<div className="flex gap-1">
<div className="w-1.5 h-1.5 bg-orange-500 rounded-full animate-bounce"></div>
<div className="w-1.5 h-1.5 bg-orange-500 rounded-full animate-bounce [animation-delay:0.2s]"></div>
<div className="w-1.5 h-1.5 bg-orange-500 rounded-full animate-bounce [animation-delay:0.4s]"></div>
</div>
<span className="text-[10px] font-black text-slate-500 uppercase tracking-widest">MoltBot is thinking...</span>
</div>
</div>
)}
</div>
<div className="p-6 border-t border-white/5 bg-[#020617]/80 backdrop-blur-xl">
<div className="max-w-4xl mx-auto">
{image && (
<div className="mb-4 relative inline-block">
<img src={image} alt="Preview" className="h-20 w-20 object-cover rounded-xl border-2 border-orange-500" />
<button onClick={() => setImage(null)} className="absolute -top-2 -right-2 bg-red-500 text-white rounded-full p-1 shadow-lg">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /></svg>
</button>
</div>
)}
<div className="flex gap-4">
<button
onClick={() => fileInputRef.current?.click()}
className="p-4 bg-[#0a0f1e] border border-white/5 rounded-2xl text-slate-400 hover:text-orange-500 transition-colors"
>
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" /></svg>
</button>
<input type="file" ref={fileInputRef} onChange={handleImageUpload} accept="image/*" className="hidden" />
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleSend()}
placeholder="Ask MoltBot anything about images or text..."
className="flex-1 bg-[#0a0f1e] border border-white/5 rounded-2xl px-6 py-4 text-white placeholder:text-slate-600 focus:border-orange-500 outline-none transition-all"
/>
<button
onClick={handleSend}
disabled={isLoading || (!input.trim() && !image)}
className="bg-orange-600 text-white px-8 rounded-2xl font-black uppercase text-xs tracking-widest hover:scale-105 active:scale-95 transition-all disabled:opacity-50 shadow-xl shadow-orange-600/20"
>
Send
</button>
</div>
</div>
</div>
</div>
);
};
export default MoltBotView;