diff --git a/web/src/features/servers/ServerActivity.tsx b/web/src/features/servers/ServerActivity.tsx index b642b5a..61b2468 100644 --- a/web/src/features/servers/ServerActivity.tsx +++ b/web/src/features/servers/ServerActivity.tsx @@ -2,9 +2,10 @@ // 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 { useState } from "react"; +import { type ReactNode, useState } from "react"; import type { Schemas } from "@/api/client"; -import { Card, Label } from "@/ui/Card"; +import { Card } from "@/ui/Card"; +import { classNames } from "@/ui/classNames"; import { LineChart } from "@/ui/LineChart"; import { Segmented } from "@/ui/Segmented"; @@ -43,17 +44,54 @@ export function ServerActivity({ const [active, setActive] = useState(null); const current = activity[range]; const points = current?.points ?? []; - const shown = points[active ?? points.length - 1]; - if (!current || !shown || points.length < 2) return null; - // A day's buckets are short enough to read as "now"; longer ones are averages over hours or days. - const averaged = range !== "day"; + // 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 hovered = active === null ? undefined : points[active]; + if (!current || !now || points.length < 2) return null; + const peak = Math.max(...points.map((point) => point.players)); return ( - -
- + +
+

+ {hovered ? ( + <> + + {hovered.players === 1 ? "player" : "players"} + {range !== "day" && " on average"} + + + + {hovered.servers === 1 ? "server" : "servers"} + + + + {when(hovered.at, current.bucket_minutes)} + + + ) : ( + <> + 0 ? "text-success" : "text-danger"} + > + online now + + + + {now.servers === 1 ? "server" : "servers"} + + + peak in {rangeLabel(range)} + + )} +

{ @@ -63,38 +101,17 @@ export function ServerActivity({ />
-

- - - {shown.players} - {" "} - - {shown.players === 1 ? "player" : "players"} - {averaged && " on average"} - - - - on {shown.servers === 1 ? "1 server" : `${shown.servers} servers`} - - - {active === null ? "Latest: " : ""} - {when(shown.at, current.bucket_minutes)} - -

-
r.value === range)?.label}. Use the arrow keys to read each point.`} + size="sm" + label={`Players online over the last ${rangeLabel(range)}. Use the arrow keys to read each point.`} points={points.map((p) => ({ x: Date.parse(p.at), y: p.players }))} active={active} onActive={setActive} formatValue={(value) => String(Math.round(value))} /> {/* Indented by the value labels' width, so the dates sit under the plot's ends. */} -
+
{edge(points[0]?.at, range)} {edge(points.at(-1)?.at, range)}
@@ -103,6 +120,34 @@ export function ServerActivity({ ); } +function Stat({ + value, + tone = "text-text", + children, +}: { + value: number; + tone?: string; + children: ReactNode; +}) { + return ( + + {value} {children} + + ); +} + +function Dot() { + return ( + + ยท + + ); +} + +function rangeLabel(range: Range): string { + return ranges.find((r) => r.value === range)?.label ?? ""; +} + /** The bucket a point covers: a time span within a day, or the whole day. */ function when(at: string, minutes: number): string { const start = new Date(at); diff --git a/web/src/features/servers/ServerAddress.tsx b/web/src/features/servers/ServerAddress.tsx index 0adc810..3af5a43 100644 --- a/web/src/features/servers/ServerAddress.tsx +++ b/web/src/features/servers/ServerAddress.tsx @@ -8,7 +8,7 @@ export function ServerAddress({ address }: { address: string }) { async function copy() { try { - await navigator.clipboard.writeText(`connect ${address}`); + await navigator.clipboard.writeText(address); setCopied(true); setTimeout(() => setCopied(false), 1500); } catch { @@ -20,14 +20,17 @@ export function ServerAddress({ address }: { address: string }) { ); diff --git a/web/src/features/servers/ServerRow.tsx b/web/src/features/servers/ServerRow.tsx index b665638..390f762 100644 --- a/web/src/features/servers/ServerRow.tsx +++ b/web/src/features/servers/ServerRow.tsx @@ -1,8 +1,7 @@ -// One server on the Servers page: its country's flag, map, hostname, address, players and a Join link. Staff -// rows add badges and tools, and show unlisted servers dimmed with a dashed edge. +// 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 type { ReactNode } from "react"; import type { Schemas } from "@/api/client"; -import { MapImage } from "@/features/maps/MapImage"; import { formatAgo } from "@/features/runs/time"; import { Badge } from "@/ui/Badge"; import { buttonClasses } from "@/ui/Button"; @@ -11,108 +10,243 @@ import { Flag } from "@/ui/Flag"; import { Link } from "@/ui/Link"; import { ServerAddress } from "./ServerAddress"; +type Server = Schemas["ListedServer"]; + export function ServerRow({ server, unlisted = false, badges, tools, + eager = false, }: { - server: Schemas["ListedServer"]; + 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; + /** For the first rows, on screen from the start. */ + eager?: boolean; }) { const address = `${server.ip}:${server.port}`; - // Each part is dimmed on its own, not inside one wrapper, so the row still wraps as one flex line on phones. + // 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", ); + const connected = "Runs on this server count on SourceJump"; return ( - - - - - {/* Only a known country: Flag's placeholder speaks of Steam, which a server has nothing to do with. */} - {server.country_code && } - - {server.hostname} + + + {server.online ? "Online" : "Offline"} + + + + + + + + + + + + + + + + + + {/* Only a known country: Flag's placeholder speaks of Steam, which a server has nothing to do with. */} + {server.country_code && } + + {server.hostname} + + {server.connected && ( + + + Connected + + )} {server.connected && ( Connected )} {badges} - - {server.map_has_page ? ( - - {server.map} - - ) : ( - {server.map} - )} + + + + - {/* With staff tools there's no room beside the name on a phone, so they take a line of their own. */} - - {server.online ? ( - - - - {server.players} - /{server.max_slots} - - - ) : ( - - Last seen{" "} - - - )} - {server.online && ( - - Join - - )} - - {tools && {tools}} + + {server.online && ( + + Join + + )} + {tools && ( + + {tools} + + )} ); } +// Always the small file, even for a phone's banner: the large one is about ten times the size. +function MapPicture({ + image, + eager, +}: { + image: Server["map_image"]; + eager: boolean; +}) { + if (!image) { + return ( + + + + ); + } + return ( + // eslint-disable-next-line @next/next/no-img-element + + ); +} + +function OnPicture({ children }: { children: ReactNode }) { + return ( + + {children} + + ); +} + +function MapName({ server }: { server: Server }) { + return server.map_has_page ? ( + + {server.map} + + ) : ( + {server.map} + ); +} + +/** `stacked` is the wider row's column. */ +function Players({ + server, + stacked = false, +}: { + server: Server; + stacked?: boolean; +}) { + if (!server.online) { + return ( + + Last seen{" "} + + + ); + } + return ( + + + + 0 ? "text-success" : "text-danger"}> + {server.players} + + + / + {server.max_slots} + + + {!stacked && ( + playing + )} + + + ); +} + /** 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 (
{children} diff --git a/web/src/features/servers/ServerRows.tsx b/web/src/features/servers/ServerRows.tsx index cd45fbc..bf7ca1c 100644 --- a/web/src/features/servers/ServerRows.tsx +++ b/web/src/features/servers/ServerRows.tsx @@ -9,6 +9,7 @@ import { StaffServerRow } from "@/features/staff/servers/StaffServerRow"; import { useStaffServers } from "@/features/staff/servers/useStaffServers"; import { Card } from "@/ui/Card"; import { ChoiceBar } from "@/ui/ChoiceBar"; +import { classNames } from "@/ui/classNames"; import { LinkTabs } from "@/ui/LinkTabs"; import { ServerRow } from "./ServerRow"; @@ -38,6 +39,7 @@ export function ServerRows({ ({ address: server.address, row: , @@ -58,9 +60,9 @@ export function ServerRows({ server.whitelist)} - rows={shown.map((server) => ({ + rows={shown.map((server, index) => ({ address: `${server.ip}:${server.port}`, - row: , + row: , }))} /> ); @@ -73,10 +75,13 @@ export function ServerRows({ function View({ whitelist, hasWhitelist, + pairs = true, rows, }: { whitelist: boolean; hasWhitelist: boolean; + /** Staff rows need the whole width for their tools. */ + pairs?: boolean; rows: { address: string; row: ReactNode }[]; }) { return ( @@ -98,7 +103,7 @@ function View({ )} {rows.length > 0 ? ( - + ) : ( {whitelist @@ -111,22 +116,35 @@ function View({ } /** Keyed by address, so a row the public list already showed stays put when the staff list replaces it. */ -function Rows({ rows }: { rows: { address: string; row: ReactNode }[] }) { +function Rows({ + rows, + pairs, +}: { + rows: { address: string; row: ReactNode }[]; + pairs: boolean; +}) { return ( -
    - {rows.map(({ address, row }, index) => ( -
  • - {row} -
  • - ))} -
+
+
    + {rows.map(({ address, row }, index) => ( +
  • + {row} +
  • + ))} +
+
); } diff --git a/web/src/features/staff/servers/StaffServerRow.tsx b/web/src/features/staff/servers/StaffServerRow.tsx index 86a582d..3a1f964 100644 --- a/web/src/features/staff/servers/StaffServerRow.tsx +++ b/web/src/features/staff/servers/StaffServerRow.tsx @@ -26,7 +26,7 @@ export function StaffServerRow({ if (!server.server || !server.report) { return ( - + {server.address} diff --git a/web/src/ui/Button.tsx b/web/src/ui/Button.tsx index a7516c5..ac6ae20 100644 --- a/web/src/ui/Button.tsx +++ b/web/src/ui/Button.tsx @@ -3,8 +3,8 @@ import { Link } from "./Link"; import type { ComponentProps } from "react"; import { classNames } from "./classNames"; -type Variant = "primary" | "secondary" | "ghost"; -type Size = "sm" | "md"; +type Variant = "primary" | "secondary" | "soft" | "ghost"; +type Size = "xs" | "sm" | "md"; interface Style { variant?: Variant; @@ -15,12 +15,15 @@ const variants: Record = { primary: "bg-accent text-on-accent hover:bg-accent-hover", secondary: "border border-border bg-surface text-text hover:border-border-strong hover:bg-surface-raised", + // For an action repeated down a list, such as a server's Join. + soft: "border border-accent/30 bg-accent-soft text-accent hover:border-accent/60 hover:bg-accent/20", // A ghost button that toggles something (a viewer's freecam, a settings popover) shows the accent while on. ghost: "text-text-muted hover:bg-surface-raised hover:text-text aria-expanded:text-accent aria-pressed:bg-accent-soft aria-pressed:text-accent", }; const sizes: Record = { + xs: "h-6 gap-1 px-2.5 text-xs", sm: "h-8 gap-1.5 px-3 text-sm", md: "h-10 gap-2 px-4 text-sm", }; diff --git a/web/src/ui/LineChart.tsx b/web/src/ui/LineChart.tsx index 9c5fd94..d21dc6b 100644 --- a/web/src/ui/LineChart.tsx +++ b/web/src/ui/LineChart.tsx @@ -3,6 +3,7 @@ // scale, e.g. world records among runs. Hovering, tapping or arrow keys pick a point and report it through // `onActive`; the parent shows the values, so no tooltip covers the line. import { type KeyboardEvent, type PointerEvent } from "react"; +import { classNames } from "./classNames"; export interface LinePoint { /** Milliseconds since the epoch. */ @@ -25,6 +26,7 @@ export function LineChart({ onActive, formatValue, label, + size = "md", }: { /** Oldest first, evenly spaced. */ points: LinePoint[]; @@ -33,6 +35,8 @@ export function LineChart({ formatValue: (value: number) => string; /** Read out for the chart, since it's a picture. */ label: string; + /** `sm` is shorter, and on a phone shows only the top value so the line spans the card. */ + size?: "sm" | "md"; }) { if (points.length < 2) return null; const top = niceMax(Math.max(...points.map((point) => point.y))); @@ -76,10 +80,15 @@ export function LineChart({ // Vertical padding leaves room for the top and bottom value labels, which center on their gridlines. return ( -
+
{ticks.map((tick) => ( onActive(null)} onKeyDown={onKeyDown} > + {size === "sm" && ( + + {formatValue(top)} + + )} void; /** Names the group for screen readers. */ label: string; + /** On a phone, spans its container with the options sharing the width. */ + fillOnPhones?: boolean; className?: string; }) { const styles = tabStyles.segmented; @@ -31,6 +34,7 @@ export function Segmented({ className={classNames( styles.list, "max-w-full [scrollbar-width:none] overflow-x-auto", + fillOnPhones && "max-sm:flex max-sm:w-full", className, )} > @@ -42,6 +46,7 @@ export function Segmented({ onClick={() => onChange(option.value)} className={classNames( "shrink-0 whitespace-nowrap transition-colors duration-(--duration-fast)", + fillOnPhones && "max-sm:flex-1", styles.tab, option.value === value ? styles.active : styles.idle, )}