Create custom photo chooser grid

This commit is contained in:
Sam Becker 2026-02-28 14:16:14 -06:00
parent 42015f7d6d
commit 0465b51427
4 changed files with 83 additions and 30 deletions

View File

@ -1,10 +1,16 @@
import AdminComponentPageClient from '@/admin/AdminComponentPageClient'; import AdminComponentPageClient from '@/admin/AdminComponentPageClient';
import { getPhotosCached } from '@/photo/cache'; import { getPhotosCached, getPhotosMetaCached } from '@/photo/cache';
export default async function ComponentsPage() { export default async function ComponentsPage() {
const photos = await getPhotosCached({ limit: 1}); const photos = await getPhotosCached();
const photosCount = await getPhotosMetaCached()
.then(({ count }) => count);
return ( return (
<AdminComponentPageClient photo={photos[0]} /> <AdminComponentPageClient
photo={photos[0]}
photos={photos}
photosCount={photosCount}
/>
); );
} }

View File

@ -15,8 +15,12 @@ import FieldsetPhotoChooser from '@/photo/form/FieldsetPhotoChooser';
export default function ComponentsPageClient({ export default function ComponentsPageClient({
photo, photo,
photos,
photosCount,
}: { }: {
photo: Photo photo: Photo
photos: Photo[]
photosCount: number
}) { }) {
const [valuePhoto, setValuePhoto] = useState(photo?.id ?? ''); const [valuePhoto, setValuePhoto] = useState(photo?.id ?? '');
const [valuePhotoChooser, setValuePhotoChooser] = useState(photo?.id ?? ''); const [valuePhotoChooser, setValuePhotoChooser] = useState(photo?.id ?? '');
@ -39,6 +43,8 @@ export default function ComponentsPageClient({
<FieldsetPhotoChooser <FieldsetPhotoChooser
label="Photo" label="Photo"
photo={photo} photo={photo}
photos={photos}
photosCount={photosCount}
value={valuePhotoChooser} value={valuePhotoChooser}
onChange={setValuePhotoChooser} onChange={setValuePhotoChooser}
/> />

View File

@ -3,10 +3,11 @@ import { altTextForPhoto, doesPhotoNeedBlurCompatibility, Photo } from '..';
import clsx from 'clsx/lite'; import clsx from 'clsx/lite';
import * as DropdownMenu from '@radix-ui/react-dropdown-menu'; import * as DropdownMenu from '@radix-ui/react-dropdown-menu';
import ImageMedium from '@/components/image/ImageMedium'; import ImageMedium from '@/components/image/ImageMedium';
import PhotoGridInfinite from '../PhotoGridInfinite';
import { menuSurfaceStyles } from '@/components/primitives/surface'; import { menuSurfaceStyles } from '@/components/primitives/surface';
import { GRID_SPACE_CLASSNAME } from '@/components'; import { GRID_SPACE_CLASSNAME } from '@/components';
import PhotoGrid from '../PhotoGrid'; import useDynamicPhoto from '../useDynamicPhoto';
import { IoSearch } from 'react-icons/io5';
import { useState } from 'react';
export default function FieldsetPhotoChooser({ export default function FieldsetPhotoChooser({
label, label,
@ -14,7 +15,6 @@ export default function FieldsetPhotoChooser({
onChange, onChange,
photo, photo,
photos = [], photos = [],
photosCount,
}: { }: {
label: string label: string
value: string value: string
@ -24,26 +24,37 @@ export default function FieldsetPhotoChooser({
photosCount?: number photosCount?: number
photosHidden?: Photo[] photosHidden?: Photo[]
}) { }) {
const [query, setQuery] = useState('');
const {
photo: photoAvatar,
isLoading: isLoadingPhotoAvatar,
} = useDynamicPhoto({
initialPhoto: photo,
photoId: value,
});
return ( return (
<> <>
<FieldsetWithStatus {...{ label, value, onChange, type: 'hidden' }} /> <FieldsetWithStatus {...{ label, value, onChange, type: 'hidden' }} />
<DropdownMenu.Root> <DropdownMenu.Root>
<DropdownMenu.Trigger asChild> <DropdownMenu.Trigger asChild>
<button type="button" className="p-1.5"> <button type="button" className="p-1.5">
{photo && <span className={clsx(
<span className={clsx( 'flex size-[6rem]',
'flex size-[6rem]', 'border border-medium rounded-[4px]',
'border border-medium rounded-[4px]', 'overflow-hidden select-none active:opacity-75',
'overflow-hidden select-none active:opacity-75', )}>
)}> {photoAvatar && <ImageMedium
<ImageMedium src={photoAvatar.url}
src={photo.url} alt={altTextForPhoto(photoAvatar)}
alt={altTextForPhoto(photo)} aspectRatio={photoAvatar.aspectRatio}
aspectRatio={photo.aspectRatio} blurDataURL={photoAvatar.blurData}
blurDataURL={photo.blurData} blurCompatibilityMode={
blurCompatibilityMode={doesPhotoNeedBlurCompatibility(photo)} doesPhotoNeedBlurCompatibility(photoAvatar)}
/> className={clsx(isLoadingPhotoAvatar && 'opacity-50')}
</span>} />}
</span>
</button> </button>
</DropdownMenu.Trigger> </DropdownMenu.Trigger>
<DropdownMenu.Portal> <DropdownMenu.Portal>
@ -55,16 +66,46 @@ export default function FieldsetPhotoChooser({
> >
<div className={clsx( <div className={clsx(
GRID_SPACE_CLASSNAME, GRID_SPACE_CLASSNAME,
'w-[14rem] max-h-[20rem] rounded-[3px] overflow-y-auto', 'w-[18rem] max-h-[20rem] rounded-[3px] overflow-y-auto',
)}> )}>
{photos.length > 0 && <div className={clsx(
<PhotoGrid {...{ photos, animate: false }} />} 'flex items-center gap-1',
{(!photosCount || photosCount > photos.length) && 'text-medium text-xs font-medium uppercase tracking-wider',
<PhotoGridInfinite {...{ 'pt-1 pb-2 px-1.5',
cacheKey: 'photo-chooser-menu', )}>
initialOffset: photos.length, <div className="grow">
animate: false, Choose photo
}} />} </div>
<IoSearch size={16} />
</div>
<input
type="text"
placeholder="Search for a photo"
className="w-full mb-2"
value={query}
onChange={e => setQuery(e.target.value)}
/>
<div className="grid grid-cols-3 gap-0.5">
{photos.map(photo => (
<span
key={photo.id}
className={clsx(
'flex w-full aspect-square object-cover',
'overflow-hidden select-none active:opacity-75',
)}
>
<ImageMedium
src={photo.url}
alt={altTextForPhoto(photo)}
aspectRatio={photo.aspectRatio}
blurDataURL={photo.blurData}
// eslint-disable-next-line max-len
blurCompatibilityMode={doesPhotoNeedBlurCompatibility(photo)}
onClick={() => onChange(photo.id)}
/>
</span>
))}
</div>
</div> </div>
</DropdownMenu.Content> </DropdownMenu.Content>
</DropdownMenu.Portal> </DropdownMenu.Portal>

View File

@ -14,7 +14,7 @@ export default function useDynamicPhoto({
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [photoIdDebounced] = useDebounce(photoId, 500); const [photoIdDebounced] = useDebounce(photoId, 500, { leading: true });
useEffect(() => { useEffect(() => {
if (photoIdDebounced) { if (photoIdDebounced) {