"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" 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] || job.job_title 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) const scrapBase = process.env.NEXT_PUBLIC_SCRAPER_URL || "http://localhost:3008" try { const res = await fetch(`${scrapBase}/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 leadLines = data.leads .filter(Boolean) .map((lead: Record, i: number) => `**${i + 1}.** ${lead?.author || "Unknown"}\n> ${(lead?.content || "").slice(0, 300)}\n> 🔗 ${lead?.url || "(no link available)"}` ) const leadsText = leadLines.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 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} ))}
)}
) }