"use client"; import { useQuery } from "@tanstack/react-query"; import { Loader2, RefreshCw, Search, Sparkles } from "lucide-react"; import { useCallback, useState } from "react"; import { EmptyState, LoadingSkeleton } from "@/components/shared"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Progress } from "@/components/ui/progress"; import { useReanalyze } from "@/hooks"; import { messagesApi } from "@/lib/api"; import { safeParseJsonArray } from "@/lib/format"; import type { MessageRecord } from "@/lib/types"; import { cn } from "@/lib/utils"; export function SearchPanel() { const [query, setQuery] = useState(""); const [enabled, setEnabled] = useState(false); const reanalyzeMut = useReanalyze(); const { data: results, isFetching } = useQuery({ queryKey: ["analysis-search", query], queryFn: async () => { const result = await messagesApi.search(query, 50); return result.results; }, enabled, }); const handleSearch = useCallback(() => { if (!query.trim()) return; setEnabled(true); }, [query]); return (
setQuery(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSearch()} className="pl-9 h-9" />
{isFetching ? ( ) : results !== undefined ? ( <>

Found {results.length} result{results.length !== 1 ? "s" : ""}

{results.length === 0 ? ( ) : (
{results.map((msg) => (
{msg.username.charAt(0).toUpperCase()}
{msg.username} {new Date(msg.created_at).toLocaleString()} {msg.ai_status && ( {msg.ai_status} )}

{msg.content}

{msg.ai_moderation_flags && msg.ai_moderation_flags !== "[]" && (
{safeParseJsonArray(msg.ai_moderation_flags).map( (flag) => ( {flag} ), )}
)} {msg.ai_analysis && (

{msg.ai_analysis}

)} {msg.ai_confidence != null && (
{(msg.ai_confidence * 100).toFixed(0)}%
)}
))}
)} ) : (

Enter a search query to find messages across all channels.

Searches message content, AI flags, and analysis text.

)}
); }