"use client"; /* eslint-disable react-hooks/set-state-in-effect */ import { useState, useEffect, useRef } from "react"; import { type SegmentRuleV2, type PreviewResult } from "@/actions/harvest-reach/segments"; import { AdminSearchInput, AdminButton } from "@/components/admin/design-system"; // Icon components const Icons = { users: (className: string) => ( ), spinner: (className: string) => ( ), chevronLeft: (className: string) => ( ), chevronRight: (className: string) => ( ), userCircle: (className: string) => ( ), }; const DEBOUNCE_MS = 300; export default function MatchingCustomersPanel({ brandId, rules, onCountChange }: Props) { const [preview, setPreview] = useState(null); const [loading, setLoading] = useState(false); const [search, setSearch] = useState(""); const [page, setPage] = useState(0); const [customerCount, setCustomerCount] = useState(undefined); const timerRef = useRef>(undefined); useEffect(() => { if (rules.filters.length === 0) { setPreview(null); setCustomerCount(undefined); return; } setLoading(true); clearTimeout(timerRef.current); timerRef.current = setTimeout(async () => { const { previewSegmentWithCustomers } = await import("@/actions/harvest-reach/segments"); const result = await previewSegmentWithCustomers(brandId, rules); setPreview(result); setLoading(false); setPage(0); const count = result?.count ?? 0; setCustomerCount(count); onCountChange?.(count); }, DEBOUNCE_MS); return () => clearTimeout(timerRef.current); }, [brandId, rules, onCountChange]); const PAGE_SIZE = 50; const filtered = preview?.sample_customers ?? []; const searched = search ? filtered.filter( (c) => c.name.toLowerCase().includes(search.toLowerCase()) || c.email.toLowerCase().includes(search.toLowerCase()) ) : filtered; const total = preview?.count ?? 0; const paged = searched.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE); return (
{/* Header */}

Matching Customers

Preview your segment audience

{preview && total > 0 && ( {total.toLocaleString()} total )}
{/* Content area */}
{rules.filters.length === 0 ? (
{/* Decorative background */}

Add filters to see matching customers

) : loading ? (
{Icons.spinner("h-5 w-5 animate-spin")} Calculating matches…
) : ( <>
{ setSearch(e.target.value); setPage(0); }} onClear={() => setSearch("")} />
{paged.length === 0 ? (

No customers match these filters.

) : (
{paged.map((c, idx) => (
{c.name ? c.name.slice(0, 2).toUpperCase() : "??"}

{c.name || "(no name)"}

{c.email}

{c.tags.length > 0 && (
{c.tags.slice(0, 2).map((tag) => ( {tag} ))}
)}
))}
)} {searched.length > PAGE_SIZE && (
Showing {page * PAGE_SIZE + 1}–{Math.min((page + 1) * PAGE_SIZE, searched.length)} of {searched.length}
setPage((p) => Math.max(0, p - 1))} disabled={page === 0} icon={Icons.chevronLeft("h-4 w-4")} iconPosition="left" > Prev setPage((p) => p + 1)} disabled={(page + 1) * PAGE_SIZE >= searched.length} icon={Icons.chevronRight("h-4 w-4")} iconPosition="right" > Next
)} )}
); } type Props = { brandId: string; rules: SegmentRuleV2; onCountChange?: (count: number) => void; };