feat: persistent sorting

This commit is contained in:
TrèsAbhi committed 2023-12-04 10:25:54 -06:00
1 parent 3e952faad3
commit 5b4601b671
3 files changed
+26 -6

No files matched your search

@@ -1,3 +1,5 @@
'use client';
import { CaretDownIcon } from '@radix-ui/react-icons'; import { CaretDownIcon } from '@radix-ui/react-icons';
import { Button, DropdownMenu, Flex } from '@radix-ui/themes'; import { Button, DropdownMenu, Flex } from '@radix-ui/themes';
import { import {
@@ -10,7 +12,7 @@ export function Options() {
const tankopediaState = useTankopedia(); const tankopediaState = useTankopedia();
return ( return (
<Flex gap="2"> <Flex gap="1">
<DropdownMenu.Root> <DropdownMenu.Root>
<DropdownMenu.Trigger> <DropdownMenu.Trigger>
<Button variant="soft"> <Button variant="soft">
@@ -26,6 +28,7 @@ export function Options() {
} }
> >
<DropdownMenu.RadioItem value="tier">Tier</DropdownMenu.RadioItem> <DropdownMenu.RadioItem value="tier">Tier</DropdownMenu.RadioItem>
<DropdownMenu.RadioItem value="name">Name</DropdownMenu.RadioItem>
</DropdownMenu.RadioGroup> </DropdownMenu.RadioGroup>
</DropdownMenu.Content> </DropdownMenu.Content>
</DropdownMenu.Root> </DropdownMenu.Root>
+21 -4
View File
@@ -32,9 +32,20 @@ export default function Page() {
const input = useRef<HTMLInputElement>(null); const input = useRef<HTMLInputElement>(null);
const sortedTanks = useMemo( const sortedTanks = useMemo(
() => () =>
awaitedTanks.sort((a, b) => { awaitedTanks.toSorted((a, b) => {
let diff = 0; let diff = 0;
if (tankopediaState.sortBy === 'tier') diff = a.tier - b.tier;
if (tankopediaState.sortBy === 'tier') {
diff = a.tier - b.tier;
}
if (tankopediaState.sortBy === 'name') {
diff = (
a.name_short ??
a.name ??
`Unknown tank ${a.id}`
).localeCompare(b.name_short ?? b.name ?? `Unknown tank ${b.id}`);
}
return tankopediaState.sortDirection === 'ascending' ? diff : -diff; return tankopediaState.sortDirection === 'ascending' ? diff : -diff;
}), }),
[tankopediaState.sortBy, tankopediaState.sortDirection], [tankopediaState.sortBy, tankopediaState.sortDirection],
@@ -43,6 +54,11 @@ export default function Page() {
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const pageInput = useRef<HTMLInputElement>(null); const pageInput = useRef<HTMLInputElement>(null);
useEffect(() => {
setSearchedList(sortedTanks);
if (input.current) input.current.value = '';
}, [sortedTanks]);
useEffect(() => { useEffect(() => {
if (pageInput.current) pageInput.current.value = `${page + 1}`; if (pageInput.current) pageInput.current.value = `${page + 1}`;
}, [page]); }, [page]);
@@ -56,9 +72,9 @@ export default function Page() {
}, [searchedList]); }, [searchedList]);
return ( return (
<PageWrapper size="wide"> <PageWrapper size="wide" color="purple">
<Suspense fallback={<Text>Loading...</Text>}> <Suspense fallback={<Text>Loading...</Text>}>
<Flex direction="column" gap="2"> <Flex direction="column" gap="3">
<TextField.Root> <TextField.Root>
<TextField.Slot> <TextField.Slot>
<MagnifyingGlassIcon height="16" width="16" /> <MagnifyingGlassIcon height="16" width="16" />
@@ -150,6 +166,7 @@ export default function Page() {
{searchedList {searchedList
.map((tank) => ( .map((tank) => (
<a <a
key={tank.id}
href={`/tools/tankopedia/${tank.id}`} href={`/tools/tankopedia/${tank.id}`}
style={{ style={{
flex: 1, flex: 1,
+1 -1
View File
@@ -2,7 +2,7 @@ import { produce } from 'immer';
import { create } from 'zustand'; import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware'; import { devtools, persist } from 'zustand/middleware';
export type TankopediaSortBy = 'tier'; export type TankopediaSortBy = 'tier' | 'name';
export type TankopediaSortDirection = 'ascending' | 'descending'; export type TankopediaSortDirection = 'ascending' | 'descending';
type Tankopedia = { type Tankopedia = {