File size: 3,989 Bytes
c52f47a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { useEffect, useState } from "react";

import { useAdmin } from "../lib/admin";
import { fetchLeaderboard, type Leaderboard as LeaderboardData } from "../lib/api";

function formatElapsed(ms: number | null) {
  if (ms === null) {
    return "—";
  }
  const totalSeconds = Math.floor(ms / 1000);
  const minutes = Math.floor(totalSeconds / 60)
    .toString()
    .padStart(2, "0");
  const seconds = (totalSeconds % 60).toString().padStart(2, "0");
  const centiseconds = Math.floor((ms % 1000) / 10)
    .toString()
    .padStart(2, "0");
  return `${minutes}:${seconds}.${centiseconds}`;
}

type Props = {
  puzzleId: string;
};

export function Leaderboard({ puzzleId }: Props) {
  const { enabled, token } = useAdmin();
  const [data, setData] = useState<LeaderboardData | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [includeTest, setIncludeTest] = useState(false);

  useEffect(() => {
    if (!enabled || !puzzleId) {
      setData(null);
      return;
    }
    let cancelled = false;
    setLoading(true);
    setError(null);
    fetchLeaderboard(puzzleId, { includeTest })
      .then((result) => {
        if (!cancelled) {
          setData(result);
        }
      })
      .catch((caught) => {
        if (!cancelled) {
          setError(caught instanceof Error ? caught.message : "Could not load leaderboard.");
        }
      })
      .finally(() => {
        if (!cancelled) {
          setLoading(false);
        }
      });
    return () => {
      cancelled = true;
    };
  }, [enabled, puzzleId, includeTest, token]);

  if (!enabled) {
    return null;
  }

  const entries = data?.entries ?? [];
  const solvedCount = entries.filter((entry) => entry.solved).length;

  return (
    <section className="panel leaderboard-panel">
      <div className="leaderboard-header">
        <div>
          <div className="eyebrow">Leaderboard · admin</div>
          <div className="leaderboard-summary">
            {loading
              ? "Loading…"
              : `${entries.length} session${entries.length === 1 ? "" : "s"} · ${solvedCount} solved`}
          </div>
        </div>
        <label className="leaderboard-toggle">
          <input
            type="checkbox"
            checked={includeTest}
            onChange={(event) => setIncludeTest(event.target.checked)}
          />
          include <code>test</code>
        </label>
      </div>

      {error ? <div className="status-box error">{error}</div> : null}

      {!loading && entries.length === 0 ? (
        <div className="leaderboard-empty">No sessions for this puzzle yet.</div>
      ) : null}

      {entries.length > 0 ? (
        <div className="leaderboard-table-wrap">
          <table className="leaderboard-table">
            <thead>
              <tr>
                <th>#</th>
                <th>Player</th>
                <th>Time</th>
                <th>Attempts</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody>
              {entries.map((entry, index) => (
                <tr
                  key={`${entry.player_name}-${entry.started_at}-${index}`}
                  className={entry.solved ? "row-solved" : "row-unsolved"}
                >
                  <td className="leaderboard-rank">{entry.solved ? index + 1 : "—"}</td>
                  <td className="leaderboard-player">{entry.player_name}</td>
                  <td className="leaderboard-time">
                    {entry.solved ? formatElapsed(entry.elapsed_ms) : "—"}
                  </td>
                  <td className="leaderboard-attempts">{entry.submission_count}</td>
                  <td className="leaderboard-status">
                    {entry.solved ? "solved" : "attempted"}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      ) : null}
    </section>
  );
}