"use client"; import { useCallback, useState } from "react"; import { UserSendEmail } from "@prisma/client"; import useSWR from "swr"; import { cn, fetcher, formatDate, htmlToText } from "@/lib/utils"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Skeleton } from "@/components/ui/skeleton"; import { PaginationWrapper } from "../shared/pagination"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "../ui/collapsible"; export default function SendsEmailList({ isAdminModel, }: { isAdminModel: boolean; }) { const [currentPage, setCurrentPage] = useState(1); const [pageSize, setPageSize] = useState(10); const [searchQuery, setSearchQuery] = useState(""); const { data, isLoading, error } = useSWR<{ list: UserSendEmail[]; total: number; }>( `/api/email/send/list?page=${currentPage}&size=${pageSize}&search=${encodeURIComponent(searchQuery)}&all=${isAdminModel}`, fetcher, { dedupingInterval: 5000 }, ); const totalPages = data ? Math.ceil(data.total / pageSize) : 1; const debouncedSearch = useCallback((value: string) => { setSearchQuery(value); setCurrentPage(1); // Reset to first page on search }, []); const handleSearch = (e: React.ChangeEvent) => { debouncedSearch(e.target.value); }; return ( Sent Emails
{isLoading ? (
{[...Array(5)].map((_, i) => ( ))}
) : error ? (
Failed to load emails. Please try again.
) : !data || data.list.length === 0 ? (
No emails found.
) : (
{data.list.map((email) => (
{email.from} {formatDate(email.createdAt as any)}
Send To: {email.to}

Subject:{" "} {email.subject || "No subject"}

{htmlToText(email.html || "")}

{htmlToText(email.html || "")}
))}
{data && totalPages > 1 && ( )}
)}
); }