import Link from 'next/link'; import { ChevronLeft, ChevronRight, Mail, Plus, Send, Users, } from 'lucide-react'; import { getTranslations } from 'next-intl/server'; 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 { ListToolbar } from '@kit/ui/list-toolbar'; 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_KEYS, } from '~/lib/status-badges'; const PAGE_SIZE = 25; interface PageProps { params: Promise<{ account: string }>; searchParams: Promise>; } function buildQuery( base: Record, overrides: Record, ): string { const params = new URLSearchParams(); for (const [key, value] of Object.entries({ ...base, ...overrides })) { if (value !== undefined && value !== '') { params.set(key, String(value)); } } const qs = params.toString(); return qs ? `?${qs}` : ''; } export default async function NewsletterPage({ params, searchParams, }: PageProps) { const { account } = await params; const search = await searchParams; const client = getSupabaseServerClient(); const t = await getTranslations('newsletter'); const { data: acct } = await client .from('accounts') .select('id') .eq('slug', account) .single(); if (!acct) return ; const q = typeof search.q === 'string' ? search.q : undefined; const status = typeof search.status === 'string' ? search.status : undefined; const page = Math.max(1, Number(search.page) || 1); const api = createNewsletterApi(client); const result = await api.listNewsletters(acct.id, { search: q, status, page, pageSize: PAGE_SIZE, }); const newsletters = result.data; const totalItems = result.total; const totalPages = result.totalPages; const safePage = result.page; const totalRecipients = newsletters.reduce( (sum: number, n: Record) => sum + (Number(n.total_recipients) || 0), 0, ); const queryBase = { q, status }; return (
{/* Header */}

{t('list.subtitle')}

{/* Stats */}
} /> } /> } />
{/* Toolbar */} {/* Table or Empty State */} {totalItems === 0 ? ( } title={t('list.noNewsletters')} description={t('list.createFirst')} actionLabel={t('list.newNewsletter')} actionHref={`/home/${account}/newsletter/new`} /> ) : ( {t('list.title')} ({totalItems})
{newsletters.map((nl: Record) => ( ))}
{t('list.subject')} Status {t('list.recipients')} {t('list.created')} {t('list.sent')}
{String(nl.subject ?? t('list.noSubject'))} {t( NEWSLETTER_STATUS_LABEL_KEYS[String(nl.status)] ?? String(nl.status), )} {nl.total_recipients != null ? String(nl.total_recipients) : '—'} {formatDate(nl.created_at as string | null)} {formatDate(nl.sent_at as string | null)}
{/* Pagination */} {totalPages > 1 && (

Seite {safePage} von {totalPages}

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