tahamajs/IE / Projects /front /src /components /threads /AIContextPanel.tsx
tahamajs's picture
download
raw
4.6 kB
// src/components/threads/AIContextPanel.tsx
import { useState } from "react";
import { Sparkles, Send, Loader2, X, RefreshCw } from "lucide-react";
import { toast } from "react-toastify";
import api from "@/lib/api";
interface AIContextPanelProps {
threadId?: string; // future use: contextual suggestions
onSuggestion?: (suggestion: string) => void;
}
export function AIContextPanel({ onSuggestion }: AIContextPanelProps) {
const [prompt, setPrompt] = useState("");
const [loading, setLoading] = useState(false);
const [suggestions, setSuggestions] = useState<string[]>([]);
const generateSuggestions = async () => {
if (!prompt.trim()) {
toast.warning("Please enter a question or topic");
return;
}
setLoading(true);
try {
// Corrected to GET request mapping on the backend
const res = await api.get("/ai/thread-suggestions", {
params: { topic: prompt.trim() }
});
setSuggestions(res.data.suggestions || []);
toast.success("AI suggestions ready");
} catch (err) {
console.error("Failed to generate AI thread suggestions:", err);
// Fail gracefully and log
toast.error("Failed to generate suggestions");
} finally {
setLoading(false);
}
};
const applySuggestion = (suggestion: string) => {
onSuggestion?.(suggestion);
toast.info("Suggestion copied to reply");
};
const clearSuggestions = () => {
setSuggestions([]);
setPrompt("");
};
return (
<div className="bg-white/5 dark:bg-black/20 border border-white/10 dark:border-white/5 rounded-2xl backdrop-blur-md p-4">
{/* Header */}
<div className="mb-3 flex items-center justify-between">
<div className="flex items-center gap-2">
<Sparkles className="h-4.5 w-4.5 text-purple-500 animate-pulse" />
<h3 className="text-xs font-semibold text-gray-200">AI Context Assistant</h3>
</div>
{suggestions.length > 0 && (
<button
onClick={clearSuggestions}
className="text-[10px] text-gray-400 hover:text-gray-200"
aria-label="Clear suggestions"
>
<X className="h-3.5 w-3.5" />
</button>
)}
</div>
{/* Input row */}
<div className="flex gap-2">
<input
type="text"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && generateSuggestions()}
placeholder="Ask for suggestions (e.g., 'improve methodology')"
className="flex-1 px-3 py-1.5 bg-white/5 border border-white/10 rounded-lg text-xs text-gray-200 focus:outline-none focus:border-white/20 transition-all"
disabled={loading}
/>
<button
onClick={generateSuggestions}
disabled={loading || !prompt.trim()}
className="px-3 py-1.5 bg-gradient-to-r from-primary to-secondary text-white font-medium text-xs rounded-lg hover:brightness-110 disabled:opacity-50 flex items-center gap-1.5 transition-all shadow-md shadow-primary/20"
>
{loading ? <Loader2 className="h-3.5 w-3.5 animate-spin" /> : <Send className="h-3.5 w-3.5" />}
{loading ? "Thinking" : "Ask"}
</button>
</div>
{/* Suggestions list */}
{suggestions.length > 0 && (
<div className="mt-3 space-y-2">
<p className="text-[10px] font-semibold text-gray-400">
Suggestions:
</p>
<div className="space-y-1.5">
{suggestions.map((suggestion, idx) => (
<button
key={idx}
onClick={() => applySuggestion(suggestion)}
className="w-full rounded-lg bg-white/5 border border-white/5 hover:border-white/15 p-2 text-left text-xs text-gray-200 transition hover:bg-white/10"
>
{suggestion}
</button>
))}
</div>
<button
onClick={generateSuggestions}
disabled={loading}
className="mt-1 flex w-full items-center justify-center gap-1 text-[10px] text-primary hover:underline transition-all"
>
<RefreshCw className="h-3 w-3 animate-spin-hover" /> Regenerate suggestions
</button>
</div>
)}
{/* Empty state hint */}
{suggestions.length === 0 && !loading && (
<p className="mt-2 text-center text-[10px] text-gray-500">
Ask a question about the thread to get AI‑powered writing help.
</p>
)}
</div>
);
}

Xet Storage Details

Size:
4.6 kB
·
Xet hash:
f88487e5e8fc66be2a771dc272742ac8b6dade9df6de9da5677a379c80b4e3dd

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