Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web/src/app/servers/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const metadata: Metadata = {
export default function ServersPage() {
return (
<Page className="gap-6">
<h1 className="text-3xl font-bold">Servers</h1>
{/* The board shows this as its own heading from sm up; a phone keeps the two boards apart, so it needs one here. */}
<h1 className="text-3xl font-bold sm:hidden">Servers</h1>
<ServerList />
</Page>
);
Expand Down
118 changes: 72 additions & 46 deletions web/src/features/servers/ServerActivity.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client";
// Players online across listed servers over the last day, week, month or year. Hovering, tapping or arrow
// keys read a point: players and servers at that time. Every range arrives with the page, so switching is
// instant.
// The top of the Servers board from sm up, as its own card on a phone: players online across listed servers
// over the last day, week, month or year. Hovering, tapping or arrow keys read a point: players and servers
// at that time. Every range arrives with the page, so switching is instant.
import { useSearchParams } from "next/navigation";
import { type ReactNode, useState } from "react";
import type { Schemas } from "@/api/client";
import { Card } from "@/ui/Card";
import { classNames } from "@/ui/classNames";
import { LineChart } from "@/ui/LineChart";
import { Segmented } from "@/ui/Segmented";
Expand Down Expand Up @@ -36,59 +36,84 @@ const date = new Intl.DateTimeFormat("en", {
});

export function ServerActivity({
heading,
servers,
activity,
}: {
/** Sits in the chart's top row, so the page doesn't spend a line on its own title. */
heading: string;
/**
* The listed servers, so the headline can count online servers and players from what this viewer actually
* sees below, on whichever of Public or Whitelist is open. Stands in for the chart's own "now" point, which
* comes from the last finished bucket and so can lag behind it by up to the bucket length.
*/
servers?: Schemas["ListedServer"][];
activity: Partial<Record<Range, Schemas["ServerActivity"]>>;
}) {
const [range, setRange] = useState<Range>("day");
const [active, setActive] = useState<number | null>(null);
const whitelist = useSearchParams().get("view") === "whitelist";
const shown = servers?.filter((s) => s.online && s.whitelist === whitelist);
const live = shown && {
servers: shown.length,
players: shown.reduce((sum, s) => sum + s.players, 0),
};
const current = activity[range];
const points = current?.points ?? [];
// The longer ranges' buckets are averages, so "now" always comes from the day's.
const now = activity.day?.points.at(-1) ?? points.at(-1);
const now = live ?? activity.day?.points.at(-1) ?? points.at(-1);
const hovered = active === null ? undefined : points[active];
if (!current || !now || points.length < 2) return null;
// Hidden on a phone: the page's own heading covers it there, since the chart is its own card.
const title = (
<h1 className="hidden text-xl font-bold sm:block">{heading}</h1>
);
// Without a chart to draw, the board still opens with its title; a phone shows nothing at all.
if (!current || !now || points.length < 2)
return <div className="hidden px-4 py-4 sm:block">{title}</div>;
const peak = Math.max(...points.map((point) => point.players));

return (
<Card className="flex flex-col gap-3">
<div className="flex flex-wrap items-center justify-between gap-3 max-sm:justify-center">
<p
aria-live="polite"
className="text-text-muted flex flex-wrap items-baseline gap-x-2 gap-y-1 text-sm tabular-nums max-sm:justify-center"
>
{hovered ? (
<>
<Stat value={hovered.players}>
{hovered.players === 1 ? "player" : "players"}
{range !== "day" && " on average"}
</Stat>
<Dot />
<Stat value={hovered.servers}>
{hovered.servers === 1 ? "server" : "servers"}
</Stat>
<Dot />
<span className="text-text-subtle">
{when(hovered.at, current.bucket_minutes)}
</span>
</>
) : (
<>
<Stat
value={now.players}
tone={now.players > 0 ? "text-success" : "text-danger"}
>
online now
</Stat>
<Dot />
<Stat value={now.servers}>
{now.servers === 1 ? "server" : "servers"}
</Stat>
<Dot />
<Stat value={peak}>peak in {rangeLabel(range)}</Stat>
</>
)}
</p>
<div className="border-border bg-surface flex flex-col gap-3 rounded-lg border p-5 sm:rounded-none sm:border-0 sm:bg-transparent sm:p-0 sm:pt-4">
<div className="flex flex-wrap items-center justify-between gap-3 max-sm:justify-center sm:px-4">
<div className="flex min-w-0 flex-col gap-1 max-sm:items-center">
{title}
<p
aria-live="polite"
className="text-text-muted flex flex-wrap items-baseline gap-x-2 gap-y-1 text-sm tabular-nums max-sm:justify-center"
>
{hovered ? (
<>
<Stat value={hovered.players}>
{hovered.players === 1 ? "player" : "players"}
{range !== "day" && " on average"}
</Stat>
<Dot />
<Stat value={hovered.servers}>
{hovered.servers === 1 ? "server" : "servers"}
</Stat>
<Dot />
<span className="text-text-subtle">
{when(hovered.at, current.bucket_minutes)}
</span>
</>
) : (
<>
<Stat
value={now.players}
tone={now.players > 0 ? "text-success" : "text-danger"}
>
online now
</Stat>
<Dot />
<Stat value={now.servers}>
{now.servers === 1 ? "server" : "servers"}
</Stat>
<Dot />
<Stat value={peak}>peak in {rangeLabel(range)}</Stat>
</>
)}
</p>
</div>
<Segmented
label="Range"
fillOnPhones
Expand All @@ -101,7 +126,8 @@ export function ServerActivity({
/>
</div>

<div className="flex flex-col gap-1">
{/* Darker than the board, so the trace reads as a screen rather than more flat fill. */}
<div className="sm:bg-bg flex flex-col gap-1 sm:px-4 sm:pt-2 sm:pb-3">
<LineChart
size="sm"
label={`Players online over the last ${rangeLabel(range)}. Use the arrow keys to read each point.`}
Expand All @@ -116,7 +142,7 @@ export function ServerActivity({
<span>{edge(points.at(-1)?.at, range)}</span>
</div>
</div>
</Card>
</div>
);
}

Expand Down
36 changes: 23 additions & 13 deletions web/src/features/servers/ServerList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// The Servers page: activity over time, then the listed servers running the SourceJump plugin, online ones
// first. Staff with manage_servers also see unlisted servers there, and list or unlist them in place.
// first. Staff with manage_servers also see unlisted servers there, and list or unlist them in place. From sm
// up this is one board; combining the chart and the list into one board read badly on a phone, so it keeps
// them as two boards there.
import { api } from "@/api/client";
import { ServerActivity } from "./ServerActivity";
import { ServerRows } from "./ServerRows";
Expand All @@ -14,22 +16,30 @@ export async function ServerList() {
),
]);
const servers = result?.data?.servers;
if (!servers) {
return <p>Servers couldn&apos;t be loaded. Try again in a moment.</p>;
}
if (servers.length === 0) return <ServerRows servers={servers} />;

return (
<div className="flex flex-col gap-4">
<div className="sm:border-border sm:bg-surface flex flex-col gap-4 sm:gap-0 sm:overflow-hidden sm:rounded-lg sm:border">
<ServerActivity
activity={{
day: day?.data,
week: week?.data,
month: month?.data,
year: year?.data,
}}
heading="Servers"
servers={servers}
activity={
servers && servers.length > 0
? {
day: day?.data,
week: week?.data,
month: month?.data,
year: year?.data,
}
: {}
}
/>
<ServerRows servers={servers} />
{servers ? (
<ServerRows servers={servers} />
) : (
<p className="text-text-muted border-border rounded-lg border p-5 sm:rounded-none sm:border-x-0 sm:border-t sm:border-b-0 sm:px-4 sm:py-6">
Servers couldn&apos;t be loaded. Try again in a moment.
</p>
)}
</div>
);
}
75 changes: 38 additions & 37 deletions web/src/features/servers/ServerRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// One server on the Servers page: a card on phones, a compact row wider. Staff rows add badges and tools.
import { Check, ImageOff } from "lucide-react";
import { Check, ImageOff, Power } from "lucide-react";
import type { ReactNode } from "react";
import type { Schemas } from "@/api/client";
import { formatAgo } from "@/features/runs/time";
Expand All @@ -14,14 +14,11 @@ type Server = Schemas["ListedServer"];

export function ServerRow({
server,
unlisted = false,
badges,
tools,
eager = false,
}: {
server: Server;
/** Shown to staff only: the public list never has unlisted servers. */
unlisted?: boolean;
badges?: ReactNode;
/** Staff controls after Join, kept at full strength when the row is dimmed. */
tools?: ReactNode;
Expand All @@ -32,30 +29,28 @@ export function ServerRow({
// Each part is dimmed on its own, not inside one wrapper, so the staff tools stay at full strength.
const dim = classNames(
"transition-opacity duration-(--duration-base)",
unlisted ? "opacity-50" : !server.online && "opacity-60",
// Only a server that's down is dimmed; staff marks like Unlisted are badges.
!server.online && "opacity-45",
);
const connected = "Runs on this server count on SourceJump";
return (
<ServerRowFrame
unlisted={unlisted}
className="relative flex flex-col sm:h-full sm:flex-row sm:items-center sm:gap-3 sm:p-3"
className={classNames(
"relative flex flex-col sm:h-full sm:flex-row sm:items-center sm:gap-3 sm:p-3",
// A step darker than the board, like a panel switched off; the red is kept for the OFFLINE label alone.
!server.online ? "bg-bg/60" : "bg-surface sm:bg-transparent",
)}
>
<span
className={classNames(
"absolute inset-y-0 left-0 z-10 w-1",
server.online ? "bg-success" : "bg-danger",
)}
>
<span className="sr-only">{server.online ? "Online" : "Offline"}</span>
</span>
<span
className={classNames(
"border-border bg-surface-raised relative flex h-28 shrink-0 items-end justify-between gap-3 overflow-hidden border-b p-3",
"sm:h-18 sm:w-32 sm:rounded-sm sm:border sm:p-0",
dim,
!server.online && "[&_img]:grayscale",
)}
>
<MapPicture image={server.map_image} eager={eager} />
{!server.online && <span className="bg-bg/50 absolute inset-0" />}

<span className="relative min-w-0 sm:hidden">
<OnPicture>
<MapName server={server} />
Expand Down Expand Up @@ -109,12 +104,7 @@ export function ServerRow({
<ServerAddress address={address} />
</span>
</span>
<span
className={classNames(
"flex shrink-0 flex-col items-center justify-center gap-1 max-sm:hidden",
dim,
)}
>
<span className="flex shrink-0 flex-col items-center justify-center gap-1 max-sm:hidden">
<Players server={server} stacked />
{server.online && (
<a
Expand Down Expand Up @@ -197,17 +187,30 @@ function Players({
stacked?: boolean;
}) {
if (!server.online) {
return (
<span
className={classNames(
"text-text-muted text-sm whitespace-nowrap",
stacked && "flex flex-col items-center text-center text-xs",
)}
>
<span>Last seen</span>{" "}
<time dateTime={server.last_seen_at}>
{formatAgo(server.seen_minutes_ago)}
</time>
const ago = (
<time dateTime={server.last_seen_at}>
{formatAgo(server.seen_minutes_ago)}
</time>
);
const offline = (
<span className="text-danger flex items-center gap-1.5 font-bold tracking-wide uppercase">
<Power aria-hidden className="size-4" />
Offline
</span>
);
return stacked ? (
<span className="flex flex-col items-center gap-1 text-center">
<span className="text-sm whitespace-nowrap">{offline}</span>
{/* Two short lines rather than one long one, so the column stays narrow next to the name and badges. */}
<span className="text-text-muted flex flex-col text-xs whitespace-nowrap">
<span>Last seen</span>
{ago}
</span>
</span>
) : (
<span className="flex items-center gap-1.5 text-sm whitespace-nowrap">
{offline}
<span className="text-text-muted">· {ago}</span>
</span>
);
}
Expand All @@ -233,19 +236,17 @@ function Players({

/** The row's box, shared with the staff-only row of a server listed before it was ever heard from. */
export function ServerRowFrame({
unlisted = false,
className,
children,
}: {
unlisted?: boolean;
className?: string;
children: ReactNode;
}) {
return (
<div
className={classNames(
"border-border bg-surface @container overflow-hidden rounded-lg border",
unlisted && "border-dashed",
// A card on a phone; a cell of the Servers board from sm up, which then draws the lines between rows.
"border-border @container h-full overflow-hidden rounded-lg border sm:rounded-none sm:border-0",
className,
)}
>
Expand Down
Loading