import React, { useState, useEffect } from 'react'; import { AuthProvider, AuthGuard, useAuth } from './AdminAuth'; import AdminLayout from './AdminLayout'; function CronContent() { const { token } = useAuth(); const [crons, setCrons] = useState([]); const [loading, setLoading] = useState(true); const [triggering, setTriggering] = useState(null); const [testAlert, setTestAlert] = useState<{ loading: boolean; result: string | null; error: boolean }>({ loading: false, result: null, error: false }); const headers = { Authorization: `Bearer ${token}` }; const load = () => { fetch('/api/admin/crons', { headers }) .then(r => r.json()) .then(res => setCrons(res.crons || [])) .finally(() => setLoading(false)); }; useEffect(load, []); const trigger = async (name: string) => { setTriggering(name); await fetch('/api/admin/crons', { method: 'POST', headers: { ...headers, 'Content-Type': 'application/json' }, body: JSON.stringify({ name }), }); setTimeout(() => { load(); setTriggering(null); }, 1000); }; const sendTestAlert = async () => { setTestAlert({ loading: true, result: null, error: false }); try { const res = await fetch('/api/admin/emails/send-test-alert', { method: 'POST', headers: { ...headers, 'Content-Type': 'application/json' }, body: JSON.stringify({}), }); const data = await res.json(); if (!res.ok) { setTestAlert({ loading: false, result: data.error || 'Verzenden mislukt', error: true }); } else { setTestAlert({ loading: false, result: `Verstuurd naar ${data.sent_to}: ${data.product} (${data.lowest_price})`, error: false }); } } catch (err: any) { setTestAlert({ loading: false, result: err.message, error: true }); } }; const timeAgo = (dt: string) => { if (!dt) return 'Nooit'; const diff = Date.now() - new Date(dt).getTime(); const h = Math.floor(diff / 3600000); if (h < 1) return `${Math.floor(diff / 60000)}m geleden`; if (h < 24) return `${h}u geleden`; return `${Math.floor(h / 24)}d geleden`; }; if (loading) return (
); return (
Test prijsalert

Stuurt een test prijsalert-email naar het admin adres met een willekeurig product

{testAlert.result && (
{testAlert.result}
)}
{crons.map((c: any) => { const lr = c.last_run; const statusColor = !lr ? 'text-[#666]' : lr.status === 'ok' ? 'text-[#00FF6A]' : lr.status === 'error' ? 'text-[#FF3B3B]' : 'text-[#FFD700]'; return (
{c.name} {c.description}
📅 {c.schedule} 🕐 {lr ? timeAgo(lr.started_at) : 'Nooit gedraaid'} {lr?.duration_secs != null && ⏱️ {Math.round(lr.duration_secs)}s} {lr?.status || 'unknown'}
{lr?.output_summary && (
{lr.output_summary}
)} {lr?.error_message && (
{lr.error_message}
)}
); })}
); } export default function CronMonitor() { return ; }