Files
blitzkit/packages/website/src/components/Gallery/List.tsx
T

59 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-10-24 17:50:18 -05:00
import { Flex, Text } from '@radix-ui/themes';
2024-10-24 17:32:46 -05:00
import fuzzysort from 'fuzzysort';
2024-10-24 17:46:02 -05:00
import { times } from 'lodash-es';
import { useMemo, useState } from 'react';
2024-10-24 17:32:46 -05:00
import { GalleryEphemeral } from '../../stores/galleryEphemeral';
2024-10-24 17:46:02 -05:00
import { GalleryCard } from './Card';
2024-10-24 17:32:46 -05:00
interface GalleryListProps {
avatars: Avatar[];
}
export interface Avatar {
name: string;
id: string;
}
2024-10-24 17:50:18 -05:00
const DEFAULT_LOADED = 42;
2024-11-24 20:39:21 -06:00
const PREVIEW_COUNT = 28;
2024-10-24 17:46:02 -05:00
2024-10-24 17:32:46 -05:00
export function GalleryList({ avatars }: GalleryListProps) {
const search = GalleryEphemeral.use((state) => state.search);
2024-10-24 17:46:02 -05:00
const [loadedCards, setLoadedCards] = useState(DEFAULT_LOADED);
2024-10-24 17:32:46 -05:00
const filtered = useMemo(() => {
2024-10-24 17:46:02 -05:00
setLoadedCards(DEFAULT_LOADED);
2024-10-24 17:32:46 -05:00
if (search === undefined) {
return avatars;
} else {
return fuzzysort
2024-10-25 10:49:11 -05:00
.go(search, avatars, { keys: ['name', 'id'] })
2024-10-24 17:32:46 -05:00
.map((result) => result.obj);
}
}, [search]);
return (
2024-10-24 17:50:18 -05:00
<>
<Text align="center" color="gray">
{filtered.length.toLocaleString()} avatars
</Text>
2024-10-24 17:46:02 -05:00
2024-10-24 17:52:23 -05:00
<Flex wrap="wrap" gap="4" justify="center">
2024-10-24 17:50:18 -05:00
{filtered.slice(0, loadedCards).map((avatar) => (
<GalleryCard key={avatar.id} id={avatar.id} name={avatar.name} />
))}
{times(
Math.min(PREVIEW_COUNT, filtered.length - loadedCards),
(index) => (
<GalleryCard
key={index}
skeleton
onIntersection={() => setLoadedCards((state) => state + 2)}
/>
),
)}
</Flex>
</>
2024-10-24 17:32:46 -05:00
);
}