Files
karaoke-gen/frontend/components/lyrics-review/AgenticCorrectionMetrics.tsx
T
Andrew Beveridge ☄️andClaude Opus 4.6 8c1dfe5460 feat(i18n): Phase 3 frontend — next-intl infrastructure + 1062 strings (en/es/de) (#656)
@coderabbitai ignore

## Summary
- Phase 3 of the i18n multilingual initiative — makes the karaoke-gen
frontend fully multilingual (English, Spanish, German)
- Installs `next-intl` with `[locale]` path-based routing,
LanguageSwitcher, and `Accept-Language` API header
- Extracts **1,062 strings** from ~70 components into `messages/en.json`
across 31 namespaces
- Wires `useTranslations()` into all user-facing components (landing,
dashboard, auth, job flow, lyrics review, instrumental review, audio
editor, etc.)
- Admin pages (`/admin/*`) stay outside locale routing — intentionally
excluded from i18n scope
- Static export generates `/en/`, `/es/`, `/de/` directories (45 total
pages) for Cloudflare Pages
- `es.json`/`de.json` are English placeholders — translation via Gemini
pipeline pending
- Translation pipeline (`scripts/translate.py`) and glossary (55
karaoke-specific terms) included

## Key architecture decisions
- Root layout keeps ThemeProvider/TenantProvider (needed by admin pages
outside `[locale]`)
- Only NextIntlClientProvider added to `[locale]/layout.tsx`
- `job-status.ts` labels converted to translation keys; consumers
translate via `useTranslations('jobStatus')`
- Jest mock loads actual `en.json` so existing tests validate i18n
wiring without assertion changes

## Test plan
- [x] All 707 frontend tests pass (39 suites)
- [x] Static build generates all 45 pages across 3 locales
- [x] Admin pages render at `/admin/` without locale prefix
- [x] CodeRabbit review: 4 issues fixed, 7 pre-existing noted
- [ ] Post-deploy: verify `/en/` landing page renders correctly
- [ ] Post-deploy: verify LanguageSwitcher redirects to `/es/` and
`/de/`
- [ ] Post-deploy: verify `/admin/` still works without locale context

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 21:50:53 -04:00

186 lines
6.2 KiB
TypeScript

'use client'
import { useTranslations } from 'next-intl'
import { useMemo } from 'react'
import { Card } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip'
import { WordCorrection } from '@/lib/lyrics-review/types'
import { cn } from '@/lib/utils'
interface GapCategoryMetric {
category: string
count: number
avgConfidence: number
corrections: WordCorrection[]
}
interface AgenticCorrectionMetricsProps {
corrections: WordCorrection[]
onCategoryClick?: (category: string) => void
onConfidenceFilterClick?: (filter: 'low' | 'high') => void
}
export default function AgenticCorrectionMetrics({
corrections,
onCategoryClick,
onConfidenceFilterClick,
}: AgenticCorrectionMetricsProps) {
const t = useTranslations('lyricsReview.agenticMetrics')
const metrics = useMemo(() => {
// Filter only agentic corrections
const agenticCorrections = corrections.filter((c) => c.handler === 'AgenticCorrector')
// Parse category from reason string (format: "reason [CATEGORY] (confidence: XX%)")
const categoryMap = new Map<string, GapCategoryMetric>()
agenticCorrections.forEach((correction) => {
const categoryMatch = correction.reason?.match(/\[([A-Z_]+)\]/)
const category = categoryMatch ? categoryMatch[1] : 'UNKNOWN'
if (!categoryMap.has(category)) {
categoryMap.set(category, {
category,
count: 0,
avgConfidence: 0,
corrections: [],
})
}
const metric = categoryMap.get(category)!
metric.count++
metric.corrections.push(correction)
})
// Calculate average confidence for each category
categoryMap.forEach((metric) => {
const totalConfidence = metric.corrections.reduce((sum, c) => sum + c.confidence, 0)
metric.avgConfidence = totalConfidence / metric.count
})
// Convert to array and sort by count descending
const sortedMetrics = Array.from(categoryMap.values()).sort((a, b) => b.count - a.count)
// Calculate overall stats
const totalCorrections = agenticCorrections.length
const avgConfidence =
totalCorrections > 0
? agenticCorrections.reduce((sum, c) => sum + c.confidence, 0) / totalCorrections
: 0
const lowConfidenceCount = agenticCorrections.filter((c) => c.confidence < 0.6).length
const highConfidenceCount = agenticCorrections.filter((c) => c.confidence >= 0.8).length
return {
categories: sortedMetrics,
totalCorrections,
avgConfidence,
lowConfidenceCount,
highConfidenceCount,
}
}, [corrections])
// Format category name for display
const formatCategory = (category: string): string => {
return category
.split('_')
.map((word) => word.charAt(0) + word.slice(1).toLowerCase())
.join(' ')
}
// Get emoji/icon for category
const getCategoryIcon = (category: string): string => {
const icons: Record<string, string> = {
SOUND_ALIKE: '',
PUNCTUATION_ONLY: '',
BACKGROUND_VOCALS: '',
EXTRA_WORDS: '',
REPEATED_SECTION: '',
COMPLEX_MULTI_ERROR: '',
AMBIGUOUS: '',
NO_ERROR: '',
}
return icons[category] || ''
}
return (
<TooltipProvider>
<Card className="p-2 h-full flex flex-col">
<span className="text-xs text-muted-foreground mb-1">{t('title')}</span>
{/* Overall stats */}
<div className="mb-2">
<p className="text-xs mb-1">
{t('total')} <strong>{metrics.totalCorrections}</strong>
</p>
<p className="text-xs mb-1">
{t('avgConfidence')} <strong>{(metrics.avgConfidence * 100).toFixed(0)}%</strong>
</p>
{/* Quick filters */}
<div className="flex gap-1 flex-wrap">
<Badge
variant="outline"
className="text-[0.65rem] h-5 cursor-pointer hover:bg-muted/30"
onClick={() => onConfidenceFilterClick?.('low')}
>
{t('low')} {metrics.lowConfidenceCount}
</Badge>
<Badge
variant="outline"
className="text-[0.65rem] h-5 cursor-pointer hover:bg-muted/30 text-green-600"
onClick={() => onConfidenceFilterClick?.('high')}
>
{t('high')} {metrics.highConfidenceCount}
</Badge>
</div>
</div>
{/* Category breakdown */}
<span className="text-xs text-muted-foreground mb-1">{t('byCategory')}</span>
<div className="flex-1 overflow-auto">
{metrics.categories.map((metric) => (
<Tooltip key={metric.category}>
<TooltipTrigger asChild>
<div
className="mb-1 p-1 rounded cursor-pointer hover:bg-muted/30 transition-colors"
onClick={() => onCategoryClick?.(metric.category)}
>
<div className="flex justify-between items-center">
<div className="flex items-center gap-1">
<span className="text-sm">{getCategoryIcon(metric.category)}</span>
<span className="text-xs">{formatCategory(metric.category)}</span>
</div>
<div className="flex gap-1 items-center">
<span className="text-[0.65rem] text-muted-foreground">
{(metric.avgConfidence * 100).toFixed(0)}%
</span>
<span className="text-xs font-semibold bg-accent px-1 rounded min-w-[24px] text-center">
{metric.count}
</span>
</div>
</div>
</div>
</TooltipTrigger>
<TooltipContent side="right">
<p>
{metric.count} correction{metric.count !== 1 ? 's' : ''} • Avg confidence:{' '}
{(metric.avgConfidence * 100).toFixed(0)}%
</p>
</TooltipContent>
</Tooltip>
))}
{metrics.categories.length === 0 && (
<p className="text-xs text-muted-foreground italic">{t('noCorrections')}</p>
)}
</div>
</Card>
</TooltipProvider>
)
}