Skip to content

Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.

8.5 KB
Raw Blame
'use client'

import { useEffect, useState } from 'react'
import { getJson } from './fetch'
import { StatCard, Card, Chip } from './bits'
import { BarChart, LineChart, HBars, fmt, fmtBytes } from './charts'
import { AdminRealtime } from './realtime'

interface DayPoint {
  day: string
  value: number
}
interface AiDayPoint extends DayPoint {
  ok: number
}
interface Overview {
  version: string
  build?: string
  dbLatencyMs: number
  totals: {
    users: number
    designs: number
    templates: number
    mediaCount: number
    mediaBytes: number
    comments: number
    aiCalls: number
    activeSessions: number
  }
  windows: { requests24h: number; requests7d: number; ai7d: number; dauToday: number }
  series: { traffic: DayPoint[]; dau: DayPoint[]; signups: DayPoint[]; aiCalls: AiDayPoint[] }
  topRoutes: { route: string; method: string; hits: number }[]
  aiByFeature: { feature: string; calls: number; ok: number }[]
  recentUsers: { id: string; username: string; role: string; createdAt: string; admin: boolean }[]
  provider: { configured: boolean; source: string | null; providerCount: number; slotsSet: number }
  /** v0.23: BYOK adoption — users who added their own API keys */
  byok: {
    users: number
    providers: number
    slots: Record<string, number>
  }
}

const SOURCE_LABEL: Record<string, string> = {
  multi: 'providers table',
  db: 'console override',
  env: 'environment',
  file: 'config file',
}

export function AdminOverview() {
  const [data, setData] = useState<Overview | null>(null)
  const [error, setError] = useState<string | null>(null)

  useEffect(() => {
    let alive = true
    getJson<Overview>('/api/admin/overview').then((res) => {
      if (!alive) return
      if (res.ok) setData(res.data)
      else setError(res.data?.error ?? `Request failed (${res.status})`)
    })
    return () => {
      alive = false
    }
  }, [])

  if (error) {
    return (
      <Card title="Overview">
        <p className="text-sm text-destructive">{error}</p>
      </Card>
    )
  }
  if (!data) {
    return (
      <div className="grid grid-cols-2 gap-4 md:grid-cols-4">
        {Array.from({ length: 8 }).map((_, i) => (
          <div key={i} className="h-24 animate-pulse rounded-xl bg-muted" />
        ))}
      </div>
    )
  }

  const aiFail7d = Math.max(
    0,
    data.series.aiCalls.slice(-7).reduce((a, p) => a + Math.max(0, p.value - p.ok), 0)
  )

  return (
    <>
      <AdminRealtime />

      <div className="grid grid-cols-2 gap-4 md:grid-cols-4">
        <StatCard label="Users" value={fmt(data.totals.users)} sub={`${data.windows.dauToday} active today`} />
        <StatCard label="Designs" value={fmt(data.totals.designs)} sub={`${fmt(data.totals.templates)} templates`} />
        <StatCard label="API requests" value={fmt(data.windows.requests24h)} sub={`${fmt(data.windows.requests7d)} in 7 days`} />
        <StatCard
          label="AI calls"
          value={fmt(data.windows.ai7d)}
          sub={`7d · ${aiFail7d > 0 ? `${fmt(aiFail7d)} failed` : 'no failures'}`}
        />
        <StatCard label="Active sessions" value={fmt(data.totals.activeSessions)} sub="unexpired" />
        <StatCard label="Media stored" value={fmtBytes(data.totals.mediaBytes)} sub={`${fmt(data.totals.mediaCount)} files`} />
        <StatCard label="Comments" value={fmt(data.totals.comments)} sub="all designs" />
        <StatCard
          label="AI providers"
          value={data.provider.configured ? (data.provider.source === 'multi' ? String(data.provider.providerCount) : 'configured') : 'none'}
          sub={
            data.provider.configured
              ? data.provider.source === 'multi'
                ? `${data.provider.slotsSet}/5 slots wired · via ${SOURCE_LABEL[data.provider.source] ?? data.provider.source}`
                : `via ${SOURCE_LABEL[data.provider.source ?? ''] ?? data.provider.source}`
              : 'local features only'
          }
        />
        {/* v0.23 BYOK adoption: how many users brought their own key.
            users = distinct accounts with ≥1 UserAIProvider row; the sub
            line counts how many of the five slots user keys cover. */}
        <StatCard
          label="Own API keys"
          value={data.byok.users > 0 ? fmt(data.byok.users) : 'none'}
          sub={
            data.byok.users > 0
              ? `${fmt(data.byok.providers)} key${data.byok.providers === 1 ? '' : 's'} · ${Object.values(data.byok.slots).filter((n) => n > 0).length}/5 slots covered`
              : 'no user-added keys (BYOK)'
          }
        />
      </div>

      <div className="grid gap-5 lg:grid-cols-2">
        <Card
          title="API traffic — last 14 days"
          right={<Chip>{fmt(data.series.traffic.reduce((a, p) => a + p.value, 0))} total</Chip>}
        >
          <BarChart bars={data.series.traffic.map((p) => ({ label: p.day, value: p.value }))} />
        </Card>
        <Card title="Daily active users — last 14 days" right={<Chip tone={data.windows.dauToday > 0 ? 'good' : 'neutral'}>{data.windows.dauToday} today</Chip>}>
          <LineChart points={data.series.dau.map((p) => ({ label: p.day, value: p.value }))} />
        </Card>
        <Card title="New sign-ups — last 14 days">
          <BarChart bars={data.series.signups.map((p) => ({ label: p.day, value: p.value }))} color="var(--cv-violet)" />
        </Card>
        <Card title="AI calls — last 14 days" right={<Chip tone={aiFail7d > 0 ? 'bad' : 'good'}>teal = ok · red = failed</Chip>}>
          <BarChart
            bars={data.series.aiCalls.map((p) => ({
              label: p.day,
              value: Math.max(0, p.value - Math.max(0, p.value - p.ok)),
              alt: Math.max(0, p.value - p.ok),
            }))}
          />
        </Card>
      </div>

      <div className="grid gap-5 lg:grid-cols-2">
        <Card title="Top routes — last 7 days">
          <HBars
            rows={data.topRoutes.map((r) => ({
              label: `${r.method} ${r.route.replace('/api/', '')}`,
              value: r.hits,
              hint: `${r.method} ${r.route}`,
            }))}
          />
        </Card>
        <Card title="AI usage by feature — last 7 days">
          <HBars
            rows={data.aiByFeature.map((f) => ({
              label: f.feature,
              value: f.calls,
              hint: `${f.calls} calls · ${f.ok} succeeded`,
            }))}
            color="var(--cv-violet)"
          />
        </Card>
      </div>

      <div className="grid gap-5 lg:grid-cols-2">
        <Card title="Newest accounts">
          <ul className="divide-y">
            {data.recentUsers.length === 0 && <li className="py-2 text-sm text-muted-foreground">no accounts yet</li>}
            {data.recentUsers.map((u) => (
              <li key={u.id} className="flex items-center justify-between py-2 text-sm">
                <span className="font-medium">{u.username}</span>
                <span className="flex items-center gap-2 text-xs text-muted-foreground">
                  {u.admin && <Chip tone="good">admin</Chip>}
                  {new Date(u.createdAt).toLocaleDateString()}
                </span>
              </li>
            ))}
          </ul>
        </Card>
        <Card title="Instance">
          <dl className="grid grid-cols-2 gap-x-4 gap-y-2 text-sm">
            <dt className="text-muted-foreground">Canvix version</dt>
            <dd className="text-right font-medium tabular-nums">{data.version}{data.build ? <span className="text-muted-foreground"> · {data.build}</span> : null}</dd>
            <dt className="text-muted-foreground">Database latency</dt>
            <dd className="text-right font-medium tabular-nums">{data.dbLatencyMs} ms</dd>
            <dt className="text-muted-foreground">AI providers</dt>
            <dd className="text-right font-medium">
              {data.provider.configured
                ? data.provider.source === 'multi'
                  ? `${data.provider.providerCount} in table · ${data.provider.slotsSet}/5 slots wired`
                  : `configured · ${SOURCE_LABEL[data.provider.source ?? ''] ?? data.provider.source}`
                : 'not configured'}
            </dd>
          </dl>
          <p className="mt-4 text-xs text-muted-foreground">
            Live counters are in-memory (reset on restart); the charts above are the durable record. Traffic counters are aggregate
            counts only — no ids, no IPs, no query strings. 90-day retention. Disable with{' '}
            <code className="rounded bg-muted px-1">CANVIX_TRAFFIC=0</code>.
          </p>
        </Card>
      </div>
    </>
  )
}