mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-22 05:15:32 +01:00
performance table rewrite and shared sort direction
This commit is contained in:
38 files changed
+965
-874
No files matched your search
@@ -0,0 +1,60 @@
|
||||
/* Spans the page even when the columns would fit in less */
|
||||
.root {
|
||||
--tank-width: 15rem;
|
||||
|
||||
width: 100%;
|
||||
|
||||
/* Fixed layout keeps stat columns equal. Fixed tables ignore cell minimums, so the table holds 8rem per stat column and scrolls past that */
|
||||
table {
|
||||
table-layout: fixed;
|
||||
width: 100%;
|
||||
min-width: calc(var(--tank-width) + var(--columns) * 8rem);
|
||||
}
|
||||
}
|
||||
|
||||
.corner {
|
||||
box-sizing: border-box;
|
||||
width: var(--tank-width);
|
||||
}
|
||||
|
||||
.value {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.value-skeleton {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Table cells ignore max-width, so the link inside caps the column instead */
|
||||
.tank {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
|
||||
white-space: nowrap;
|
||||
|
||||
.icon {
|
||||
flex-shrink: 0;
|
||||
/* Bleeds into the cell's padding so rows stay as tall as their text */
|
||||
height: calc(1em + 2 * var(--space-3));
|
||||
margin: calc(-1 * var(--space-3)) 0;
|
||||
aspect-ratio: 4 / 3;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.glyph {
|
||||
flex-shrink: 0;
|
||||
width: 1em;
|
||||
height: 1em;
|
||||
}
|
||||
|
||||
.name {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
}
|
||||
|
||||
.tank-skeleton {
|
||||
width: 100%;
|
||||
}
|
||||
@@ -0,0 +1,391 @@
|
||||
import { tankIcon } from "@blitzkit/core";
|
||||
import {
|
||||
TankType,
|
||||
type AverageDefinitionsAllStats,
|
||||
type TankDefinition,
|
||||
} from "@blitzkit/protos";
|
||||
import {
|
||||
memo,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
type CSSProperties,
|
||||
} from "react";
|
||||
import { api } from "../../api/dynamic";
|
||||
import {
|
||||
performanceStatNames,
|
||||
performanceStats,
|
||||
type PerformanceEntry,
|
||||
type PerformanceStatName,
|
||||
} from "../../config/performanceStats";
|
||||
import { awaitableAverageDefinitions } from "../../core/awaitables/averageDefinitions";
|
||||
import { awaitableDiscoveredIdsDefinitions } from "../../core/awaitables/discoveredIdsDefinitions";
|
||||
import { filterTanks } from "../../core/blitzkit/filterTanks";
|
||||
import { useLocale } from "../../hooks/useLocale";
|
||||
import { useStrings } from "../../hooks/useStrings";
|
||||
import { useUnwrapper } from "../../hooks/useUnwrapper";
|
||||
import { App } from "../../stores/app";
|
||||
import { Performance } from "../../stores/performance";
|
||||
import { TankFilters } from "../../stores/tankFilters";
|
||||
import { SortDirection } from "../../stores/tankopediaPersistent";
|
||||
import type { MaybeSkeletonComponentProps } from "../../types/maybeSkeletonComponentProps";
|
||||
import { ClassIcon } from "../ClassIcon";
|
||||
import { EmptyState } from "../EmptyState";
|
||||
import { ExperimentIcon } from "../ExperimentIcon";
|
||||
import { IncrementalLoader } from "../IncrementalLoader";
|
||||
import { InlineSkeleton } from "../InlineSkeleton";
|
||||
import { LinkI18n } from "../LinkI18n";
|
||||
import { Skeleton } from "../Skeleton";
|
||||
import { Table } from "../Table";
|
||||
import { Text } from "../Text";
|
||||
import styles from "./index.module.css";
|
||||
|
||||
const [tankDefinitions, averageDefinitions, discoveredIdsDefinitions] =
|
||||
await Promise.all([
|
||||
api.tanks(),
|
||||
awaitableAverageDefinitions,
|
||||
awaitableDiscoveredIdsDefinitions,
|
||||
]);
|
||||
|
||||
// Only tanks with statistics can be listed
|
||||
const tanksWithAverages = Object.values(tankDefinitions.tanks).filter(
|
||||
({ id }) => id in averageDefinitions.averages,
|
||||
);
|
||||
|
||||
// Samples only cover part of the discovered players, so player counts are scaled up to all of them
|
||||
const exclusionRatio =
|
||||
discoveredIdsDefinitions.count / averageDefinitions.samples!.total;
|
||||
|
||||
interface PerformanceRow {
|
||||
tank: TankDefinition;
|
||||
entry: PerformanceEntry;
|
||||
values: Record<PerformanceStatName, number>;
|
||||
}
|
||||
|
||||
function computeValues(entry: PerformanceEntry) {
|
||||
const values = {} as Record<PerformanceStatName, number>;
|
||||
|
||||
for (const name of performanceStatNames) {
|
||||
values[name] = performanceStats[name].value(entry);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
export function PerformanceTable() {
|
||||
const strings = useStrings();
|
||||
const filters = TankFilters.use();
|
||||
const wargaming = App.use((state) => state.logins.wargaming);
|
||||
const [filtered, setFiltered] = useState<TankDefinition[] | null>(null);
|
||||
|
||||
// Filtering can wait on the player's garage, so the last results stay up until it settles
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
filterTanks(filters, tanksWithAverages, wargaming?.id).then((tanks) => {
|
||||
if (!cancelled) setFiltered(tanks);
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [filters, wargaming?.id]);
|
||||
|
||||
if (filtered !== null && filtered.length === 0) {
|
||||
return (
|
||||
<EmptyState
|
||||
title={strings.performance.no_tanks.title}
|
||||
body={strings.performance.no_tanks.body}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Table.Root
|
||||
scrollable
|
||||
className={styles.root}
|
||||
style={{ "--columns": performanceStatNames.length } as CSSProperties}
|
||||
>
|
||||
<Head />
|
||||
<Body filtered={filtered} />
|
||||
</Table.Root>
|
||||
);
|
||||
}
|
||||
|
||||
function Head() {
|
||||
return (
|
||||
<Table.Head>
|
||||
<Table.Row>
|
||||
<Table.IconCorner className={styles.corner} />
|
||||
|
||||
{performanceStatNames.map((name) => (
|
||||
<ColumnHeader key={name} name={name} />
|
||||
))}
|
||||
</Table.Row>
|
||||
</Table.Head>
|
||||
);
|
||||
}
|
||||
|
||||
interface BodyProps {
|
||||
filtered: TankDefinition[] | null;
|
||||
}
|
||||
|
||||
// Owns sorting and period, so changing them never re-renders the head
|
||||
function Body({ filtered }: BodyProps) {
|
||||
const sorting = Performance.use((state) => state.sorting);
|
||||
const period = Performance.use((state) => state.player_count_period);
|
||||
|
||||
const rows = useMemo(() => {
|
||||
if (filtered === null) return null;
|
||||
|
||||
return filtered.map((tank) => {
|
||||
const average = averageDefinitions.averages[tank.id];
|
||||
const entry = {
|
||||
stats: average.mu!,
|
||||
players: exclusionRatio * average.samples![period],
|
||||
} satisfies PerformanceEntry;
|
||||
|
||||
return {
|
||||
tank,
|
||||
entry,
|
||||
values: computeValues(entry),
|
||||
} satisfies PerformanceRow;
|
||||
});
|
||||
}, [filtered, period]);
|
||||
|
||||
// Every tank's stats weighted by how many players it had in the period
|
||||
const total = useMemo(() => {
|
||||
if (rows === null) return null;
|
||||
|
||||
const stats = {} as AverageDefinitionsAllStats;
|
||||
let samples = 0;
|
||||
|
||||
for (const { tank } of rows) {
|
||||
const average = averageDefinitions.averages[tank.id];
|
||||
const weight = average.samples![period];
|
||||
|
||||
samples += weight;
|
||||
|
||||
for (const key in average.mu!) {
|
||||
const stat = key as keyof AverageDefinitionsAllStats;
|
||||
|
||||
stats[stat] = (stats[stat] ?? 0) + weight * average.mu![stat];
|
||||
}
|
||||
}
|
||||
|
||||
return computeValues({ stats, players: exclusionRatio * samples });
|
||||
}, [rows, period]);
|
||||
|
||||
const sorted = useMemo(() => {
|
||||
if (rows === null) return null;
|
||||
|
||||
const direction = sorting.direction === SortDirection.Ascending ? 1 : -1;
|
||||
|
||||
return [...rows].sort(
|
||||
(a, b) => direction * (a.values[sorting.by] - b.values[sorting.by]),
|
||||
);
|
||||
}, [rows, sorting]);
|
||||
|
||||
const data = useMemo(
|
||||
() =>
|
||||
(sorted ?? []).map((row) => ({
|
||||
key: `${row.tank.id}`,
|
||||
row,
|
||||
total: total!,
|
||||
})),
|
||||
[sorted, total],
|
||||
);
|
||||
|
||||
return (
|
||||
<Table.Body>
|
||||
<TotalRow total={total} />
|
||||
|
||||
<IncrementalLoader
|
||||
skeleton={sorted === null}
|
||||
initial={32}
|
||||
intermediate={8}
|
||||
data={data}
|
||||
Component={Row}
|
||||
/>
|
||||
</Table.Body>
|
||||
);
|
||||
}
|
||||
|
||||
interface TotalRowProps {
|
||||
total: Record<PerformanceStatName, number> | null;
|
||||
}
|
||||
|
||||
function TotalRow({ total }: TotalRowProps) {
|
||||
const strings = useStrings();
|
||||
|
||||
return (
|
||||
<Table.Row>
|
||||
<Table.RowHeader>{strings.performance.total}</Table.RowHeader>
|
||||
|
||||
{performanceStatNames.map((name) => (
|
||||
<Table.Cell key={name}>
|
||||
{total === null ? (
|
||||
<Skeleton className={styles["value-skeleton"]} />
|
||||
) : (
|
||||
<Value name={name} value={total[name]} />
|
||||
)}
|
||||
</Table.Cell>
|
||||
))}
|
||||
</Table.Row>
|
||||
);
|
||||
}
|
||||
|
||||
interface ColumnHeaderProps {
|
||||
name: PerformanceStatName;
|
||||
}
|
||||
|
||||
function ColumnHeader({ name }: ColumnHeaderProps) {
|
||||
const strings = useStrings();
|
||||
const sorting = Performance.use((state) => state.sorting);
|
||||
|
||||
return (
|
||||
<Table.ColumnHeaderSortable
|
||||
direction={sorting.by === name ? sorting.direction : undefined}
|
||||
onSort={() => {
|
||||
Performance.mutate((draft) => {
|
||||
const ascending =
|
||||
draft.sorting.by === name &&
|
||||
draft.sorting.direction === SortDirection.Descending;
|
||||
|
||||
draft.sorting = {
|
||||
by: name,
|
||||
direction: ascending
|
||||
? SortDirection.Ascending
|
||||
: SortDirection.Descending,
|
||||
};
|
||||
});
|
||||
}}
|
||||
>
|
||||
{strings.common.stats[name]}
|
||||
</Table.ColumnHeaderSortable>
|
||||
);
|
||||
}
|
||||
|
||||
interface ValueProps {
|
||||
name: PerformanceStatName;
|
||||
value: number;
|
||||
}
|
||||
|
||||
function Value({ name, value }: ValueProps) {
|
||||
const locale = useLocale();
|
||||
|
||||
return (
|
||||
<Text className={styles.value}>
|
||||
{Number.isFinite(value)
|
||||
? performanceStats[name].format(value, locale)
|
||||
: "-"}
|
||||
</Text>
|
||||
);
|
||||
}
|
||||
|
||||
interface RowProps {
|
||||
row: PerformanceRow;
|
||||
total: Record<PerformanceStatName, number>;
|
||||
}
|
||||
|
||||
// Memoized so loading more rows leaves the loaded ones be
|
||||
const Row = memo((props: MaybeSkeletonComponentProps<RowProps>) => {
|
||||
if (props.skeleton) {
|
||||
return <SkeletonRow onIntersection={props.onIntersection} />;
|
||||
}
|
||||
|
||||
const { row, total } = props;
|
||||
|
||||
return (
|
||||
<Table.Row>
|
||||
<Table.RowHeader>
|
||||
<TankLink tank={row.tank} />
|
||||
</Table.RowHeader>
|
||||
|
||||
{performanceStatNames.map((name) => (
|
||||
<Table.CellRanked
|
||||
key={name}
|
||||
ranking={performanceStats[name].ranking}
|
||||
value={row.values[name]}
|
||||
truth={total[name]}
|
||||
>
|
||||
<Value name={name} value={row.values[name]} />
|
||||
</Table.CellRanked>
|
||||
))}
|
||||
</Table.Row>
|
||||
);
|
||||
});
|
||||
|
||||
interface TankLinkProps {
|
||||
tank: TankDefinition;
|
||||
}
|
||||
|
||||
function TankLink({ tank }: TankLinkProps) {
|
||||
const locale = useLocale();
|
||||
const unwrap = useUnwrapper();
|
||||
const name = unwrap(tank.name!);
|
||||
|
||||
return (
|
||||
<LinkI18n
|
||||
locale={locale}
|
||||
href={`/tanks/${tank.slug}`}
|
||||
underline="hover"
|
||||
className={styles.tank}
|
||||
color={
|
||||
tank.type === TankType.TANK_TYPE_COLLECTOR
|
||||
? "blue"
|
||||
: tank.type === TankType.TANK_TYPE_PREMIUM
|
||||
? "amber"
|
||||
: "gray"
|
||||
}
|
||||
highContrast={tank.type === TankType.TANK_TYPE_RESEARCHABLE}
|
||||
>
|
||||
<img
|
||||
alt={name}
|
||||
src={tankIcon(tank.id)}
|
||||
draggable={false}
|
||||
className={styles.icon}
|
||||
/>
|
||||
|
||||
<ClassIcon class={tank.class} className={styles.glyph} />
|
||||
{tank.testing && <ExperimentIcon className={styles.glyph} />}
|
||||
|
||||
<span className={styles.name}>{name}</span>
|
||||
</LinkI18n>
|
||||
);
|
||||
}
|
||||
|
||||
interface SkeletonRowProps {
|
||||
onIntersection?: () => void;
|
||||
}
|
||||
|
||||
function SkeletonRow({ onIntersection }: SkeletonRowProps) {
|
||||
const row = useRef<HTMLTableRowElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!onIntersection) return;
|
||||
|
||||
const observer = new IntersectionObserver(([entry]) => {
|
||||
if (entry.isIntersecting) onIntersection();
|
||||
});
|
||||
|
||||
observer.observe(row.current!);
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<Table.Row ref={row}>
|
||||
<Table.RowHeader>
|
||||
<InlineSkeleton className={styles["tank-skeleton"]} />
|
||||
</Table.RowHeader>
|
||||
|
||||
{performanceStatNames.map((name) => (
|
||||
<Table.Cell key={name}>
|
||||
<InlineSkeleton className={styles["value-skeleton"]} />
|
||||
</Table.Cell>
|
||||
))}
|
||||
</Table.Row>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user