import 'server-only'; import { env } from '@konobangu/env'; import type { BetterStackResponse } from './types'; export const Status = async () => { if (!env.BETTERSTACK_API_KEY || !env.BETTERSTACK_URL) { return null; } let statusColor = 'bg-muted-foreground'; let statusLabel = 'Unable to fetch status'; try { const response = await fetch( 'https://uptime.betterstack.com/api/v2/monitors', { headers: { Authorization: `Bearer ${env.BETTERSTACK_API_KEY}`, }, } ); if (!response.ok) { throw new Error('Failed to fetch status'); } const { data } = (await response.json()) as BetterStackResponse; const status = data.filter((monitor) => monitor.attributes.status === 'up').length / data.length; if (status === 0) { statusColor = 'bg-destructive'; statusLabel = 'Degraded performance'; } else if (status < 1) { statusColor = 'bg-warning'; statusLabel = 'Partial outage'; } else { statusColor = 'bg-success'; statusLabel = 'All systems normal'; } } catch { statusColor = 'bg-muted-foreground'; statusLabel = 'Unable to fetch status'; } return ( {statusLabel} ); };