All files / web/src/components/admin SystemHealthBanner.tsx

0% Statements 0/176
0% Branches 0/1
0% Functions 0/1
0% Lines 0/176

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                                                                                                                                                                                                                                                                                                                                                 
'use client'

import { useEffect, useState } from 'react'
import { css } from '../../../styled-system/css'
import { classifySongFailure } from '@/lib/session-song/classify-failure'
import type { SessionSongFailureKind } from '@/db/schema/session-songs'

interface SongFailureGroup {
  failureKind: SessionSongFailureKind | 'unknown'
  count: number
  latestErrorMessage: string | null
  latestAt: number
}

interface SystemHealthData {
  songFailures: SongFailureGroup[]
  windowHours: number
  generatedAt: number
}

/**
 * Reactive admin banner — flags server-level config issues by looking at the
 * trailing edge of the failure stream. Specifically calls out auth_invalid
 * and quota_exceeded since those affect every user, not just whoever
 * happened to play.
 */
export function SystemHealthBanner() {
  const [data, setData] = useState<SystemHealthData | null>(null)

  useEffect(() => {
    let cancelled = false
    const load = async () => {
      try {
        const res = await fetch('/api/admin/system-health')
        if (!res.ok || cancelled) return
        const body = (await res.json()) as SystemHealthData
        if (!cancelled) setData(body)
      } catch {
        // Banner is best-effort; fail silently.
      }
    }
    load()
    const interval = setInterval(load, 60_000)
    return () => {
      cancelled = true
      clearInterval(interval)
    }
  }, [])

  if (!data) return null

  // Only escalate on server-level kinds. Skip rate_limited (transient) and
  // unknown (could be one-offs), and skip transient.
  const escalating = data.songFailures.filter(
    (g) =>
      g.failureKind === 'missing_config' ||
      g.failureKind === 'auth_invalid' ||
      g.failureKind === 'quota_exceeded'
  )
  if (escalating.length === 0) return null

  return (
    <div
      data-component="admin-system-health-banner"
      className={css({
        marginBottom: '16px',
        padding: '14px 16px',
        borderRadius: '8px',
        backgroundColor: '#3a1f1f',
        borderLeft: '4px solid #f85149',
        color: '#f0f6fc',
      })}
    >
      <div
        className={css({
          fontSize: '14px',
          fontWeight: 'bold',
          marginBottom: '6px',
          display: 'flex',
          alignItems: 'center',
          gap: '8px',
        })}
      >
        <span aria-hidden>⚠️</span>
        Session songs are failing for users
      </div>
      <p
        className={css({
          fontSize: '12px',
          color: '#c9d1d9',
          marginBottom: '10px',
        })}
      >
        {escalating.reduce((acc, g) => acc + g.count, 0)} failed song{' '}
        {escalating.reduce((acc, g) => acc + g.count, 0) === 1 ? 'request' : 'requests'} in the
        last {data.windowHours}h.
      </p>
      <ul
        className={css({
          margin: 0,
          padding: 0,
          listStyle: 'none',
          display: 'flex',
          flexDirection: 'column',
          gap: '6px',
        })}
      >
        {escalating.map((g) => {
          const classified = classifySongFailure(g.latestErrorMessage ?? g.failureKind)
          return (
            <li
              key={g.failureKind}
              data-failure-kind={g.failureKind}
              className={css({
                fontSize: '13px',
                padding: '8px 10px',
                borderRadius: '6px',
                backgroundColor: '#1f1414',
                display: 'flex',
                flexDirection: 'column',
                gap: '4px',
              })}
            >
              <div
                className={css({
                  display: 'flex',
                  alignItems: 'center',
                  gap: '8px',
                  flexWrap: 'wrap',
                })}
              >
                <span
                  className={css({
                    fontSize: '11px',
                    fontWeight: 'bold',
                    textTransform: 'uppercase',
                    letterSpacing: '0.5px',
                    color: '#f85149',
                  })}
                >
                  {g.failureKind.replace('_', ' ')}
                </span>
                <span className={css({ color: '#8b949e', fontSize: '11px' })}>
                  ×{g.count} ·{' '}
                  {new Date(g.latestAt).toLocaleString(undefined, {
                    month: 'short',
                    day: 'numeric',
                    hour: 'numeric',
                    minute: '2-digit',
                  })}
                </span>
              </div>
              <span className={css({ color: '#c9d1d9' })}>{classified.ownerMessage}</span>
              {classified.remediation && (
                <a
                  href={classified.remediation.href}
                  target="_blank"
                  rel="noopener noreferrer"
                  data-action={`remediate-${g.failureKind}`}
                  className={css({
                    color: '#58a6ff',
                    fontSize: '12px',
                    textDecoration: 'none',
                    _hover: { textDecoration: 'underline' },
                  })}
                >
                  {classified.remediation.label} →
                </a>
              )}
            </li>
          )
        })}
      </ul>
    </div>
  )
}