2024-01-21 11:06:40 -06:00
|
|
|
import { slateDark } from '@radix-ui/colors';
|
|
|
|
|
import { CaretDownIcon, MagnifyingGlassIcon } from '@radix-ui/react-icons';
|
|
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
Card,
|
|
|
|
|
DropdownMenu,
|
|
|
|
|
Flex,
|
|
|
|
|
Inset,
|
|
|
|
|
Text,
|
|
|
|
|
TextField,
|
|
|
|
|
} from '@radix-ui/themes';
|
|
|
|
|
import { go } from 'fuzzysort';
|
|
|
|
|
import { Suspense, use, useEffect, useMemo, useRef, useState } from 'react';
|
2024-02-06 13:18:39 -06:00
|
|
|
import { TankType, TreeType } from '../../../../../components/Tanks';
|
2024-01-21 11:06:40 -06:00
|
|
|
import { asset } from '../../../../../core/blitzkrieg/asset';
|
|
|
|
|
import {
|
2024-02-06 13:18:39 -06:00
|
|
|
NATIONS,
|
2024-01-21 11:06:40 -06:00
|
|
|
TANK_ICONS,
|
|
|
|
|
TANK_ICONS_COLLECTOR,
|
|
|
|
|
TANK_ICONS_PREMIUM,
|
|
|
|
|
TIER_ROMAN_NUMERALS,
|
|
|
|
|
TankDefinition,
|
|
|
|
|
tanksDefinitionsArray,
|
|
|
|
|
} from '../../../../../core/blitzkrieg/tankDefinitions';
|
|
|
|
|
import { tankIcon } from '../../../../../core/blitzkrieg/tankIcon';
|
|
|
|
|
import { theme } from '../../../../../stitches.config';
|
|
|
|
|
import mutateTankopediaPersistent, {
|
|
|
|
|
TankopediaSortBy,
|
|
|
|
|
TankopediaSortDirection,
|
|
|
|
|
useTankopediaPersistent,
|
|
|
|
|
} from '../../../../../stores/tankopedia';
|
|
|
|
|
import * as styles from '../../page.css';
|
|
|
|
|
import { PageTurner } from '../PageTurner';
|
|
|
|
|
import { CompactSearchResultRow } from './components/CompactSearchResultRow';
|
|
|
|
|
import { Options } from './components/Options';
|
|
|
|
|
|
|
|
|
|
interface TankSearchProps {
|
|
|
|
|
compact?: boolean;
|
|
|
|
|
onSelect?: (tank: TankDefinition) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 13:18:39 -06:00
|
|
|
const tankTypeOrder: TankType[] = ['light', 'medium', 'heavy', 'tankDestroyer'];
|
|
|
|
|
const treeTypeOrder: TreeType[] = ['researchable', 'premium', 'collector'];
|
|
|
|
|
|
2024-01-21 11:06:40 -06:00
|
|
|
export function TankSearch({ compact, onSelect = () => {} }: TankSearchProps) {
|
2024-02-06 13:28:05 -06:00
|
|
|
const tanksPerPage = compact ? 64 : 96;
|
2024-02-06 13:18:39 -06:00
|
|
|
const nations = use(NATIONS);
|
2024-01-21 11:06:40 -06:00
|
|
|
const filters = useTankopediaPersistent((state) => state.filters);
|
|
|
|
|
const sort = useTankopediaPersistent((state) => state.sort);
|
2024-02-06 13:18:39 -06:00
|
|
|
const awaitedTanks = use(
|
|
|
|
|
tanksDefinitionsArray.then((tanks) =>
|
|
|
|
|
tanks
|
|
|
|
|
.sort(
|
|
|
|
|
(a, b) =>
|
|
|
|
|
treeTypeOrder.indexOf(a.treeType) -
|
|
|
|
|
treeTypeOrder.indexOf(b.treeType),
|
|
|
|
|
)
|
|
|
|
|
.sort(
|
|
|
|
|
(a, b) =>
|
|
|
|
|
tankTypeOrder.indexOf(a.type) - tankTypeOrder.indexOf(b.type),
|
|
|
|
|
)
|
|
|
|
|
.sort((a, b) => nations.indexOf(a.nation) - nations.indexOf(b.nation)),
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-01-21 11:06:40 -06:00
|
|
|
const input = useRef<HTMLInputElement>(null);
|
|
|
|
|
const searchableTanks = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
awaitedTanks
|
|
|
|
|
.filter(
|
|
|
|
|
(tank) =>
|
|
|
|
|
(filters.tiers.length === 0
|
|
|
|
|
? true
|
|
|
|
|
: filters.tiers.includes(tank.tier)) &&
|
|
|
|
|
(filters.types.length === 0
|
|
|
|
|
? true
|
|
|
|
|
: filters.types.includes(tank.type)) &&
|
|
|
|
|
(filters.treeTypes.length === 0
|
|
|
|
|
? true
|
2024-02-03 15:54:21 -06:00
|
|
|
: filters.treeTypes.includes(tank.treeType)) &&
|
2024-01-21 11:06:40 -06:00
|
|
|
(filters.nations.length === 0
|
|
|
|
|
? true
|
|
|
|
|
: filters.nations.includes(tank.nation)) &&
|
|
|
|
|
(filters.test === 'include'
|
|
|
|
|
? true
|
|
|
|
|
: filters.test === 'exclude'
|
|
|
|
|
? !tank.testing
|
|
|
|
|
: tank.testing),
|
|
|
|
|
)
|
|
|
|
|
.sort((a, b) => {
|
|
|
|
|
let diff = 0;
|
|
|
|
|
|
|
|
|
|
if (sort.by === 'tier') {
|
|
|
|
|
diff = a.tier - b.tier;
|
|
|
|
|
}
|
|
|
|
|
if (sort.by === 'name') {
|
|
|
|
|
diff = a.name.localeCompare(b.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sort.direction === 'ascending' ? diff : -diff;
|
|
|
|
|
}),
|
|
|
|
|
[filters, sort],
|
|
|
|
|
);
|
|
|
|
|
const [searchResults, setSearchedList] = useState(searchableTanks);
|
|
|
|
|
const page = useTankopediaPersistent((state) => state.filters.page);
|
2024-01-21 21:06:12 -06:00
|
|
|
const chunkSize =
|
|
|
|
|
typeof window !== 'undefined' && window.innerWidth > 512
|
|
|
|
|
? tanksPerPage / 2
|
|
|
|
|
: Infinity;
|
2024-01-21 11:06:40 -06:00
|
|
|
const searchResultsPageSlice = searchResults.slice(
|
|
|
|
|
page * tanksPerPage,
|
|
|
|
|
(page + 1) * tanksPerPage,
|
|
|
|
|
);
|
|
|
|
|
const firstChunk = searchResultsPageSlice.slice(0, chunkSize);
|
|
|
|
|
const secondChunk = searchResultsPageSlice.slice(chunkSize);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setSearchedList(searchableTanks);
|
|
|
|
|
if (input.current) input.current.value = '';
|
|
|
|
|
}, [searchableTanks]);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
mutateTankopediaPersistent((draft) => {
|
|
|
|
|
draft.filters.page = Math.min(
|
|
|
|
|
Math.max(0, page),
|
|
|
|
|
Math.ceil(searchResults.length / tanksPerPage) - 1,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}, [searchResults]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Flex direction="column" gap="4">
|
|
|
|
|
<Flex gap="2">
|
|
|
|
|
<TextField.Root style={{ flex: 1 }}>
|
|
|
|
|
<TextField.Slot>
|
|
|
|
|
<MagnifyingGlassIcon height="16" width="16" />
|
|
|
|
|
</TextField.Slot>
|
|
|
|
|
<TextField.Input
|
|
|
|
|
ref={input}
|
|
|
|
|
placeholder="Search tanks..."
|
|
|
|
|
onChange={(event) => {
|
|
|
|
|
if (event.target.value.length === 0) {
|
|
|
|
|
setSearchedList(searchableTanks);
|
|
|
|
|
} else {
|
|
|
|
|
setSearchedList(
|
|
|
|
|
go(event.target.value, searchableTanks, {
|
2024-01-21 16:28:26 -06:00
|
|
|
keys: [
|
|
|
|
|
'name',
|
2024-02-03 16:33:33 -06:00
|
|
|
'nameFull',
|
2024-01-21 16:28:26 -06:00
|
|
|
'id',
|
|
|
|
|
] satisfies (keyof TankDefinition)[],
|
2024-01-21 11:06:40 -06:00
|
|
|
}).map(({ obj }) => obj),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</TextField.Root>
|
|
|
|
|
|
|
|
|
|
<DropdownMenu.Root>
|
|
|
|
|
<DropdownMenu.Trigger>
|
|
|
|
|
<Button variant="soft">
|
|
|
|
|
Sort
|
|
|
|
|
<CaretDownIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</DropdownMenu.Trigger>
|
|
|
|
|
<DropdownMenu.Content>
|
|
|
|
|
<DropdownMenu.Label>By</DropdownMenu.Label>
|
|
|
|
|
<DropdownMenu.RadioGroup
|
|
|
|
|
value={sort.by}
|
|
|
|
|
onValueChange={(value) =>
|
|
|
|
|
mutateTankopediaPersistent((draft) => {
|
|
|
|
|
draft.sort.by = value as TankopediaSortBy;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<DropdownMenu.RadioItem value="tier">
|
|
|
|
|
Tier
|
|
|
|
|
</DropdownMenu.RadioItem>
|
|
|
|
|
<DropdownMenu.RadioItem value="name">
|
|
|
|
|
Name
|
|
|
|
|
</DropdownMenu.RadioItem>
|
|
|
|
|
</DropdownMenu.RadioGroup>
|
|
|
|
|
|
|
|
|
|
<DropdownMenu.Separator />
|
|
|
|
|
|
|
|
|
|
<DropdownMenu.Label>Direction</DropdownMenu.Label>
|
|
|
|
|
<DropdownMenu.RadioGroup
|
|
|
|
|
value={sort.direction}
|
|
|
|
|
onValueChange={(value) =>
|
|
|
|
|
mutateTankopediaPersistent((draft) => {
|
|
|
|
|
draft.sort.direction = value as TankopediaSortDirection;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<DropdownMenu.RadioItem value="ascending">
|
|
|
|
|
Ascending
|
|
|
|
|
</DropdownMenu.RadioItem>
|
|
|
|
|
<DropdownMenu.RadioItem value="descending">
|
|
|
|
|
Descending
|
|
|
|
|
</DropdownMenu.RadioItem>
|
|
|
|
|
</DropdownMenu.RadioGroup>
|
|
|
|
|
</DropdownMenu.Content>
|
|
|
|
|
</DropdownMenu.Root>
|
|
|
|
|
</Flex>
|
|
|
|
|
|
2024-01-21 11:09:02 -06:00
|
|
|
<Card variant={compact ? 'ghost' : 'surface'}>
|
2024-01-21 11:06:40 -06:00
|
|
|
<Suspense fallback={<Text>Loading...</Text>}>
|
|
|
|
|
<Options />
|
|
|
|
|
</Suspense>
|
|
|
|
|
</Card>
|
|
|
|
|
</Flex>
|
|
|
|
|
|
|
|
|
|
{!compact && (
|
|
|
|
|
<PageTurner tanksPerPage={tanksPerPage} searchedList={searchResults} />
|
|
|
|
|
)}
|
|
|
|
|
|
2024-02-06 13:28:05 -06:00
|
|
|
<Flex wrap="wrap" gap="2" justify={compact ? 'between' : 'center'}>
|
2024-01-21 11:06:40 -06:00
|
|
|
{!compact &&
|
|
|
|
|
searchResultsPageSlice.map((tank) => (
|
|
|
|
|
<Card
|
|
|
|
|
key={tank.id}
|
|
|
|
|
onClick={() => onSelect(tank)}
|
|
|
|
|
className={styles.listing}
|
|
|
|
|
style={{
|
|
|
|
|
flex: 1,
|
|
|
|
|
cursor: 'pointer',
|
2024-02-06 13:28:05 -06:00
|
|
|
height: 64,
|
2024-01-21 11:06:40 -06:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Inset
|
|
|
|
|
key={tank.id}
|
|
|
|
|
style={{
|
|
|
|
|
minWidth: 256,
|
2024-02-06 13:28:05 -06:00
|
|
|
minHeight: 64,
|
2024-01-21 11:06:40 -06:00
|
|
|
position: 'relative',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
className={styles.flag}
|
|
|
|
|
src={asset(`flags/scratched/${tank.nation}.webp`)}
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
objectFit: 'contain',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
width: 256,
|
|
|
|
|
height: 128,
|
|
|
|
|
}}
|
|
|
|
|
className={styles.listingImage}
|
|
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
src={tankIcon(tank.id)}
|
|
|
|
|
style={{
|
|
|
|
|
objectFit: 'contain',
|
|
|
|
|
objectPosition: 'left center',
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
className={styles.listingShadow}
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
height: '100%',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
transition: `box-shadow ${theme.durations.regular}`,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Flex
|
|
|
|
|
align="center"
|
|
|
|
|
justify="between"
|
|
|
|
|
className={styles.listingLabel}
|
|
|
|
|
style={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
bottom: 8,
|
|
|
|
|
padding: '0 12px',
|
|
|
|
|
width: '100%',
|
|
|
|
|
boxSizing: 'border-box',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Flex align="center" justify="center" gap="1">
|
|
|
|
|
<img
|
|
|
|
|
src={
|
2024-02-03 15:54:21 -06:00
|
|
|
(tank.treeType === 'collector'
|
2024-01-21 11:06:40 -06:00
|
|
|
? TANK_ICONS_COLLECTOR
|
2024-02-03 15:54:21 -06:00
|
|
|
: tank.treeType === 'premium'
|
2024-01-21 11:06:40 -06:00
|
|
|
? TANK_ICONS_PREMIUM
|
|
|
|
|
: TANK_ICONS)[tank.type]
|
|
|
|
|
}
|
|
|
|
|
style={{ width: '1em', height: '1em' }}
|
|
|
|
|
/>
|
|
|
|
|
<Text
|
|
|
|
|
size="4"
|
|
|
|
|
color={
|
2024-02-03 15:54:21 -06:00
|
|
|
tank.treeType === 'collector'
|
2024-01-21 11:06:40 -06:00
|
|
|
? 'blue'
|
2024-02-03 15:54:21 -06:00
|
|
|
: tank.treeType === 'premium'
|
2024-01-21 11:06:40 -06:00
|
|
|
? 'amber'
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
|
|
|
|
weight="medium"
|
|
|
|
|
style={{
|
|
|
|
|
color:
|
2024-02-03 15:54:21 -06:00
|
|
|
tank.treeType === 'researchable'
|
2024-01-21 11:06:40 -06:00
|
|
|
? slateDark.slate12
|
|
|
|
|
: undefined,
|
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
|
maxWidth: 160,
|
|
|
|
|
display: 'block',
|
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{tank.name}
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
|
|
|
|
|
|
|
|
|
<Text size="4" color="gray" highContrast={false}>
|
|
|
|
|
{TIER_ROMAN_NUMERALS[tank.tier]}
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
|
|
|
|
</Inset>
|
|
|
|
|
</Card>
|
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
{compact && (
|
|
|
|
|
<CompactSearchResultRow tanks={firstChunk} onSelect={onSelect} />
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{compact && secondChunk.length > 0 && (
|
|
|
|
|
<CompactSearchResultRow tanks={secondChunk} onSelect={onSelect} />
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
|
|
|
|
|
|
|
|
|
<PageTurner tanksPerPage={tanksPerPage} searchedList={searchResults} />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|