'use client'; import React, { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { Card } from '@/components/ui/Card'; import { Button } from '@/components/ui/Button'; import { Badge } from '@/components/ui/Badge'; import { Mail, Users, Send, CheckCircle, AlertCircle, Loader2, Lock, LogOut } from 'lucide-react'; interface Subscriber { email: string; createdAt: string; } interface BroadcastInfo { total: number; recent: Subscriber[]; } export default function NewsletterPage() { const router = useRouter(); const [isAuthenticated, setIsAuthenticated] = useState(false); const [isAuthenticating, setIsAuthenticating] = useState(true); const [loginError, setLoginError] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [info, setInfo] = useState(null); const [loading, setLoading] = useState(true); const [broadcasting, setBroadcasting] = useState(false); const [result, setResult] = useState<{ success: boolean; message: string; sent?: number; failed?: number; } | null>(null); useEffect(() => { checkAuth(); }, []); const checkAuth = async () => { try { const response = await fetch('/api/newsletter/broadcast'); if (response.ok) { setIsAuthenticated(true); const data = await response.json(); setInfo(data); setLoading(false); } else { setIsAuthenticated(false); } } catch (error) { setIsAuthenticated(false); } finally { setIsAuthenticating(false); } }; const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoginError(''); setIsAuthenticating(true); try { const response = await fetch('/api/newsletter/admin-login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email, password }), }); if (response.ok) { setIsAuthenticated(true); await checkAuth(); } else { const data = await response.json(); setLoginError(data.error || 'Invalid credentials'); } } catch (error) { setLoginError('Login failed. Please try again.'); } finally { setIsAuthenticating(false); } }; const handleLogout = async () => { await fetch('/api/auth/logout', { method: 'POST' }); router.push('/'); }; const fetchSubscriberInfo = async () => { try { const response = await fetch('/api/newsletter/broadcast'); if (response.ok) { const data = await response.json(); setInfo(data); } } catch (error) { console.error('Failed to fetch subscriber info:', error); } }; const handleBroadcast = async () => { if (!confirm(`Are you sure you want to send the AI feature launch email to ${info?.total} subscribers?`)) { return; } setBroadcasting(true); setResult(null); try { const response = await fetch('/api/newsletter/broadcast', { method: 'POST', }); const data = await response.json(); setResult({ success: response.ok, message: data.message || data.error, sent: data.sent, failed: data.failed, }); if (response.ok) { await fetchSubscriberInfo(); } } catch (error) { setResult({ success: false, message: 'Failed to send broadcast. Please try again.', }); } finally { setBroadcasting(false); } }; // Login Screen if (!isAuthenticated) { return (

Newsletter Admin

Sign in to manage subscribers

setEmail(e.target.value)} placeholder="Email" required className="w-full px-4 py-3 rounded-xl bg-background border border-border focus:border-purple-500 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all" />
setPassword(e.target.value)} placeholder="Password" required className="w-full px-4 py-3 rounded-xl bg-background border border-border focus:border-purple-500 focus:ring-2 focus:ring-purple-500/20 outline-none transition-all" />
{loginError && (

{loginError}

)}

Admin credentials required

); } // Loading if (loading) { return (
); } // Admin Dashboard return (

Newsletter Management

Manage AI feature launch notifications

{/* Stats Card */}

{info?.total || 0}

Total Subscribers

Active
{/* Broadcast Button */}

Broadcast AI Feature Launch

Send the AI feature launch announcement to all {info?.total} subscribers. This will inform them that the features are now available.

{/* Result Message */} {result && (
{result.success ? ( ) : ( )}

{result.message}

{result.sent !== undefined && (

Sent: {result.sent} | Failed: {result.failed}

)}
)} {/* Recent Subscribers */}

Recent Subscribers

{info?.recent && info.recent.length > 0 ? (
{info.recent.map((subscriber, index) => (
{subscriber.email}
{new Date(subscriber.createdAt).toLocaleDateString()}
))}
) : (

No subscribers yet

)}

💡 Tip: View all subscribers in{' '} Prisma Studio {' '}(NewsletterSubscription table)

); }