import { ComponentProps, ReactNode, useMemo } from 'react'; import SharedHover from '../shared-hover/SharedHover'; import { Photo, photoQuantityText } from '@/photo'; import { useSharedHoverState } from '../shared-hover/state'; import useSWR from 'swr'; import { getDimensionsFromSize } from '@/utility/size'; import PhotoMedium from '@/photo/PhotoMedium'; import Spinner from '../Spinner'; import clsx from 'clsx'; import { useAppText } from '@/i18n/state/client'; import { SWR_KEY_SHARED_HOVER } from '@/swr'; const { width, height } = getDimensionsFromSize(300, 16 / 9); export default function EntityHover({ hoverKey, header, getPhotos, photosCount, children, color, }: { hoverKey: string header: ReactNode getPhotos: () => Promise photosCount: number color?: ComponentProps['color'] children: ReactNode }) { const appText = useAppText(); const { isHoverBeingShown } = useSharedHoverState(); const isHovering = isHoverBeingShown?.(hoverKey); const { data: photos, isLoading, } = useSWR( isHovering ? `${SWR_KEY_SHARED_HOVER}-${hoverKey}` : null, getPhotos, { revalidateIfStale: false, revalidateOnFocus: false, revalidateOnReconnect: false, }); const photosToShow = useMemo(() => { if (photosCount >= 6) { return 6; } else if (photosCount >= 4) { return 4; } else if (photosCount >= 2) { return 2; } else { return 1; } }, [photosCount]); const gridClass = useMemo(() => { if (photosCount >= 6) { return 'grid-cols-3 grid-rows-2'; } else if (photosCount >= 4) { return 'grid-cols-2 grid-rows-2'; } else if (photosCount >= 2) { return 'grid-cols-2'; } else { return 'grid-cols-1'; } }, [photosCount]); const content = useMemo(() =>
{/* Photo grid */}
{Array.from({ length: photosToShow }).map((_, index) => photos?.[index] && )}
{/* Placeholder grid */}
{Array.from({ length: photosToShow }).map((_, index) =>
)}
{/* Text guard */}
{/* Text */}
{/* Header */}
{header}
{/* Caption */}
{photoQuantityText(photosCount, appText, false)} {isLoading && }
, [ gridClass, photosToShow, photos, header, photosCount, appText, isLoading, ]); return {children} ; }