mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
Fix: keep tank order stable on select
Tanks sharing the same tier, nation, class and type were shown in an arbitrary order that changed after selecting a tank. The meta sort had no tiebreaker, and the filtered list was sorted and reversed in place, so each re-sort started from the previous reversed order and flipped tied tanks around. - Added tank ID as the first sort key in `metaSortTank` so tied tanks always come out in the same order - Sorted a copy of the tank search results, pre-sorted by ID, instead of the cached list - Added an `exclude` option to tank search and used it in Compare so tanks already being compared can't be added again, including through select all or by swapping another slot Fixes #313
This commit is contained in:
1 parent
116c30be88
commit
273881c460
4 files changed
+28
-4
No files matched your search
@@ -12,6 +12,7 @@ export function metaSortTank(
|
|||||||
gameDefinitions: GameDefinitions,
|
gameDefinitions: GameDefinitions,
|
||||||
) {
|
) {
|
||||||
return tanks
|
return tanks
|
||||||
|
.sort((a, b) => a.id - b.id)
|
||||||
.sort((a, b) => b.tier - a.tier)
|
.sort((a, b) => b.tier - a.tier)
|
||||||
.sort(
|
.sort(
|
||||||
(a, b) => treeTypeOrder.indexOf(b.type) - treeTypeOrder.indexOf(a.type),
|
(a, b) => treeTypeOrder.indexOf(b.type) - treeTypeOrder.indexOf(a.type),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { PlusIcon, TrashIcon } from "@radix-ui/react-icons";
|
import { PlusIcon, TrashIcon } from "@radix-ui/react-icons";
|
||||||
import { Button, Dialog, Flex, SegmentedControl } from "@radix-ui/themes";
|
import { Button, Dialog, Flex, SegmentedControl } from "@radix-ui/themes";
|
||||||
|
import { useMemo } from "react";
|
||||||
import { api } from "../../core/blitzkit/api";
|
import { api } from "../../core/blitzkit/api";
|
||||||
import { tankToCompareMember } from "../../core/blitzkit/tankToCompareMember";
|
import { tankToCompareMember } from "../../core/blitzkit/tankToCompareMember";
|
||||||
import { useLocale } from "../../hooks/useLocale";
|
import { useLocale } from "../../hooks/useLocale";
|
||||||
@@ -22,6 +23,11 @@ export function Controls({
|
|||||||
onAddTankDialogOpenChange,
|
onAddTankDialogOpenChange,
|
||||||
}: ControlsProps) {
|
}: ControlsProps) {
|
||||||
const deltaMode = ComparePersistent.use((state) => state.deltaMode);
|
const deltaMode = ComparePersistent.use((state) => state.deltaMode);
|
||||||
|
const members = CompareEphemeral.use((state) => state.members);
|
||||||
|
const memberIds = useMemo(
|
||||||
|
() => members.map((member) => member.tank.id),
|
||||||
|
[members],
|
||||||
|
);
|
||||||
const { strings } = useLocale();
|
const { strings } = useLocale();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -53,6 +59,7 @@ export function Controls({
|
|||||||
>
|
>
|
||||||
<TankSearch
|
<TankSearch
|
||||||
compact
|
compact
|
||||||
|
exclude={memberIds}
|
||||||
onSelect={(tank) => {
|
onSelect={(tank) => {
|
||||||
CompareEphemeral.mutate((draft) => {
|
CompareEphemeral.mutate((draft) => {
|
||||||
draft.members.push(
|
draft.members.push(
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { ExternalLinkIcon, LoopIcon, TrashIcon } from "@radix-ui/react-icons";
|
import { ExternalLinkIcon, LoopIcon, TrashIcon } from "@radix-ui/react-icons";
|
||||||
import { Dialog, Flex, IconButton } from "@radix-ui/themes";
|
import { Dialog, Flex, IconButton } from "@radix-ui/themes";
|
||||||
import { useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { api } from "../../core/blitzkit/api";
|
import { api } from "../../core/blitzkit/api";
|
||||||
import { tankToCompareMember } from "../../core/blitzkit/tankToCompareMember";
|
import { tankToCompareMember } from "../../core/blitzkit/tankToCompareMember";
|
||||||
import { useLocale } from "../../hooks/useLocale";
|
import { useLocale } from "../../hooks/useLocale";
|
||||||
@@ -18,6 +18,14 @@ const provisionDefinitions = await api.provisionDefinitions();
|
|||||||
export function TankControl({ index, slug }: TankControlProps) {
|
export function TankControl({ index, slug }: TankControlProps) {
|
||||||
const { locale, strings } = useLocale();
|
const { locale, strings } = useLocale();
|
||||||
const [switchTankDialogOpen, setSwitchTankDialogOpen] = useState(false);
|
const [switchTankDialogOpen, setSwitchTankDialogOpen] = useState(false);
|
||||||
|
const members = CompareEphemeral.use((state) => state.members);
|
||||||
|
const otherMemberIds = useMemo(
|
||||||
|
() =>
|
||||||
|
members
|
||||||
|
.filter((_, memberIndex) => memberIndex !== index)
|
||||||
|
.map((member) => member.tank.id),
|
||||||
|
[members, index],
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Flex gap="3" justify="center" style={{ width: "100%" }}>
|
<Flex gap="3" justify="center" style={{ width: "100%" }}>
|
||||||
@@ -57,6 +65,7 @@ export function TankControl({ index, slug }: TankControlProps) {
|
|||||||
>
|
>
|
||||||
<TankSearch
|
<TankSearch
|
||||||
compact
|
compact
|
||||||
|
exclude={otherMemberIds}
|
||||||
onSelect={(tank) => {
|
onSelect={(tank) => {
|
||||||
CompareEphemeral.mutate((draft) => {
|
CompareEphemeral.mutate((draft) => {
|
||||||
draft.members[index] = tankToCompareMember(
|
draft.members[index] = tankToCompareMember(
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export type TankSearchProps = MaybeSkeletonComponentProps &
|
|||||||
compact?: boolean;
|
compact?: boolean;
|
||||||
onSelect?: (tank: TankDefinition) => void;
|
onSelect?: (tank: TankDefinition) => void;
|
||||||
onSelectAll?: (tanks: TankDefinition[]) => void;
|
onSelectAll?: (tanks: TankDefinition[]) => void;
|
||||||
|
exclude?: number[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const PREVIEW_COUNT = 20;
|
const PREVIEW_COUNT = 20;
|
||||||
@@ -52,14 +53,14 @@ const [gameDefinitions, modelDefinitions, tankDefinitions, tankNames] =
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
export const TankSearch = memo<TankSearchProps>(
|
export const TankSearch = memo<TankSearchProps>(
|
||||||
({ compact, onSelect, onSelectAll, skeleton, ...props }) => {
|
({ compact, onSelect, onSelectAll, exclude, skeleton, ...props }) => {
|
||||||
const { strings, locale } = useLocale();
|
const { strings, locale } = useLocale();
|
||||||
const wargaming = App.use((state) => state.logins.wargaming);
|
const wargaming = App.use((state) => state.logins.wargaming);
|
||||||
const awaitedTanksDefinitionsArray = Object.values(tankDefinitions.tanks);
|
const awaitedTanksDefinitionsArray = Object.values(tankDefinitions.tanks);
|
||||||
const tankopediaSort = TankSort.use();
|
const tankopediaSort = TankSort.use();
|
||||||
const tankFilters = TankFilters.use();
|
const tankFilters = TankFilters.use();
|
||||||
|
|
||||||
const filtered = usePromise(
|
const allFiltered = usePromise(
|
||||||
() => {
|
() => {
|
||||||
if (tankFilters.search === null) {
|
if (tankFilters.search === null) {
|
||||||
return filterTanks(
|
return filterTanks(
|
||||||
@@ -81,9 +82,15 @@ export const TankSearch = memo<TankSearchProps>(
|
|||||||
[tankFilters] as any,
|
[tankFilters] as any,
|
||||||
);
|
);
|
||||||
const sorted = useMemo(() => {
|
const sorted = useMemo(() => {
|
||||||
|
const filtered = allFiltered.filter(
|
||||||
|
(tank) => !exclude?.includes(tank.id),
|
||||||
|
);
|
||||||
|
|
||||||
if (tankFilters.search === null) {
|
if (tankFilters.search === null) {
|
||||||
let sorted: TankDefinition[];
|
let sorted: TankDefinition[];
|
||||||
|
|
||||||
|
filtered.sort((a, b) => a.id - b.id);
|
||||||
|
|
||||||
switch (tankopediaSort.by) {
|
switch (tankopediaSort.by) {
|
||||||
case "meta.none":
|
case "meta.none":
|
||||||
sorted = metaSortTank(filtered, gameDefinitions);
|
sorted = metaSortTank(filtered, gameDefinitions);
|
||||||
@@ -417,7 +424,7 @@ export const TankSearch = memo<TankSearchProps>(
|
|||||||
} else {
|
} else {
|
||||||
return filtered;
|
return filtered;
|
||||||
}
|
}
|
||||||
}, [tankFilters, tankopediaSort]);
|
}, [tankFilters, tankopediaSort, exclude]);
|
||||||
|
|
||||||
const [loadedCards, setLoadedCards] = useState(DEFAULT_LOADED_CARDS);
|
const [loadedCards, setLoadedCards] = useState(DEFAULT_LOADED_CARDS);
|
||||||
const tanks = sorted.slice(0, loadedCards);
|
const tanks = sorted.slice(0, loadedCards);
|
||||||
|
|||||||
Reference in new issue
Block a user