tahamajs/IE / Projects /front /src /components /genui /GenerativeChatArea.tsx
tahamajs's picture
download
raw
8.29 kB
// src/components/genui/GenerativeChatArea.tsx
import { useState, useRef, useEffect } from "react";
import { useGenerativeUI } from "@/hooks/useGenerativeUI";
import { ComponentRenderer } from "./ComponentRenderer";
import {
Send, Sparkles, Loader2, Trash2, StopCircle, Wand2,
ChevronDown, BarChart3, Calendar, Table2, Brain,
} from "lucide-react";
import { toast } from "react-toastify";
interface GenerativeChatAreaProps {
contextDocuments?: { id: string; title: string; content: string }[];
contextData?: Record<string, any>;
}
export function GenerativeChatArea({ contextDocuments, contextData }: GenerativeChatAreaProps) {
const { messages, isStreaming, error, sendMessage, onComponentAction, clearConversation, cancelGeneration, scrollRef } =
useGenerativeUI({ contextDocuments, contextData });
const [input, setInput] = useState("");
const [showSuggestions, setShowSuggestions] = useState(true);
const handleSend = () => {
if (!input.trim()) return;
sendMessage(input.trim());
setInput("");
setShowSuggestions(false);
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
const suggestionChips = [
{ icon: BarChart3, label: "Plot my publication stats", prompt: "Plot a chart of my publications by year" },
{ icon: Calendar, label: "Show grant deadlines", prompt: "Show my upcoming grant deadlines in a calendar" },
{ icon: Table2, label: "Literature matrix", prompt: "Create a literature review matrix comparing the papers I've read" },
{ icon: Brain, label: "Analyze dataset", prompt: "Analyze the uploaded CSV and show me a summary table" },
];
return (
<div className="flex flex-col h-full bg-white/30 dark:bg-slate-950/20 backdrop-blur-md rounded-b-2xl border border-white/5 overflow-hidden shadow-xl select-text font-sans">
{/* Header */}
<div className="px-4 py-3 border-b border-white/10 dark:border-white/5 flex items-center justify-between bg-white/30 dark:bg-slate-900/30 backdrop-blur-lg select-none">
<div className="flex items-center gap-2">
<Wand2 className="w-4.5 h-4.5 text-teal-500 animate-pulse" />
<span className="font-extrabold text-sm text-slate-900 dark:text-white">Generative AI Assistant</span>
{isStreaming && (
<span className="text-[10px] bg-teal-500/10 text-teal-650 dark:text-teal-400 px-2 py-0.5 rounded-lg border border-teal-500/10 font-bold animate-pulse">
generating...
</span>
)}
</div>
<button
onClick={() => {
clearConversation();
toast.warn("Cleared conversation logs.");
}}
className="p-2 text-slate-400 hover:text-rose-500 rounded-xl transition-all active:scale-95"
title="Clear conversation"
>
<Trash2 className="w-4 h-4" />
</button>
</div>
{/* Messages */}
<div ref={scrollRef} className="flex-1 overflow-y-auto p-4 space-y-4 custom-scrollbar">
{messages.length === 0 && showSuggestions && (
<div className="text-center py-12 select-none">
<div className="w-16 h-16 mx-auto mb-4 rounded-2xl bg-teal-500/10 flex items-center justify-center">
<Wand2 className="w-8 h-8 text-teal-500" />
</div>
<h4 className="font-extrabold text-slate-700 dark:text-slate-350 text-sm mb-2">
Ask me to create something
</h4>
<p className="text-xs text-slate-500 max-w-xs mx-auto mb-5 leading-relaxed font-medium">
I can generate charts, calendars, tables, and more — all rendered directly in the chat.
</p>
<div className="flex flex-wrap gap-2 justify-center max-w-lg mx-auto">
{suggestionChips.map(chip => (
<button
key={chip.label}
onClick={() => { setInput(chip.prompt); sendMessage(chip.prompt); setShowSuggestions(false); }}
className="flex items-center gap-1.5 px-3.5 py-2 bg-white/40 dark:bg-slate-900/40 border border-white/10 dark:border-white/5 hover:bg-teal-500/10 hover:text-teal-650 dark:hover:text-teal-400 hover:border-teal-500/20 rounded-xl transition-all duration-200 text-xs font-bold text-slate-655 dark:text-slate-300"
>
<chip.icon className="w-3.5 h-3.5 text-teal-505" />
{chip.label}
</button>
))}
</div>
</div>
)}
{messages.map(msg => (
<div key={msg.id} className={`flex ${msg.role === "user" ? "justify-end" : "justify-start"}`}>
<div className={`max-w-[90%] ${msg.role === "user" ? "" : "w-full space-y-3"}`}>
{/* Text */}
{msg.text && (
<div className={`inline-block rounded-2xl px-4.5 py-3 text-xs ${
msg.role === "user"
? "bg-teal-600 text-white rounded-br-sm shadow-md shadow-teal-600/10 font-semibold"
: "bg-white/45 dark:bg-slate-900/45 border border-white/10 dark:border-white/5 text-slate-805 dark:text-slate-200 rounded-bl-sm shadow-sm font-medium leading-relaxed"
}`}>
<p className="whitespace-pre-wrap">{msg.text}</p>
</div>
)}
{/* Components */}
{msg.components?.map(comp => (
<ComponentRenderer
key={comp.id}
instance={comp}
onAction={onComponentAction}
/>
))}
</div>
</div>
))}
{isStreaming && messages[messages.length - 1]?.components?.length === 0 && !messages[messages.length - 1]?.text && (
<div className="flex justify-start">
<div className="bg-white/40 dark:bg-slate-900/45 border border-white/10 dark:border-white/5 rounded-2xl rounded-bl-sm px-4.5 py-3 flex items-center gap-2.5 shadow-sm text-xs select-none">
<Loader2 className="w-4 h-4 animate-spin text-teal-500" />
<span className="text-slate-500 dark:text-slate-450 font-bold">Formulating layout components...</span>
</div>
</div>
)}
{error && (
<div className="flex justify-center">
<div className="bg-rose-500/10 border border-rose-500/35 rounded-xl px-4 py-2 text-xs font-bold text-rose-500 select-text">
{error}
</div>
</div>
)}
</div>
{/* Input */}
<div className="p-3.5 border-t border-white/10 dark:border-white/5 bg-slate-50/5 dark:bg-slate-900/10">
<div className="flex items-end gap-2.5">
<textarea
value={input}
onChange={e => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Create a chart, calendar, table... e.g. 'Plot my publications'"
rows={1}
className="flex-1 resize-none px-4 py-3 bg-slate-100/50 dark:bg-slate-900/50 border border-white/10 dark:border-white/5 rounded-xl focus:outline-none focus:ring-1 focus:ring-teal-500/50 text-slate-805 dark:text-slate-205 text-xs font-semibold placeholder-slate-500 custom-scrollbar"
style={{ minHeight: "42px", maxHeight: "120px" }}
/>
{isStreaming ? (
<button
onClick={() => {
cancelGeneration();
toast.warn("Generation cancelled.");
}}
className="p-3 bg-rose-500 hover:bg-rose-600 text-white rounded-xl transition active:scale-95 shrink-0"
>
<StopCircle className="w-4.5 h-4.5" />
</button>
) : (
<button
onClick={handleSend}
disabled={!input.trim()}
className="p-3 bg-teal-605 hover:bg-teal-700 text-white rounded-xl disabled:opacity-50 transition active:scale-95 shrink-0 shadow-md shadow-teal-600/10"
>
<Send className="w-4.5 h-4.5" />
</button>
)}
</div>
</div>
</div>
);
}
export default GenerativeChatArea;

Xet Storage Details

Size:
8.29 kB
·
Xet hash:
c5ca15af94534811525f3f9ba0867f964a495c5929ac1a6c8a2a1f60845255b2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.