'use client'
import { useState } from 'react'
import { Button, toast, Spinner } from '@ciphera-net/ui'
import { Plus, Pencil, Trash, EnvelopeSimple, WebhooksLogo, PaperPlaneTilt } from '@phosphor-icons/react'
import { useReportSchedules, useAlertSchedules } from '@/lib/swr/dashboard'
import { deleteReportSchedule, testReportSchedule, updateReportSchedule, type ReportSchedule } from '@/lib/api/report-schedules'
import { getAuthErrorMessage } from '@ciphera-net/ui'
function ChannelIcon({ channel }: { channel: string }) {
switch (channel) {
case 'email': return
case 'webhook': return
default: return
}
}
function ScheduleRow({ schedule, siteId, onMutate }: { schedule: ReportSchedule; siteId: string; onMutate: () => void }) {
const [testing, setTesting] = useState(false)
const handleTest = async () => {
setTesting(true)
try {
await testReportSchedule(siteId, schedule.id)
toast.success('Test report sent')
} catch (err) {
toast.error(getAuthErrorMessage(err as Error) || 'Failed to send test')
} finally {
setTesting(false)
}
}
const handleToggle = async () => {
try {
await updateReportSchedule(siteId, schedule.id, {
channel: schedule.channel,
channel_config: schedule.channel_config,
frequency: schedule.frequency,
report_type: schedule.report_type,
enabled: !schedule.enabled,
send_hour: schedule.send_hour,
send_day: schedule.send_day ?? undefined,
timezone: schedule.timezone,
purpose: schedule.purpose,
})
toast.success(schedule.enabled ? 'Report paused' : 'Report enabled')
onMutate()
} catch (err) {
toast.error(getAuthErrorMessage(err as Error) || 'Failed to update')
}
}
const handleDelete = async () => {
try {
await deleteReportSchedule(siteId, schedule.id)
toast.success('Report deleted')
onMutate()
} catch (err) {
toast.error(getAuthErrorMessage(err as Error) || 'Failed to delete')
}
}
return (
{schedule.channel === 'email' && 'recipients' in schedule.channel_config
? (schedule.channel_config as { recipients: string[] }).recipients?.[0]
: schedule.channel}
{!schedule.enabled && (paused)}
{schedule.frequency} ยท {schedule.report_type} report
)
}
export default function SiteReportsTab({ siteId }: { siteId: string }) {
const { data: reports = [], isLoading: reportsLoading, mutate: mutateReports } = useReportSchedules(siteId)
const { data: alerts = [], isLoading: alertsLoading, mutate: mutateAlerts } = useAlertSchedules(siteId)
const loading = reportsLoading || alertsLoading
if (loading) return
return (
{/* Scheduled Reports */}
Scheduled Reports
Automated analytics summaries via email or webhook.
{reports.length === 0 ? (
No scheduled reports yet.
) : (
{reports.map(r => (
mutateReports()} />
))}
)}
{/* Alert Channels */}
Alert Channels
Get notified when uptime monitors go down.
{alerts.length === 0 ? (
No alert channels configured.
) : (
{alerts.map(a => (
mutateAlerts()} />
))}
)}
)
}