"use client" import { useState, useCallback, useRef } from "react" import { AIChat, type AIChatHandle } from "@/components/ai/ai-chat" import { JobSelector } from "@/components/ai/job-selector" import { Bot, ChevronRight } from "lucide-react" const tips = [ "Ask for cold email templates for a specific job", "Request objection handling tips", "Ask for outreach strategies per industry", "Generate a follow up sequence", "Get LinkedIn connection message templates", ] export default function AIAssistantPage() { const [selectedJob, setSelectedJob] = useState<{ job_title: string; keywords: string[]; industry: string; description: string } | null>(null) const [recentPrompts, setRecentPrompts] = useState([]) const [searching, setSearching] = useState(false) const aiChatRef = useRef(null) const handleJobSelect = useCallback((job: typeof selectedJob) => { setSelectedJob(job) }, []) const handleSearch = useCallback(async (job: NonNullable) => { setSearching(true) const keyword = job.keywords[0] aiChatRef.current?.addAssistantMessage(`🔍 Searching Facebook for **${job.job_title}** leads...`) const controller = new AbortController() const timeoutId = setTimeout(() => controller.abort(), 360000) const statusId = setTimeout(() => { aiChatRef.current?.addAssistantMessage("⏳ Still searching Facebook (this can take up to 5 minutes)...") }, 45000) try { const res = await fetch(`http://localhost:3008/scrape/facebook?force=true&query=${encodeURIComponent(keyword)}`, { method: "POST", signal: controller.signal }) clearTimeout(timeoutId) clearTimeout(statusId) const data = await res.json() if (data.success && data.leads?.length > 0) { const leadsText = data.leads.map((lead: any, i: number) => `**${i + 1}.** ${lead.author || "Unknown"}\n> ${(lead.content || "").slice(0, 300)}\n> 🔗 ${lead.url || "(no link available)"}` ).join("\n\n") aiChatRef.current?.addAssistantMessage(`✅ Found **${data.leads.length}** leads:\n\n${leadsText}`) } else { const reason = data.error || data.flag_reason || "No leads found this time" aiChatRef.current?.addAssistantMessage(`⚠️ ${reason}`) } } catch (err: any) { clearTimeout(timeoutId) clearTimeout(statusId) const msg = err.name === "AbortError" ? "❌ Scraper timed out after 5 minutes. Try again or check the scraper service." : `❌ ${err instanceof Error ? err.message : "Search failed"}` aiChatRef.current?.addAssistantMessage(msg) } finally { setSearching(false) } }, []) const handleTipClick = useCallback((tip: string) => { aiChatRef.current?.fillInput(tip) }, []) const handleRecentPromptClick = useCallback((prompt: string) => { aiChatRef.current?.fillInput(prompt) }, []) const handleMessageSent = useCallback((msg: string) => { setRecentPrompts((prev) => { const next = [msg, ...prev.filter((p) => p !== msg)].slice(0, 3) return next }) }, []) return (

AI Sales Assistant

Powered by local AI

Online
Local AI Model
Target Job
{selectedJob && (

{selectedJob.job_title}

{selectedJob.industry}

{selectedJob.description}

{selectedJob.keywords.map((kw, i) => ( {kw} ))}
)}
Quick Tips
{tips.map((tip, i) => (
handleTipClick(tip)} className="flex items-start gap-2.5 bg-card/50 hover:bg-card border border-border hover:border-primary/20 rounded-xl p-3.5 mb-2 cursor-pointer transition-all duration-200 group" > {tip}
))}
Recent Prompts
{recentPrompts.length > 0 ? ( recentPrompts.map((prompt, i) => (
handleRecentPromptClick(prompt)} className="bg-card/50 rounded-xl p-3 mb-2 border border-border text-muted-foreground text-xs truncate hover:text-foreground cursor-pointer transition-colors duration-200" > {prompt}
)) ) : (
Your recent prompts will appear here
)}
Messages today
24
Tokens used
12.4k
) }