Files
argument.es/frontend.tsx

508 lines
16 KiB
TypeScript
Raw Normal View History

2026-02-20 04:49:40 -08:00
import React, { useState, useEffect } from "react";
2026-02-19 22:46:41 -08:00
import { createRoot } from "react-dom/client";
import "./frontend.css";
2026-02-20 04:49:40 -08:00
// ── Types ────────────────────────────────────────────────────────────────────
2026-02-19 22:46:41 -08:00
type Model = { id: string; name: string };
2026-02-22 00:23:35 -08:00
type TaskInfo = {
model: Model;
startedAt: number;
finishedAt?: number;
result?: string;
error?: string;
};
type VoteInfo = {
voter: Model;
startedAt: number;
finishedAt?: number;
votedFor?: Model;
error?: boolean;
};
2026-02-19 22:46:41 -08:00
type RoundState = {
num: number;
phase: "prompting" | "answering" | "voting" | "done";
prompter: Model;
promptTask: TaskInfo;
prompt?: string;
contestants: [Model, Model];
answerTasks: [TaskInfo, TaskInfo];
votes: VoteInfo[];
scoreA?: number;
scoreB?: number;
};
2026-02-22 00:23:35 -08:00
type GameState = {
completed: RoundState[];
active: RoundState | null;
scores: Record<string, number>;
done: boolean;
2026-02-22 01:20:19 -08:00
isPaused: boolean;
generation: number;
2026-02-22 00:23:35 -08:00
};
type ServerMessage = {
type: "state";
data: GameState;
totalRounds: number;
viewerCount: number;
2026-02-22 03:12:44 -08:00
version?: string;
2026-02-22 00:23:35 -08:00
};
2026-02-19 23:28:03 -08:00
2026-02-20 04:49:40 -08:00
// ── Model colors & logos ─────────────────────────────────────────────────────
2026-02-19 23:28:03 -08:00
const MODEL_COLORS: Record<string, string> = {
"Gemini 3.1 Pro": "#4285F4",
"Kimi K2": "#00E599",
"DeepSeek 3.2": "#4D6BFE",
"GLM-5": "#1F63EC",
"GPT-5.2": "#10A37F",
"Opus 4.6": "#D97757",
"Sonnet 4.6": "#D97757",
"Grok 4.1": "#FFFFFF",
2026-02-20 02:52:39 -08:00
"MiniMax 2.5": "#FF3B30",
2026-02-19 22:46:41 -08:00
};
2026-02-19 23:28:03 -08:00
function getColor(name: string): string {
return MODEL_COLORS[name] ?? "#A1A1A1";
2026-02-19 22:46:41 -08:00
}
2026-02-19 23:28:03 -08:00
function getLogo(name: string): string | null {
if (name.includes("Gemini")) return "/assets/logos/gemini.svg";
if (name.includes("Kimi")) return "/assets/logos/kimi.svg";
if (name.includes("DeepSeek")) return "/assets/logos/deepseek.svg";
if (name.includes("GLM")) return "/assets/logos/glm.svg";
if (name.includes("GPT")) return "/assets/logos/openai.svg";
2026-02-22 00:23:35 -08:00
if (name.includes("Opus") || name.includes("Sonnet"))
return "/assets/logos/claude.svg";
2026-02-19 23:28:03 -08:00
if (name.includes("Grok")) return "/assets/logos/grok.svg";
2026-02-20 02:52:39 -08:00
if (name.includes("MiniMax")) return "/assets/logos/minimax.svg";
2026-02-19 23:28:03 -08:00
return null;
2026-02-19 22:46:41 -08:00
}
2026-02-20 04:49:40 -08:00
// ── Helpers ──────────────────────────────────────────────────────────────────
2026-02-19 22:46:41 -08:00
2026-02-20 04:49:40 -08:00
function Dots() {
2026-02-22 00:23:35 -08:00
return (
<span className="dots">
<span>.</span>
<span>.</span>
<span>.</span>
</span>
);
2026-02-19 22:46:41 -08:00
}
2026-02-20 04:49:40 -08:00
function ModelTag({ model, small }: { model: Model; small?: boolean }) {
2026-02-19 23:28:03 -08:00
const logo = getLogo(model.name);
const color = getColor(model.name);
return (
2026-02-22 00:23:35 -08:00
<span
className={`model-tag ${small ? "model-tag--sm" : ""}`}
style={{ color }}
>
2026-02-20 04:49:40 -08:00
{logo && <img src={logo} alt="" className="model-tag__logo" />}
2026-02-19 23:28:03 -08:00
{model.name}
</span>
);
2026-02-19 22:46:41 -08:00
}
2026-02-20 04:49:40 -08:00
// ── Prompt ───────────────────────────────────────────────────────────────────
2026-02-19 23:28:03 -08:00
function PromptCard({ round }: { round: RoundState }) {
if (round.phase === "prompting" && !round.prompt) {
return (
2026-02-20 04:49:40 -08:00
<div className="prompt">
<div className="prompt__by">
2026-02-22 00:23:35 -08:00
<ModelTag model={round.prompter} small /> is writing a prompt
<Dots />
</div>
<div className="prompt__text prompt__text--loading">
<Dots />
2026-02-19 23:28:03 -08:00
</div>
</div>
);
}
if (round.promptTask.error) {
return (
2026-02-20 04:49:40 -08:00
<div className="prompt">
2026-02-22 00:23:35 -08:00
<div className="prompt__text prompt__text--error">
Prompt generation failed
</div>
2026-02-19 23:28:03 -08:00
</div>
);
}
2026-02-19 22:46:41 -08:00
return (
2026-02-20 04:49:40 -08:00
<div className="prompt">
2026-02-22 00:23:35 -08:00
<div className="prompt__by">
Prompted by <ModelTag model={round.prompter} small />
</div>
2026-02-20 04:49:40 -08:00
<div className="prompt__text">{round.prompt}</div>
2026-02-19 23:28:03 -08:00
</div>
);
}
2026-02-20 04:49:40 -08:00
// ── Contestant ───────────────────────────────────────────────────────────────
function ContestantCard({
2026-02-22 00:23:35 -08:00
task,
voteCount,
totalVotes,
isWinner,
showVotes,
voters,
2026-02-19 23:28:03 -08:00
}: {
task: TaskInfo;
voteCount: number;
totalVotes: number;
isWinner: boolean;
showVotes: boolean;
2026-02-19 23:40:03 -08:00
voters: VoteInfo[];
2026-02-19 23:28:03 -08:00
}) {
const color = getColor(task.model.name);
const pct = totalVotes > 0 ? Math.round((voteCount / totalVotes) * 100) : 0;
return (
2026-02-20 04:49:40 -08:00
<div
className={`contestant ${isWinner ? "contestant--winner" : ""}`}
style={{ "--accent": color } as React.CSSProperties}
>
<div className="contestant__head">
<ModelTag model={task.model} />
{isWinner && <span className="win-tag">WIN</span>}
2026-02-19 23:28:03 -08:00
</div>
2026-02-20 04:49:40 -08:00
<div className="contestant__body">
2026-02-19 23:28:03 -08:00
{!task.finishedAt ? (
2026-02-22 00:23:35 -08:00
<p className="answer answer--loading">
<Dots />
</p>
2026-02-19 23:28:03 -08:00
) : task.error ? (
2026-02-20 04:49:40 -08:00
<p className="answer answer--error">{task.error}</p>
2026-02-19 23:28:03 -08:00
) : (
2026-02-20 04:49:40 -08:00
<p className="answer">&ldquo;{task.result}&rdquo;</p>
2026-02-19 22:46:41 -08:00
)}
</div>
2026-02-19 23:28:03 -08:00
{showVotes && (
2026-02-20 04:49:40 -08:00
<div className="contestant__foot">
<div className="vote-bar">
2026-02-22 00:23:35 -08:00
<div
className="vote-bar__fill"
style={{ width: `${pct}%`, background: color }}
/>
2026-02-19 23:28:03 -08:00
</div>
2026-02-20 04:49:40 -08:00
<div className="vote-meta">
2026-02-22 00:23:35 -08:00
<span className="vote-meta__count" style={{ color }}>
{voteCount}
</span>
<span className="vote-meta__label">
vote{voteCount !== 1 ? "s" : ""}
</span>
2026-02-20 04:49:40 -08:00
<span className="vote-meta__dots">
{voters.map((v, i) => {
const logo = getLogo(v.voter.name);
return logo ? (
2026-02-22 00:23:35 -08:00
<img
key={i}
src={logo}
alt={v.voter.name}
title={v.voter.name}
className="voter-dot"
/>
2026-02-20 04:49:40 -08:00
) : (
2026-02-22 00:23:35 -08:00
<span
key={i}
className="voter-dot voter-dot--letter"
style={{ color: getColor(v.voter.name) }}
title={v.voter.name}
>
2026-02-20 04:49:40 -08:00
{v.voter.name[0]}
</span>
);
})}
</span>
2026-02-19 23:28:03 -08:00
</div>
2026-02-19 22:46:41 -08:00
</div>
)}
2026-02-19 23:28:03 -08:00
</div>
);
}
2026-02-19 22:46:41 -08:00
2026-02-20 04:49:40 -08:00
// ── Arena ─────────────────────────────────────────────────────────────────────
2026-02-19 23:28:03 -08:00
2026-02-20 04:49:40 -08:00
function Arena({ round, total }: { round: RoundState; total: number | null }) {
2026-02-19 23:28:03 -08:00
const [contA, contB] = round.contestants;
const showVotes = round.phase === "voting" || round.phase === "done";
const isDone = round.phase === "done";
2026-02-22 00:23:35 -08:00
let votesA = 0,
votesB = 0;
2026-02-19 23:28:03 -08:00
for (const v of round.votes) {
if (v.votedFor?.name === contA.name) votesA++;
else if (v.votedFor?.name === contB.name) votesB++;
}
const totalVotes = votesA + votesB;
2026-02-22 00:23:35 -08:00
const votersA = round.votes.filter((v) => v.votedFor?.name === contA.name);
const votersB = round.votes.filter((v) => v.votedFor?.name === contB.name);
2026-02-19 23:28:03 -08:00
2026-02-20 04:49:40 -08:00
const phaseText =
2026-02-22 00:23:35 -08:00
round.phase === "prompting"
? "Writing prompt"
: round.phase === "answering"
? "Answering"
: round.phase === "voting"
? "Judges voting"
: "Complete";
2026-02-19 23:28:03 -08:00
return (
<div className="arena">
2026-02-20 04:49:40 -08:00
<div className="arena__meta">
<span className="arena__round">
2026-02-22 00:23:35 -08:00
Round {round.num}
{total ? <span className="dim">/{total}</span> : null}
2026-02-20 04:49:40 -08:00
</span>
<span className="arena__phase">{phaseText}</span>
2026-02-19 23:28:03 -08:00
</div>
<PromptCard round={round} />
{round.phase !== "prompting" && (
2026-02-20 04:49:40 -08:00
<div className="showdown">
<ContestantCard
task={round.answerTasks[0]}
voteCount={votesA}
totalVotes={totalVotes}
isWinner={isDone && votesA > votesB}
showVotes={showVotes}
voters={votersA}
/>
<ContestantCard
task={round.answerTasks[1]}
voteCount={votesB}
totalVotes={totalVotes}
isWinner={isDone && votesB > votesA}
showVotes={showVotes}
voters={votersB}
/>
</div>
)}
2026-02-19 23:28:03 -08:00
2026-02-20 04:49:40 -08:00
{isDone && votesA === votesB && totalVotes > 0 && (
<div className="tie-label">Tie</div>
2026-02-19 23:28:03 -08:00
)}
</div>
);
}
2026-02-20 04:49:40 -08:00
// ── Game Over ────────────────────────────────────────────────────────────────
2026-02-19 23:28:03 -08:00
function GameOver({ scores }: { scores: Record<string, number> }) {
const sorted = Object.entries(scores).sort((a, b) => b[1] - a[1]);
const champion = sorted[0];
return (
<div className="game-over">
2026-02-20 04:49:40 -08:00
<div className="game-over__label">Game Over</div>
2026-02-19 23:28:03 -08:00
{champion && champion[1] > 0 && (
2026-02-20 04:49:40 -08:00
<div className="game-over__winner">
<span className="game-over__crown">👑</span>
2026-02-22 00:23:35 -08:00
<span
className="game-over__name"
style={{ color: getColor(champion[0]) }}
>
2026-02-19 23:28:03 -08:00
{getLogo(champion[0]) && <img src={getLogo(champion[0])!} alt="" />}
{champion[0]}
2026-02-20 04:49:40 -08:00
</span>
<span className="game-over__sub">is the funniest AI</span>
2026-02-19 22:46:41 -08:00
</div>
)}
</div>
);
}
2026-02-20 04:49:40 -08:00
// ── Standings ────────────────────────────────────────────────────────────────
2026-02-22 00:23:35 -08:00
function Standings({
scores,
activeRound,
}: {
scores: Record<string, number>;
activeRound: RoundState | null;
}) {
2026-02-19 22:46:41 -08:00
const sorted = Object.entries(scores).sort((a, b) => b[1] - a[1]);
const maxScore = sorted[0]?.[1] || 1;
2026-02-19 23:28:03 -08:00
const competing = activeRound
2026-02-22 00:23:35 -08:00
? new Set([
activeRound.contestants[0].name,
activeRound.contestants[1].name,
])
2026-02-19 23:28:03 -08:00
: new Set<string>();
2026-02-19 22:46:41 -08:00
return (
2026-02-20 04:49:40 -08:00
<aside className="standings">
<div className="standings__head">
<span className="standings__title">Standings</span>
2026-02-22 01:20:19 -08:00
<div className="standings__links">
<a href="/history" className="standings__link">
History
</a>
2026-02-22 16:02:56 -08:00
<a href="https://twitch.tv/quipslop" target="_blank" rel="noopener noreferrer" className="standings__link">
Twitch
</a>
<a href="https://github.com/T3-Content/quipslop" target="_blank" rel="noopener noreferrer" className="standings__link">
GitHub
2026-02-22 01:20:19 -08:00
</a>
</div>
2026-02-19 23:28:03 -08:00
</div>
2026-02-20 04:49:40 -08:00
<div className="standings__list">
{sorted.map(([name, score], i) => {
const pct = maxScore > 0 ? Math.round((score / maxScore) * 100) : 0;
const color = getColor(name);
const active = competing.has(name);
return (
2026-02-22 00:23:35 -08:00
<div
key={name}
className={`standing ${active ? "standing--active" : ""}`}
>
<span className="standing__rank">
{i === 0 && score > 0 ? "👑" : i + 1}
</span>
2026-02-20 04:49:40 -08:00
<ModelTag model={{ id: name, name }} small />
<div className="standing__bar">
2026-02-22 00:23:35 -08:00
<div
className="standing__fill"
style={{ width: `${pct}%`, background: color }}
/>
2026-02-20 04:49:40 -08:00
</div>
<span className="standing__score">{score}</span>
</div>
);
})}
2026-02-20 00:28:48 -08:00
</div>
2026-02-19 23:28:03 -08:00
</aside>
);
}
2026-02-20 04:49:40 -08:00
// ── Connecting ───────────────────────────────────────────────────────────────
2026-02-19 23:28:03 -08:00
function ConnectingScreen() {
return (
<div className="connecting">
2026-02-22 00:23:35 -08:00
<div className="connecting__logo">
<img src="/assets/logo.svg" alt="quipslop" />
</div>
<div className="connecting__sub">
Connecting
<Dots />
</div>
2026-02-19 22:46:41 -08:00
</div>
);
}
2026-02-20 04:49:40 -08:00
// ── App ──────────────────────────────────────────────────────────────────────
2026-02-19 23:28:03 -08:00
2026-02-19 22:46:41 -08:00
function App() {
const [state, setState] = useState<GameState | null>(null);
2026-02-20 04:49:40 -08:00
const [totalRounds, setTotalRounds] = useState<number | null>(null);
2026-02-22 00:11:20 -08:00
const [viewerCount, setViewerCount] = useState(0);
2026-02-19 22:46:41 -08:00
const [connected, setConnected] = useState(false);
useEffect(() => {
2026-02-20 03:55:36 -08:00
const wsProtocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${wsProtocol}//${window.location.host}/ws`;
2026-02-19 22:46:41 -08:00
let ws: WebSocket;
let reconnectTimer: ReturnType<typeof setTimeout>;
2026-02-22 03:12:44 -08:00
let knownVersion: string | null = null;
2026-02-19 22:46:41 -08:00
function connect() {
ws = new WebSocket(wsUrl);
ws.onopen = () => setConnected(true);
ws.onclose = () => {
setConnected(false);
reconnectTimer = setTimeout(connect, 2000);
};
ws.onmessage = (e) => {
const msg: ServerMessage = JSON.parse(e.data);
if (msg.type === "state") {
2026-02-22 03:12:44 -08:00
if (msg.version) {
if (!knownVersion) knownVersion = msg.version;
else if (knownVersion !== msg.version) return location.reload();
}
2026-02-19 22:46:41 -08:00
setState(msg.data);
setTotalRounds(msg.totalRounds);
2026-02-22 00:11:20 -08:00
setViewerCount(msg.viewerCount);
2026-02-19 22:46:41 -08:00
}
};
}
connect();
return () => {
clearTimeout(reconnectTimer);
ws?.close();
};
}, []);
2026-02-20 04:49:40 -08:00
if (!connected || !state) return <ConnectingScreen />;
const lastCompleted = state.completed[state.completed.length - 1];
2026-02-22 00:23:35 -08:00
const isNextPrompting =
state.active?.phase === "prompting" && !state.active.prompt;
const displayRound =
isNextPrompting && lastCompleted ? lastCompleted : state.active;
2026-02-19 22:46:41 -08:00
return (
2026-02-19 23:28:03 -08:00
<div className="app">
<div className="layout">
2026-02-20 04:49:40 -08:00
<main className="main">
<header className="header">
<a href="/" className="logo">
2026-02-22 00:23:35 -08:00
<img src="/assets/logo.svg" alt="quipslop" />
2026-02-20 04:49:40 -08:00
</a>
2026-02-22 01:20:19 -08:00
<div style={{ display: "flex", gap: "8px", alignItems: "center" }}>
{state.isPaused && (
<div
className="viewer-pill"
style={{ color: "var(--text-muted)", borderColor: "var(--border)" }}
>
Paused
</div>
)}
<div className="viewer-pill" aria-live="polite">
<span className="viewer-pill__dot" />
{viewerCount} viewer{viewerCount === 1 ? "" : "s"} watching
</div>
2026-02-22 00:11:20 -08:00
</div>
2026-02-20 04:49:40 -08:00
</header>
{state.done ? (
<GameOver scores={state.scores} />
) : displayRound ? (
<Arena round={displayRound} total={totalRounds} />
) : (
2026-02-22 00:23:35 -08:00
<div className="waiting">
Starting
<Dots />
</div>
2026-02-20 04:49:40 -08:00
)}
{isNextPrompting && lastCompleted && (
<div className="next-toast">
2026-02-22 00:23:35 -08:00
<ModelTag model={state.active!.prompter} small /> is writing the
next prompt
<Dots />
2026-02-20 04:49:40 -08:00
</div>
)}
2026-02-19 23:28:03 -08:00
</main>
2026-02-19 22:46:41 -08:00
2026-02-20 04:49:40 -08:00
<Standings scores={state.scores} activeRound={state.active} />
2026-02-19 23:28:03 -08:00
</div>
2026-02-19 22:46:41 -08:00
</div>
);
}
2026-02-20 04:49:40 -08:00
// ── Mount ────────────────────────────────────────────────────────────────────
2026-02-19 22:46:41 -08:00
const root = createRoot(document.getElementById("root")!);
2026-02-20 04:49:40 -08:00
root.render(<App />);