"use client"; import React, { useCallback, useMemo, useRef, useState } from "react"; import { FaMedal } from "react-icons/fa"; import { contentValue, type LocalizedText } from "@/lib/i18n"; import { TRIWORLD_COLORS, TRIWORLD_TABLE_METRICS, type TriWorldBoardEntry, } from "./metrics"; import { LocalizedMarkdown } from "./LocalizedMarkdown"; type MetricText = { code: string; label: LocalizedText; shortLabel: LocalizedText; title: LocalizedText; description?: LocalizedText; }; type Props = { rows: TriWorldBoardEntry[]; metricTexts: Record; descriptionText: LocalizedText; }; const RANGE_SIZE = 20; function DualText({ en, zh }: { en: React.ReactNode; zh: React.ReactNode }) { return ( <> {en} {zh} ); } function displayScore(value: number | null | undefined): string { if (value === null || value === undefined || !Number.isFinite(Number(value))) return "TBA"; return Number(value).toFixed(2); } function textFor( metricTexts: Record, code: string, fallback: string ): LocalizedText { return metricTexts[code]?.label || metricTexts[code]?.shortLabel || { en: fallback, zh: fallback }; } function headerLines(value: string): string[] { const words = value.trim().split(/\s+/).filter(Boolean); if (words.length <= 2) return [value.trim()]; const lines: string[] = []; for (let index = 0; index < words.length; index += 2) { lines.push(words.slice(index, index + 2).join(" ")); } return lines; } function MetricHeaderText({ text }: { text: LocalizedText }) { const en = contentValue(text.en); const zh = contentValue(text.zh); return ( <> {en ? ( {headerLines(en).map((line, index) => ( {line} ))} ) : null} {zh ? ( {headerLines(zh).map((line, index) => ( {line} ))} ) : null} ); } function valueFor(row: TriWorldBoardEntry, code: string): number | null { return code === "TWBScore" ? row.score : row.metrics[code] ?? null; } function isScore(value: number | null): value is number { return value !== null && Number.isFinite(value); } export function TriWorldLeaderboard({ rows, metricTexts, descriptionText }: Props) { const [query, setQuery] = useState(""); const [highlightedName, setHighlightedName] = useState(null); const [message, setMessage] = useState(""); const rangeMenuRef = useRef(null); const rowRefs = useRef(new Map()); const ranges = useMemo(() => { const total = Math.ceil(rows.length / RANGE_SIZE); return Array.from({ length: total }, (_, index) => { const start = index * RANGE_SIZE; const end = Math.min(start + RANGE_SIZE, rows.length); return { index, start, end, label: start === 0 ? `Top ${end}` : `Ranks ${start + 1}-${end}` }; }); }, [rows.length]); const metricRanks = useMemo(() => { const ranksByMetric = new Map>(); for (const metric of TRIWORLD_TABLE_METRICS) { const rankedRows = rows .map((row) => ({ row, value: valueFor(row, metric.code) })) .filter((entry): entry is { row: TriWorldBoardEntry; value: number } => isScore(entry.value)) .sort((left, right) => right.value - left.value); const ranks = new Map(); let previousValue: number | null = null; let previousRank = 0; rankedRows.forEach((entry, index) => { const rank = previousValue !== null && entry.value === previousValue ? previousRank : index + 1; ranks.set(entry.row, rank); previousValue = entry.value; previousRank = rank; }); ranksByMetric.set(metric.code, ranks); } return ranksByMetric; }, [rows]); const locate = useCallback((name = query) => { const normalized = name.trim().toLocaleLowerCase(); if (!normalized) { setHighlightedName(null); setMessage(""); return; } const matched = rows.find((row) => row.modelName.toLocaleLowerCase() === normalized) || rows.find((row) => row.modelName.toLocaleLowerCase().includes(normalized)); if (!matched) { setHighlightedName(null); setMessage(`No published model matches "${name.trim()}".`); return; } setHighlightedName(matched.modelName); setMessage(`Located ${matched.modelName} at rank ${matched.rank}.`); window.requestAnimationFrame(() => { rowRefs.current.get(matched.modelName)?.scrollIntoView({ behavior: "smooth", block: "center", inline: "nearest" }); }); }, [query, rows]); const jumpToRange = (start: number) => { const row = rows[start]; if (!row) return; setHighlightedName(null); setMessage(""); rangeMenuRef.current?.removeAttribute("open"); window.requestAnimationFrame(() => { rowRefs.current.get(row.modelName)?.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" }); }); }; return (

{ranges.map((range) => ( ))}
{ event.preventDefault(); locate(); }} > setQuery(event.target.value)} placeholder="Model name" autoComplete="off" />
{message ?

{message}

: null}
{TRIWORLD_TABLE_METRICS.map((metric) => { const label = textFor(metricTexts, metric.code, metric.code); return ( ); })} {rows.flatMap((row, index) => { const color = TRIWORLD_COLORS[index % TRIWORLD_COLORS.length]; const range = ranges.find((item) => item.start === index); const isHighlighted = highlightedName === row.modelName; const entry = ( { if (node) rowRefs.current.set(row.modelName, node); else rowRefs.current.delete(row.modelName); }} data-highlighted={isHighlighted ? "true" : undefined} style={{ "--model-highlight": color } as React.CSSProperties} > {TRIWORLD_TABLE_METRICS.map((metric) => { const value = valueFor(row, metric.code); const metricRank = metricRanks.get(metric.code)?.get(row) ?? null; return ( ); })} ); return range ? [ , entry, ] : [entry]; })}
{row.modelName} {row.teamName ? {row.teamName} : null} {row.rank}
{metricRank !== null ? ( {metricRank <= 3 ? ( ) : null} {displayScore(value)}
); }