22// Players online across listed servers over the last day, week, month or year. Hovering, tapping or arrow
33// keys read a point: players and servers at that time. Every range arrives with the page, so switching is
44// instant.
5- import { useState } from "react" ;
5+ import { type ReactNode , useState } from "react" ;
66import type { Schemas } from "@/api/client" ;
7- import { Card , Label } from "@/ui/Card" ;
7+ import { Card } from "@/ui/Card" ;
8+ import { classNames } from "@/ui/classNames" ;
89import { LineChart } from "@/ui/LineChart" ;
910import { Segmented } from "@/ui/Segmented" ;
1011
@@ -43,17 +44,54 @@ export function ServerActivity({
4344 const [ active , setActive ] = useState < number | null > ( null ) ;
4445 const current = activity [ range ] ;
4546 const points = current ?. points ?? [ ] ;
46- const shown = points [ active ?? points . length - 1 ] ;
47- if ( ! current || ! shown || points . length < 2 ) return null ;
48- // A day's buckets are short enough to read as "now"; longer ones are averages over hours or days.
49- const averaged = range !== "day" ;
47+ // The longer ranges' buckets are averages, so "now" always comes from the day's.
48+ const now = activity . day ?. points . at ( - 1 ) ?? points . at ( - 1 ) ;
49+ const hovered = active === null ? undefined : points [ active ] ;
50+ if ( ! current || ! now || points . length < 2 ) return null ;
51+ const peak = Math . max ( ...points . map ( ( point ) => point . players ) ) ;
5052
5153 return (
52- < Card className = "flex flex-col gap-4" >
53- < div className = "flex flex-wrap items-center justify-between gap-3" >
54- < Label > Players online</ Label >
54+ < Card className = "flex flex-col gap-3" >
55+ < div className = "flex flex-wrap items-center justify-between gap-3 max-sm:justify-center" >
56+ < p
57+ aria-live = "polite"
58+ className = "text-text-muted flex flex-wrap items-baseline gap-x-2 gap-y-1 text-sm tabular-nums max-sm:justify-center"
59+ >
60+ { hovered ? (
61+ < >
62+ < Stat value = { hovered . players } >
63+ { hovered . players === 1 ? "player" : "players" }
64+ { range !== "day" && " on average" }
65+ </ Stat >
66+ < Dot />
67+ < Stat value = { hovered . servers } >
68+ { hovered . servers === 1 ? "server" : "servers" }
69+ </ Stat >
70+ < Dot />
71+ < span className = "text-text-subtle" >
72+ { when ( hovered . at , current . bucket_minutes ) }
73+ </ span >
74+ </ >
75+ ) : (
76+ < >
77+ < Stat
78+ value = { now . players }
79+ tone = { now . players > 0 ? "text-success" : "text-danger" }
80+ >
81+ online now
82+ </ Stat >
83+ < Dot />
84+ < Stat value = { now . servers } >
85+ { now . servers === 1 ? "server" : "servers" }
86+ </ Stat >
87+ < Dot />
88+ < Stat value = { peak } > peak in { rangeLabel ( range ) } </ Stat >
89+ </ >
90+ ) }
91+ </ p >
5592 < Segmented
5693 label = "Range"
94+ fillOnPhones
5795 options = { ranges }
5896 value = { range }
5997 onChange = { ( value ) => {
@@ -63,38 +101,17 @@ export function ServerActivity({
63101 />
64102 </ div >
65103
66- < p
67- aria-live = "polite"
68- className = "flex flex-wrap items-baseline gap-x-4 gap-y-1"
69- >
70- < span >
71- < span className = "text-2xl font-bold tabular-nums" >
72- { shown . players }
73- </ span > { " " }
74- < span className = "text-text-muted text-sm" >
75- { shown . players === 1 ? "player" : "players" }
76- { averaged && " on average" }
77- </ span >
78- </ span >
79- < span className = "text-text-muted text-sm tabular-nums" >
80- on { shown . servers === 1 ? "1 server" : `${ shown . servers } servers` }
81- </ span >
82- < span className = "text-text-subtle text-sm tabular-nums" >
83- { active === null ? "Latest: " : "" }
84- { when ( shown . at , current . bucket_minutes ) }
85- </ span >
86- </ p >
87-
88104 < div className = "flex flex-col gap-1" >
89105 < LineChart
90- label = { `Players online over the last ${ ranges . find ( ( r ) => r . value === range ) ?. label } . Use the arrow keys to read each point.` }
106+ size = "sm"
107+ label = { `Players online over the last ${ rangeLabel ( range ) } . Use the arrow keys to read each point.` }
91108 points = { points . map ( ( p ) => ( { x : Date . parse ( p . at ) , y : p . players } ) ) }
92109 active = { active }
93110 onActive = { setActive }
94111 formatValue = { ( value ) => String ( Math . round ( value ) ) }
95112 />
96113 { /* Indented by the value labels' width, so the dates sit under the plot's ends. */ }
97- < div className = "text-text-subtle flex justify-between pl-10 text-xs tabular-nums" >
114+ < div className = "text-text-subtle flex justify-between pl-10 text-xs tabular-nums max-sm:pl-0 " >
98115 < span > { edge ( points [ 0 ] ?. at , range ) } </ span >
99116 < span > { edge ( points . at ( - 1 ) ?. at , range ) } </ span >
100117 </ div >
@@ -103,6 +120,34 @@ export function ServerActivity({
103120 ) ;
104121}
105122
123+ function Stat ( {
124+ value,
125+ tone = "text-text" ,
126+ children,
127+ } : {
128+ value : number ;
129+ tone ?: string ;
130+ children : ReactNode ;
131+ } ) {
132+ return (
133+ < span >
134+ < span className = { classNames ( "font-bold" , tone ) } > { value } </ span > { children }
135+ </ span >
136+ ) ;
137+ }
138+
139+ function Dot ( ) {
140+ return (
141+ < span aria-hidden className = "text-text-subtle" >
142+ ·
143+ </ span >
144+ ) ;
145+ }
146+
147+ function rangeLabel ( range : Range ) : string {
148+ return ranges . find ( ( r ) => r . value === range ) ?. label ?? "" ;
149+ }
150+
106151/** The bucket a point covers: a time span within a day, or the whole day. */
107152function when ( at : string , minutes : number ) : string {
108153 const start = new Date ( at ) ;
0 commit comments