import Link from 'next/link'; import { ChevronLeft, ChevronRight, Mail, Plus, Send, Users, } from 'lucide-react'; import { createNewsletterApi } from '@kit/newsletter/api'; import { formatDate } from '@kit/shared/dates'; import { getSupabaseServerClient } from '@kit/supabase/server-client'; import { Badge } from '@kit/ui/badge'; import { Button } from '@kit/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@kit/ui/card'; import { AccountNotFound } from '~/components/account-not-found'; import { CmsPageShell } from '~/components/cms-page-shell'; import { EmptyState } from '~/components/empty-state'; import { StatsCard } from '~/components/stats-card'; import { NEWSLETTER_STATUS_VARIANT, NEWSLETTER_STATUS_LABEL, } from '~/lib/status-badges'; const PAGE_SIZE = 25; interface PageProps { params: Promise<{ account: string }>; searchParams: Promise>; } export default async function NewsletterPage({ params, searchParams, }: PageProps) { const { account } = await params; const search = await searchParams; const client = getSupabaseServerClient(); const { data: acct } = await client .from('accounts') .select('id') .eq('slug', account) .single(); if (!acct) return ; const api = createNewsletterApi(client); const allNewsletters = await api.listNewsletters(acct.id); const sentCount = allNewsletters.filter( (n: Record) => n.status === 'sent', ).length; const totalRecipients = allNewsletters.reduce( (sum: number, n: Record) => sum + (Number(n.total_recipients) || 0), 0, ); // Pagination const currentPage = Math.max(1, Number(search.page) || 1); const totalItems = allNewsletters.length; const totalPages = Math.max(1, Math.ceil(totalItems / PAGE_SIZE)); const safePage = Math.min(currentPage, totalPages); const startIdx = (safePage - 1) * PAGE_SIZE; const newsletters = allNewsletters.slice(startIdx, startIdx + PAGE_SIZE); return (
{/* Header */}

Newsletter

Newsletter erstellen und versenden

{/* Stats */}
} /> } /> } />
{/* Table or Empty State */} {totalItems === 0 ? ( } title="Keine Newsletter vorhanden" description="Erstellen Sie Ihren ersten Newsletter, um loszulegen." actionLabel="Neuer Newsletter" actionHref={`/home/${account}/newsletter/new`} /> ) : ( Alle Newsletter ({totalItems})
{newsletters.map((nl: Record) => ( ))}
Betreff Status Empfänger Erstellt Gesendet
{String(nl.subject ?? '(Kein Betreff)')} {NEWSLETTER_STATUS_LABEL[String(nl.status)] ?? String(nl.status)} {nl.total_recipients != null ? String(nl.total_recipients) : '—'} {formatDate(nl.created_at)} {formatDate(nl.sent_at)}
{/* Pagination */} {totalPages > 1 && (

{startIdx + 1}–{Math.min(startIdx + PAGE_SIZE, totalItems)}{' '} von {totalItems}

{safePage > 1 ? ( ) : ( )} {safePage} / {totalPages} {safePage < totalPages ? ( ) : ( )}
)}
)}
); }