'use client'; import { useState, useEffect } from 'react'; import { useSearchParams } from 'next/navigation'; import Link from 'next/link'; interface Email { key: string; subject: string; date: string; processed: string; } export default function Emails() { const searchParams = useSearchParams(); const bucket = searchParams.get('bucket'); const mailbox = searchParams.get('mailbox'); const [emails, setEmails] = useState([]); const [message, setMessage] = useState(''); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { if (!bucket || !mailbox) { setError('Missing parameters'); setLoading(false); return; } const auth = localStorage.getItem('auth'); if (!auth) { setError('Not authenticated'); setLoading(false); return; } fetch(`/api/emails?bucket=${bucket}&mailbox=${encodeURIComponent(mailbox)}`, { headers: { Authorization: `Basic ${auth}` } }) .then(res => { if (!res.ok) throw new Error('Failed to fetch emails'); return res.json(); }) .then(data => { // Sortiere nach date descending const sorted = data.sort((a: Email, b: Email) => new Date(b.date).getTime() - new Date(a.date).getTime()); setEmails(sorted); }) .catch(err => setError(err.message)) .finally(() => setLoading(false)); }, [bucket, mailbox]); if (loading) return
Loading...
; if (error) return
{error}
; const formatDate = (dateStr: string) => { const date = new Date(dateStr); return date.toLocaleString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }); }; const handleResendAll = async () => { const auth = localStorage.getItem('auth'); if (!auth) return setMessage('Not authenticated'); const response = await fetch('/api/resend-domain', { method: 'POST', headers: { Authorization: `Basic ${auth}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ bucket }), }); const res = await response.json(); setMessage(res.message || res.error); }; const handleUpdateProcessed = async (key: string, newValue: boolean) => { const auth = localStorage.getItem('auth'); if (!auth) return; await fetch('/api/email', { method: 'PUT', headers: { Authorization: `Basic ${auth}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ bucket, key, processed: newValue ? 'true' : 'false' }), }); setEmails(emails.map(em => em.key === key ? { ...em, processed: newValue ? 'true' : 'false' } : em)); }; const handleResend = async (key: string) => { const auth = localStorage.getItem('auth'); if (!auth) return alert('Not authenticated'); const response = await fetch('/api/email', { method: 'POST', headers: { Authorization: `Basic ${auth}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ bucket, key }), }); const res = await response.json(); alert(res.message || res.error); }; return (

Emails for {mailbox} in {bucket}

{message &&

{message}

}
{emails.map((e: Email) => ( ))}
Subject Date S3 Key Processed Actions
{e.subject} {formatDate(e.date)} {e.key} handleUpdateProcessed(e.key, e.processed !== 'true')} className="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded" /> View
); }