feat: better compact search chunking

This commit is contained in:
TrèsAbhi committed 2024-04-06 17:28:45 -05:00
1 parent 85d350a912
commit 11b99ef211
1 file changed
+19 -6
@@ -401,14 +401,17 @@ export function TankSearch({
]); ]);
const [searchResults, setSearchedList] = useState(searchableTanks); const [searchResults, setSearchedList] = useState(searchableTanks);
const page = useTankopediaPersistent((state) => state.filters.page); const page = useTankopediaPersistent((state) => state.filters.page);
const wideFormat = typeof window !== 'undefined' && window.innerWidth > 512;
const chunkSize = wideFormat ? tanksPerPage / 2 : Infinity;
const searchResultsPageSlice = searchResults.slice( const searchResultsPageSlice = searchResults.slice(
page * tanksPerPage, page * tanksPerPage,
(page + 1) * tanksPerPage, (page + 1) * tanksPerPage,
); );
const firstChunk = searchResultsPageSlice.slice(0, chunkSize); const firstChunk = searchResultsPageSlice.slice(
const secondChunk = searchResultsPageSlice.slice(chunkSize); 0,
Math.ceil(searchResultsPageSlice.length / 2),
);
const secondChunk = searchResultsPageSlice.slice(
Math.ceil(searchResultsPageSlice.length / 2),
);
useEffect(() => { useEffect(() => {
setSearchedList(searchableTanks); setSearchedList(searchableTanks);
@@ -848,7 +851,17 @@ export function TankSearch({
{!compact && ( {!compact && (
<PageTurner tanksPerPage={tanksPerPage} searchedList={searchResults} /> <PageTurner tanksPerPage={tanksPerPage} searchedList={searchResults} />
)} )}
<Flex wrap="wrap" gap="2" justify={compact ? 'between' : 'center'}>
<Flex
wrap="wrap"
gap="2"
style={{
justifyContent:
compact && firstChunk.length > 0 && secondChunk.length > 0
? 'space-around'
: 'center',
}}
>
{!compact && {!compact &&
searchResultsPageSlice.map((tank) => ( searchResultsPageSlice.map((tank) => (
<Link <Link
@@ -970,7 +983,7 @@ export function TankSearch({
</Link> </Link>
))} ))}
{compact && ( {compact && firstChunk.length > 0 && (
<CompactSearchResultRow tanks={firstChunk} onSelect={onSelect} /> <CompactSearchResultRow tanks={firstChunk} onSelect={onSelect} />
)} )}