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
6 changes: 3 additions & 3 deletions web/src/features/maps/MapList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ function MapStats({
if (!hasTiers && newest.length === 0 && playedThisWeek.length === 0)
return null;
return (
<Card className="grid gap-5 p-4 sm:grid-cols-2 sm:gap-6 md:flex">
<Card className="grid gap-5 p-4 sm:grid-cols-2 sm:gap-6 xl:flex">
{hasTiers && (
<div className="min-w-0 sm:col-span-2 md:shrink-0">
<div className="min-w-0 sm:col-span-2 xl:shrink-0">
<TierSpread
byTier={stats.by_tier}
untiered={stats.maps - tiered}
Expand Down Expand Up @@ -442,7 +442,7 @@ function MapStatsList({
tierCount: number | null | undefined;
}) {
return (
<section className="border-border flex min-w-0 flex-1 flex-col gap-3 border-t pt-4 sm:border-t-0 sm:pt-0 md:border-l md:pl-6">
<section className="border-border flex min-w-0 flex-1 flex-col gap-3 border-t pt-4 sm:border-t-0 sm:pt-0 xl:border-l xl:pl-6">
<h2 className="text-sm font-bold">
{href ? (
<Link href={href} className="hover:text-accent">
Expand Down
305 changes: 251 additions & 54 deletions web/src/ui/TierSpread.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,284 @@
// Map difficulty filters with matching controls for all tiers and individual tiers.
// How many maps hold each tier: a donut split by tier, each slice in its tier's color, beside each tier's count and
// share. Every slice and row links to the maps of that tier; the chosen tier stays bright and the others dim.
// Maps without a tier fill the donut's end in a neutral color. On a phone, where the donut and its list stood
// nearly a screen tall, it's a bar split by tier and the tiers as a grid of small tiles instead.
import type { CSSProperties } from "react";
import { classNames } from "./classNames";
import { Link } from "./Link";
import { TierBadge } from "./TierBadge";
import { TierBadge, tierColor } from "./TierBadge";

/** Each tier's share of all maps, so the shares (with "No tier") add up to the whole list. */
const percent = new Intl.NumberFormat("en", {
style: "percent",
maximumFractionDigits: 0,
});
const number = new Intl.NumberFormat("en");

/** The ring's radius and width, in the SVG's 100-unit box. */
const RADIUS = 40;
const WIDTH = 16;
/** Space between slices, in hundredths of the ring. */
const GAP = 0.6;

/** "All tiers", in the look of the page's filter menus and tier tiles: bordered, raised when it's the choice. */
const allTiersClasses = (chosen: boolean) =>
classNames(
"flex h-7 w-full items-center justify-between gap-2 rounded-sm border px-2 text-sm font-medium transition-colors duration-(--duration-fast)",
chosen
? "bg-surface-raised border-border-strong text-text"
: "border-border text-text-muted hover:text-accent",
);

const rowClasses =
"col-span-3 grid grid-cols-subgrid items-center rounded-sm px-2 py-0.5 transition-[opacity,background-color] duration-(--duration-base)";

/** A quiet highlight keeps the tier colors in the badges and chart. */
const chosenTier = "bg-text/8";

export function TierSpread({
byTier,
untiered,
selected,
tierHref,
}: {
/** Map counts in tier order, easiest first. */
/** Maps per tier, tier 1 first; its length is the tier count. */
byTier: number[];
/** Maps which have no tier assigned. */
untiered: number;
/** The selected tier, if the map list is filtered. */
/** The tier the list is filtered to, if any. */
selected?: number;
/** Where a tier tile should link. Null returns to the complete list. */
/** Where a tier leads; null for the whole list. */
tierHref: (tier: number | null) => string;
}) {
const total = byTier.reduce((sum, count) => sum + count, untiered);
if (total === 0) return null;
const tierCount = byTier.length;
const dimmed = (tier: number) => selected != null && selected !== tier;

// Each slice is a dashed stroke around the ring: as long as its share, starting where the last one ended.
// Strokes rather than wedge paths, so a slice is one circle and grows its width on hover.
const slices = byTier.map((count, index) => ({
tier: index + 1,
count,
start:
(byTier.slice(0, index).reduce((sum, c) => sum + c, 0) / total) * 100,
length: (count / total) * 100,
}));
const tieredShare = (byTier.reduce((sum, c) => sum + c, 0) / total) * 100;
const chosen = selected != null ? byTier[selected - 1] : undefined;
const half = Math.ceil(tierCount / 2);
// The first column holds the lower half; the second the rest and "No tier" after them.
const rows = Math.max(half, tierCount - half + (untiered > 0 ? 1 : 0));
const tierRow = ({ tier, count }: (typeof slices)[number]) => (
<li key={tier} className="col-span-3 grid grid-cols-subgrid">
<Link
href={tierHref(selected === tier ? null : tier)}
scroll={false}
aria-current={selected === tier ? "true" : undefined}
style={{ "--tier": tierColor(tier, tierCount) } as CSSProperties}
className={classNames(
rowClasses,
selected === tier ? chosenTier : "hover:text-accent",
dimmed(tier) && "opacity-40 hover:opacity-100",
)}
>
<TierBadge tier={tier} tierCount={tierCount} />
<span className="text-right tabular-nums">{number.format(count)}</span>
<span className="text-text-subtle tabular-nums">
{percent.format(count / total)}
</span>
</Link>
</li>
);

return (
<section
aria-label="Maps by tier"
className="flex w-fit max-w-full min-w-0 flex-col gap-3"
>
<div className="flex items-center justify-between gap-4">
<h2 className="text-sm font-bold">Difficulty</h2>
<Link
href={tierHref(null)}
scroll={false}
aria-current={selected == null ? "true" : undefined}
className={classNames(
"flex h-7 items-center gap-2 rounded-sm px-2 text-xs transition-colors duration-(--duration-fast)",
selected == null
? "bg-text/8 text-text"
: "text-text-muted hover:text-accent",
)}
<>
<div className="flex flex-col gap-3 sm:hidden">
{/* The donut's shares laid end to end. */}
<div
aria-hidden
className="bg-surface-raised flex h-2 gap-px overflow-hidden rounded-full"
>
<span>All tiers</span>
<span className="text-text-muted text-xs tabular-nums">
{number.format(total)}
</span>
</Link>
</div>
<ul
className="grid w-fit grid-flow-col grid-cols-2 gap-x-6 gap-y-1"
style={{
gridTemplateRows: `repeat(${Math.ceil(tierCount / 2)}, minmax(0, 1fr))`,
}}
>
{byTier.map((count, index) => {
const tier = index + 1;
const active = tier === selected;
return (
<li key={tier} className="min-w-0">
{slices.map(
({ tier, count }) =>
count > 0 && (
<span
key={tier}
style={
{
"--tier": tierColor(tier, tierCount),
flexGrow: count,
} as CSSProperties
}
className={classNames(
"bg-(--tier) transition-opacity duration-(--duration-base)",
dimmed(tier) && "opacity-25",
)}
/>
),
)}
{untiered > 0 && <span style={{ flexGrow: untiered }} />}
</div>
<div className="flex items-center justify-between gap-3 text-sm">
<Link
href={tierHref(null)}
scroll={false}
aria-current={selected == null ? "true" : undefined}
className={allTiersClasses(selected == null)}
>
All tiers
<span className="text-text-subtle text-xs font-normal tabular-nums">
{number.format(total)} maps
</span>
</Link>
{untiered > 0 && (
<span className="text-text-muted text-xs tabular-nums">
{number.format(untiered)} without a tier
</span>
)}
</div>
<ul className="grid grid-cols-5 gap-1.5">
{slices.map(({ tier, count }) => (
<li key={tier}>
<Link
href={tierHref(tier)}
href={tierHref(selected === tier ? null : tier)}
scroll={false}
aria-current={active ? "true" : undefined}
aria-current={selected === tier ? "true" : undefined}
aria-label={`Tier ${tier}: ${number.format(count)} maps`}
style={
{ "--tier": tierColor(tier, tierCount) } as CSSProperties
}
className={classNames(
"group flex h-6 items-center gap-2 rounded-sm px-2 transition-colors duration-(--duration-fast)",
active && "bg-text/8",
"flex flex-col items-center gap-1 rounded-md border py-1.5 transition-[opacity,background-color] duration-(--duration-base)",
selected === tier
? "border-(--tier)/50 bg-(--tier)/15"
: "border-border hover:bg-text/4",
dimmed(tier) && "opacity-40",
)}
>
<TierBadge tier={tier} tierCount={tierCount} className="w-8" />
<span className="text-text-muted group-hover:text-accent w-6 text-xs tabular-nums transition-colors duration-(--duration-fast)">
<TierBadge tier={tier} tierCount={tierCount} />
<span className="text-xs tabular-nums">
{number.format(count)}
</span>
</Link>
</li>
);
})}
</ul>
{untiered > 0 && (
<p className="text-text-subtle text-xs">
{number.format(untiered)} without a tier
</p>
)}
</section>
))}
</ul>
</div>
<div className="hidden min-w-0 flex-col items-center gap-4 sm:flex sm:flex-row sm:items-center sm:gap-6">
<svg
viewBox="0 0 100 100"
className="size-36 shrink-0 -rotate-90"
role="img"
aria-label={`Maps per tier, ${tierCount} tiers`}
>
{untiered > 0 && (
<circle
cx="50"
cy="50"
r={RADIUS}
fill="none"
pathLength={100}
strokeWidth={WIDTH}
className="stroke-surface-raised"
strokeDasharray={`${Math.max((untiered / total) * 100 - GAP, 0)} 100`}
strokeDashoffset={-tieredShare}
/>
)}
{slices.map(
({ tier, count, start, length }) =>
count > 0 && (
<Link
key={tier}
href={tierHref(selected === tier ? null : tier)}
scroll={false}
aria-label={`Tier ${tier}: ${number.format(count)} maps`}
>
<title>{`Tier ${tier}: ${number.format(count)} maps`}</title>
<circle
cx="50"
cy="50"
r={RADIUS}
fill="none"
pathLength={100}
strokeWidth={WIDTH}
// Very small tiers still show as a sliver rather than vanishing into the gap.
strokeDasharray={`${Math.max(length - GAP, 0.3)} 100`}
strokeDashoffset={-start}
style={
{ "--tier": tierColor(tier, tierCount) } as CSSProperties
}
className={classNames(
"stroke-(--tier) transition-[opacity,stroke-width] duration-(--duration-base) hover:stroke-[19]",
dimmed(tier) && "opacity-25",
selected === tier && "stroke-[19]",
)}
/>
</Link>
),
)}
{/* Counter-rotated, so the middle reads upright inside the turned ring. */}
<g className="[transform-origin:50px_50px] rotate-90">
<text
x="50"
y={chosen != null ? 48 : 54}
textAnchor="middle"
className="fill-text text-[13px] font-bold tabular-nums"
>
{selected != null ? `T${selected}` : "All"}
</text>
{chosen != null && (
<text
x="50"
y="61"
textAnchor="middle"
className="fill-text-muted text-[8px] tabular-nums"
>
{number.format(chosen)} maps
</text>
)}
</g>
</svg>

{/* "All tiers", always there to go back to, centered over the two columns it sums; the tiers split evenly
below it, low tiers first, and "No tier" ends the second column. */}
<div className="flex flex-col gap-1">
<Link
href={tierHref(null)}
scroll={false}
aria-current={selected == null ? "true" : undefined}
className={allTiersClasses(selected == null)}
>
All tiers
<span className="text-text-subtle text-xs font-normal tabular-nums">
{number.format(total)} maps
</span>
</Link>
<ul
style={{ "--rows": rows } as CSSProperties}
className="grid grid-flow-col grid-cols-[auto_auto_auto_auto_auto_auto] grid-rows-[repeat(var(--rows),auto)] gap-x-2 gap-y-0.5 text-sm"
>
{slices.slice(0, half).map(tierRow)}
{slices.slice(half).map(tierRow)}
{untiered > 0 && (
<li
className={classNames(
rowClasses,
"text-text-muted",
selected != null && "opacity-40",
)}
>
No tier
<span className="text-right tabular-nums">
{number.format(untiered)}
</span>
<span className="text-text-subtle tabular-nums">
{percent.format(untiered / total)}
</span>
</li>
)}
</ul>
</div>
</div>
</>
);
}