Skip to content

Commit 128f4c1

Browse files
Merge pull request #3 from zasbu/feat/server-page-redesign
Server page redesign
2 parents 1268194 + c2e4db0 commit 128f4c1

8 files changed

Lines changed: 340 additions & 115 deletions

File tree

‎web/src/features/servers/ServerActivity.tsx‎

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
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";
66
import type { Schemas } from "@/api/client";
7-
import { Card, Label } from "@/ui/Card";
7+
import { Card } from "@/ui/Card";
8+
import { classNames } from "@/ui/classNames";
89
import { LineChart } from "@/ui/LineChart";
910
import { 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. */
107152
function when(at: string, minutes: number): string {
108153
const start = new Date(at);

‎web/src/features/servers/ServerAddress.tsx‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function ServerAddress({ address }: { address: string }) {
88

99
async function copy() {
1010
try {
11-
await navigator.clipboard.writeText(`connect ${address}`);
11+
await navigator.clipboard.writeText(address);
1212
setCopied(true);
1313
setTimeout(() => setCopied(false), 1500);
1414
} catch {
@@ -20,14 +20,17 @@ export function ServerAddress({ address }: { address: string }) {
2020
<button
2121
type="button"
2222
onClick={copy}
23-
aria-label={`Copy "connect ${address}"`}
24-
className="text-text-muted hover:text-text inline-flex items-center gap-1.5 text-sm tabular-nums transition-colors duration-(--duration-fast)"
23+
aria-label={`Copy ${address}`}
24+
className="text-text-muted hover:text-text inline-flex items-center gap-2.5 text-sm tabular-nums transition-colors duration-(--duration-fast)"
2525
>
2626
{address}
2727
{copied ? (
28-
<Check aria-hidden className="text-success size-3.5" />
28+
<Check
29+
aria-hidden
30+
className="text-success mt-0.75 size-3.25 self-start"
31+
/>
2932
) : (
30-
<Copy aria-hidden className="size-3.5" />
33+
<Copy aria-hidden className="mt-0.75 size-3.25 self-start" />
3134
)}
3235
</button>
3336
);

0 commit comments

Comments
 (0)