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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChampionPortrait } from "#src/assets/index.tsx";
import { cn } from "#src/lib/cn.ts";

/** The diameters the redesigned pages use, in CSS pixels. */
export const CHAMPION_FACE_SIZES = [26, 36, 40, 46, 76, 92] as const;
export const CHAMPION_FACE_SIZES = [26, 36, 40, 46, 72, 76, 92] as const;
export type ChampionFaceSize = (typeof CHAMPION_FACE_SIZES)[number];

export type ChampionFaceResult = "win" | "loss";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,14 @@ describe("ResultStrip", () => {
});

describe("WinRateBar", () => {
test("formats at most one decimal", () => {
expect(formatPercent(65)).toBe("65%");
test("always shows one decimal, so 52.0% sits beside 52.6%", () => {
expect(formatPercent(65)).toBe("65.0%");
expect(formatPercent(52)).toBe("52.0%");
expect(formatPercent(51.8)).toBe("51.8%");
expect(formatPercent(43.14)).toBe("43.1%");
expect(formatPercent(50.04)).toBe("50%");
expect(formatPercent(50.04)).toBe("50.0%");
expect(formatPercent(0)).toBe("0.0%");
expect(formatPercent(100)).toBe("100.0%");
});

test("places values on a track the way the public page does", () => {
Expand All @@ -130,7 +133,8 @@ describe("WinRateBar", () => {
test("renders a filled bar with its value and an accessible summary", () => {
const html = renderToStaticMarkup(<WinRateBar value={65} even you />);
expect(html).toContain('style="width:65%"');
expect(html).toContain('aria-label="Win rate 65%."');
expect(html).toContain("65.0%</b>");
expect(html).toContain('aria-label="Win rate 65.0%."');
expect(html).toContain('data-you="true"');
expect(html).toContain("scout-win-rate__even");
});
Expand All @@ -154,6 +158,22 @@ describe("WinRateBar", () => {
expect(html).toContain("50%, even");
});

test("shows the ends of a likely range with one decimal", () => {
const html = renderToStaticMarkup(
<WinRateBar
variant="range"
value={52}
range={{ low: 51, high: 53 }}
domain={[48, 56]}
/>,
);
expect(html).toContain("51.0%");
expect(html).toContain("53.0%");
expect(html).toContain(
'aria-label="52.0%, likely between 51.0% and 53.0%.',
);
});

test("fails loudly on a value outside 0 to 100 or a backwards range", () => {
expect(() => renderToStaticMarkup(<WinRateBar value={101} />)).toThrow(
RangeError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const FriendsRanking: Story = {
key={friend.name}
style={{
display: "grid",
gridTemplateColumns: "46px minmax(0, 1fr) 7.5rem",
gridTemplateColumns: "46px minmax(0, 1fr) 8rem",
alignItems: "center",
gap: "0.875rem",
padding: "0.4rem 0.75rem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export type WinRateBarProps = {
className?: string;
};

/** "65", "51.8": at most one decimal, no trailing zero. */
/** A rate always shows one decimal: "65.0%", "51.8%", so a column lines up. */
export function formatPercent(value: number): string {
return `${String(Number(value.toFixed(1)))}%`;
return `${value.toFixed(1)}%`;
}

function assertPercent(name: string, value: number): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { GameAssetImage } from "#src/assets/index.tsx";
import { cn } from "#src/lib/cn.ts";

/** A game icon a row can show: an item, rune, summoner spell, ability or lane. */
export type AssetRef = {
kind: "item" | "rune" | "spell" | "ability" | "lane";
key: string | number;
};

/**
* A small square icon from the game assets. The label is the icon's accessible
* name; pass an empty string when the words beside it already say it.
*/
export function AssetIcon({
asset,
label,
className,
}: {
asset: AssetRef;
label: string;
className?: string;
}) {
return (
<span className={cn("scout-asset-icon", className)} data-kind={asset.kind}>
<GameAssetImage kind={asset.kind} assetKey={asset.key} alt={label} />
</span>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import type { Meta, StoryObj } from "@storybook/react-vite";
import { ChampionCard, type ChampionCardProps } from "./champion-card.tsx";

const meta = {
title: "Domain/ChampionCard",
component: ChampionCard,
tags: ["autodocs"],
args: {
champion: "Jinx",
href: "#jinx",
lane: { label: "Bot", icon: "adc" },
winRate: 52.9,
games: 9840,
laneWinRate: 53.8,
laneGames: 1970,
tier: "S tier",
},
} satisfies Meta<typeof ChampionCard>;

export default meta;

type Story = StoryObj<typeof meta>;

/** A single card at the width of one column of the index. */
const oneColumn: Story["render"] = (args) => (
<div style={{ maxWidth: "13rem" }}>
<ChampionCard {...args} />
</div>
);

export const Default: Story = {
render: oneColumn,
};

export const WithoutLane: Story = {
render: oneColumn,
args: { champion: "Ahri", winRate: 51.8, games: 12_480 },
};

export const LongName: Story = {
render: oneColumn,
args: {
champion: "Nunu",
lane: { label: "Jungle", icon: "jungle" },
tier: "B tier",
},
};

export const SmallSample: Story = {
render: oneColumn,
args: {
champion: "Aatrox",
winRate: 60,
games: 5,
laneWinRate: 100,
laneGames: 1,
},
};

const cards: ChampionCardProps[] = [
{
champion: "Jinx",
href: "#a",
lane: { label: "Bot", icon: "adc" },
winRate: 52.9,
games: 9840,
laneWinRate: 53.8,
laneGames: 1970,
tier: "S tier",
},
{
champion: "Thresh",
href: "#b",
lane: { label: "Support", icon: "support" },
winRate: 52.4,
games: 11_200,
laneWinRate: 50.6,
laneGames: 2240,
tier: "S tier",
},
{
champion: "Ahri",
href: "#c",
lane: { label: "Mid", icon: "middle" },
winRate: 51.8,
games: 12_480,
laneWinRate: 52.4,
laneGames: 2460,
tier: "A tier",
},
{
champion: "Nunu",
href: "#d",
lane: { label: "Jungle", icon: "jungle" },
winRate: 50.2,
games: 4120,
},
{
champion: "Renata",
href: "#e",
lane: { label: "Support", icon: "support" },
winRate: 49.1,
games: 3870,
laneWinRate: 48.7,
laneGames: 770,
tier: "C tier",
},
{
champion: "Garen",
href: "#f",
lane: { label: "Top", icon: "top" },
winRate: 50.9,
games: 8120,
tier: "B tier",
},
];

export const Index: Story = {
parameters: { controls: { disable: true } },
render: () => (
<ul
className="scout-row-list"
style={{
gridTemplateColumns: "repeat(auto-fill, minmax(10.5rem, 1fr))",
gap: "1rem",
}}
>
{cards.map((card) => (
<li key={card.champion} style={{ display: "grid" }}>
<ChampionCard {...card} />
</li>
))}
</ul>
),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { getBrowserChampionName } from "@scout-for-lol/data/browser-assets";
import { LaneIcon } from "#src/assets/index.tsx";
import { cn } from "#src/lib/cn.ts";
import { ChampionFace } from "#src/domain/profile/champion-face.tsx";
import { formatPercent } from "#src/domain/profile/win-rate-bar.tsx";
import { gamesLabel } from "./format.ts";

export type ChampionCardLane = {
/** The lane's name on the card: "Bot". */
label: string;
/** The lane icon key: "top", "jungle", "middle", "adc", "support". */
icon: string;
};

export type ChampionCardProps = {
champion: string | number;
href: string;
lane?: ChampionCardLane;
/** Win rate in percent, and the games behind it. */
winRate: number;
games: number;
/** Win rate in lane in percent, and the games behind it. */
laneWinRate?: number;
laneGames?: number;
/** "S tier". */
tier?: string;
className?: string;
};

/** A champion on the index: face, name, lane, win rate and the games behind it. */
export function ChampionCard({
champion,
href,
lane,
winRate,
games,
laneWinRate,
laneGames,
tier,
className,
}: ChampionCardProps) {
return (
<a className={cn("scout-champion-card", className)} href={href}>
<ChampionFace champion={champion} size={72} alt="" />
<b className="scout-champion-card__name">
{getBrowserChampionName(champion)}
</b>
{lane === undefined ? null : (
<span className="scout-champion-card__lane">
<LaneIcon lane={lane.icon} alt="" />
{lane.label}
</span>
)}
<span className="scout-champion-card__win">
<b>{formatPercent(winRate)}</b> wins
</span>
<span className="scout-champion-card__from">
from {gamesLabel(games)}
</span>
{laneWinRate === undefined ? null : (
<span className="scout-champion-card__from scout-champion-card__lane-line">
<span>Wins lane {formatPercent(laneWinRate)}</span>
{laneGames === undefined ? null : (
<span>from {laneGames.toLocaleString("en-US")}</span>
)}
</span>
)}
{tier === undefined ? null : (
<span className="scout-champion-card__tier-slot">
<span className="scout-champion-card__tier">{tier}</span>
</span>
)}
</a>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** The games behind a win rate, with thousands separators: "7,840 games". */
export function gamesLabel(games: number): string {
return `${games.toLocaleString("en-US")} ${games === 1 ? "game" : "games"}`;
}
Loading