File size: 2,861 Bytes
d70f132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState } from "react";
import {
  Bar,
  BarChart,
  CartesianGrid,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";

export interface TopicWer {
  topic: string;
  wer: number;
}

export interface TopicChartProps {
  data: TopicWer[];
}

/**
 * Horizontal per-topic WER bar chart. Rendered only when a run actually
 * recorded a per-topic breakdown (platform-verified runs).
 * Colors tuned for the lifted palette: category labels in full text ink,
 * brighter accent bars. The one-shot bar draw-in is gated on
 * prefers-reduced-motion (recharts animation does not read the CSS override).
 */
export default function TopicChart({ data }: TopicChartProps) {
  /* lazy init: only meaningful client-side — ResponsiveContainer measures
     on mount anyway, so there is no server-rendered chart to mismatch */
  const [animate] = useState(
    () =>
      typeof window !== "undefined" &&
      !window.matchMedia("(prefers-reduced-motion: reduce)").matches,
  );

  return (
    <div
      role="img"
      aria-label={`Per-topic WER: ${data
        .map((d) => `${d.topic} ${d.wer.toFixed(2)} percent`)
        .join(", ")}`}
      style={{ height: data.length * 44 + 40 }}
    >
      <ResponsiveContainer width="100%" height="100%">
        <BarChart
          data={data}
          layout="vertical"
          margin={{ top: 4, right: 28, bottom: 4, left: 8 }}
        >
          <CartesianGrid horizontal={false} stroke="var(--line)" />
          <XAxis
            type="number"
            unit="%"
            tick={{
              fill: "var(--muted)",
              fontSize: 11,
              fontFamily: "var(--font-mono)",
            }}
            axisLine={{ stroke: "var(--line)" }}
            tickLine={false}
          />
          <YAxis
            type="category"
            dataKey="topic"
            width={170}
            tick={{ fill: "var(--text)", fontSize: 12 }}
            axisLine={false}
            tickLine={false}
          />
          <Tooltip
            cursor={{ fill: "var(--accent-dim)" }}
            contentStyle={{
              background: "var(--surface-2)",
              border: "1px solid var(--line)",
              borderRadius: 2,
              fontFamily: "var(--font-mono)",
              fontSize: 12,
              color: "var(--text)",
            }}
            labelStyle={{ color: "var(--text)" }}
            formatter={(value) => [`${Number(value).toFixed(2)} %`, "WER"]}
          />
          <Bar
            dataKey="wer"
            fill="var(--accent)"
            fillOpacity={0.7}
            barSize={14}
            radius={[0, 2, 2, 0]}
            isAnimationActive={animate}
            animationDuration={700}
            animationEasing="ease-out"
          />
        </BarChart>
      </ResponsiveContainer>
    </div>
  );
}