import { alias } from "@blitzkit/core"; import { literals } from "@blitzkit/i18n/src/literals"; import { CaretLeftIcon, CaretRightIcon, CaretSortIcon, } from "@radix-ui/react-icons"; import { useMemo } from "react"; import { characteristicsGroups } from "../../config/characteristicsGroups"; import { compareInsertionMarker } from "../../config/compare"; import { computeCharacteristics } from "../../hooks/useCharacteristics"; import { withLocale } from "../../hocs/withLocale"; import { useStrings } from "../../hooks/useStrings"; import { useUnwrapper } from "../../hooks/useUnwrapper"; import { CompareEphemeral, syncShellSlot, type CompareMember, } from "../../stores/compareEphemeral"; import { ComparePersistent } from "../../stores/comparePersistent"; import { SortDirection } from "../../stores/tankopediaPersistent"; import { rankingValue } from "../../tankopedia/calculateRankings"; import { resolveTankModules } from "../../tankopedia/resolveTankModules"; import { type CharacteristicName, type CharacteristicRenderConfig, type ComputedCharacteristics, } from "../../types/characteristics"; import { CharacteristicValue } from "../CharacteristicValue"; import { CompareEnvironment } from "../CompareEnvironment"; import { CompareLoadout } from "../CompareLoadout"; import { CompareTankCard } from "../CompareTankCard"; import { EmptyState } from "../EmptyState"; import { IconButton } from "../IconButton"; import { InsertionMarker } from "../InsertionMarker"; import { LazyImage } from "../LazyImage"; import { Table } from "../Table"; import { Text } from "../Text"; import { Tooltip } from "../Tooltip"; import styles from "./index.module.css"; // Removes one occurrence of each excluded id, keeping other tanks' duplicates function without(ids: number[], excluded: number[]) { const remaining = [...ids]; for (const id of excluded) { remaining.splice(remaining.indexOf(id), 1); } return remaining; } // The table of members, or an empty state when there are none export const CompareTable = withLocale(Content); function Content() { const strings = useStrings(); const members = CompareEphemeral.useDeferred( (state) => state.members, CompareEphemeral.initial.members, ); const environment = CompareEphemeral.use((state) => state.environment); // Other tanks' consumables and provisions can reduce a tank's damage, so each // tank is computed against a synthetic opponent carrying all of theirs const characteristics = useMemo(() => { const allConsumables = members.flatMap(({ state }) => state.consumables); const allProvisions = members.flatMap(({ state }) => state.provisions); return members.map((member) => { const antagonist = { ...member.state, consumables: without(allConsumables, member.state.consumables), provisions: without(allProvisions, member.state.provisions), }; return computeCharacteristics( { protagonist: member.state, antagonist: members.length > 1 ? antagonist : member.state, }, environment, ); }); }, [members, environment]); if (members.length === 0) { return ( ); } return ( <> {members.map(({ key, state }, index) => ( ))} {members.map((member, index) => ( ))} {characteristicsGroups.map((group) => ( ))} ); } interface LoadoutCellProps { index: number; } function LoadoutCell({ index }: LoadoutCellProps) { return ( ); } interface ShellSwitcherProps { member: CompareMember; index: number; } function ShellSwitcher({ member, index }: ShellSwitcherProps) { const unwrap = useUnwrapper(); const { gun, shell } = resolveTankModules(member.state); return (
{gun.shells.map((candidate, shellIndex) => { const isSelected = candidate.id === shell.id; const name = unwrap(candidate.name!); return ( { CompareEphemeral.mutate((draft) => { if (draft.syncShells) { syncShellSlot(draft.members, shellIndex); } else { draft.members[index].state.shell = candidate.id; } draft.sorting.by = null; }); }} > ); })}
); } interface GroupProps { group: (typeof characteristicsGroups)[number]; characteristics: ComputedCharacteristics[]; } function Group({ group, characteristics }: GroupProps) { const strings = useStrings(); const members = CompareEphemeral.use((state) => state.members); // A lone tank still shows its values; otherwise rows need two tanks to compare const minimumPresent = Math.min(2, characteristics.length); const items = group.order.filter( (item): item is CharacteristicRenderConfig => "name" in item && characteristics.filter((computed) => computed.has(item.name)).length >= minimumPresent, ); if (items.length === 0) return null; const title = ( strings.tanks.characteristics.titles as Record )[group.name]; return ( {title} {group.shells && members.map((member, index) => ( ))} {items.map((config) => ( ))} ); } interface RowProps { config: CharacteristicRenderConfig; characteristics: ComputedCharacteristics[]; } function Row({ config, characteristics }: RowProps) { const strings = useStrings(); const sorting = CompareEphemeral.use((state) => state.sorting); const deltaMode = ComparePersistent.use((state) => state.deltaMode); const members = CompareEphemeral.use((state) => state.members); const values = characteristics.map((computed) => rankingValue(config.name, computed.get(config.name)), ); const truth = values[0]; const isSorted = sorting.by === config.name; const label = (strings.tanks.characteristics.items as Record)[ config.name ]; return (
sort(config.name, values, members)} > {!isSorted && } {isSorted && sorting.direction === SortDirection.Ascending && ( )} {isSorted && sorting.direction === SortDirection.Descending && ( )} {label} {config.units !== undefined && ( {literals(strings.units[config.units], { value: "" })} )}
{characteristics.map((computed, index) => { const value = values[index]; const output = computed.get(config.name); const delta = value !== undefined && truth !== undefined ? value - truth : 0; return (
{output && ( )} {delta !== 0 && Number.isFinite(delta) && ( <> {deltaMode === "absolute" && ( ({delta > 0 ? "+" : ""} {Number(delta.toFixed(config.decimals ?? 2))}) )} {deltaMode === "percentage" && truth !== 0 && ( ({delta > 0 ? "+" : ""} {Math.round((delta / Math.abs(truth!)) * 100)}%) )} )}
); })}
); } function sort( name: CharacteristicName, values: (number | undefined)[], members: CompareMember[], ) { const byKey = new Map(members.map(({ key }, index) => [key, values[index]])); CompareEphemeral.mutate((draft) => { const ascending = draft.sorting?.by === name && draft.sorting.direction === SortDirection.Descending; // Missing values always sink to the end draft.members.sort((a, b) => { const valueA = byKey.get(a.key); const valueB = byKey.get(b.key); if (valueA === undefined && valueB === undefined) return 0; if (valueA === undefined) return 1; if (valueB === undefined) return -1; return ascending ? valueA - valueB : valueB - valueA; }); draft.sorting = { by: name, direction: ascending ? SortDirection.Ascending : SortDirection.Descending, }; }); }