"use client"; import { useVirtualizer } from "@tanstack/react-virtual"; import { AlertCircle, Inbox } from "lucide-react"; import { motion, useReducedMotion } from "motion/react"; import { useEffect, useRef, useState, type MutableRefObject, type ReactNode, } from "react"; import { EASE_OUT, SPRING_PANEL } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; /** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */ export const infiniteMasonryMessages = { label: "Infinite masonry feed", emptyTitle: "No items yet", emptyDescription: "New items will appear here when they become available.", loadError: "Couldn't load more", retry: "Try again", loadingMore: "Loading more items", }; export type InfiniteMasonryMessages = typeof infiniteMasonryMessages; export type InfiniteMasonryKey = string | number | bigint; export interface InfiniteMasonryProps { /** The items loaded so far, in feed order. */ items: readonly T[]; /** Returns a stable unique key for an item. */ getItemKey: (item: T, index: number) => InfiniteMasonryKey; /** Renders one item's card. */ renderItem: (item: T, index: number) => ReactNode; /** Called when the scroll nears the end and more items should load. */ onLoadMore: () => void | Promise; /** Whether more items can load. False shows `endState` after the last item. */ hasMore: boolean; /** Shows loading placeholders at the end while more items load. Default: false */ loading?: boolean; /** Error shown in a card at the end. Pauses loading while set. */ error?: ReactNode; /** Called from the error card's retry button. The button shows only when set. */ onRetry?: () => void; /** Estimated item height in px before it is measured. Default: 240 */ estimateSize?: (item: T, index: number) => number; /** Renders a loading placeholder card while more items load. */ renderLoadingItem?: (index: number) => ReactNode; /** Shown when there are no items and nothing more to load. */ emptyState?: ReactNode; /** Shown below the last item once `hasMore` is false. */ endState?: ReactNode; /** Smallest column width in px before a column is dropped. Default: 208 */ minColumnWidth?: number; /** Most columns to show. Default: 4 */ maxColumns?: number; /** Space between items in px. Default: 12 */ gap?: number; /** Rows to render past the viewport, per column. Default: 4 */ overscan?: number; /** Starts loading when this many items from the end are visible. Default: 3 */ prefetch?: number; /** Fades and slides in items added after the first render. Default: true */ animateItems?: boolean; /** Accessible name for the feed. Default: "Infinite masonry feed" */ ariaLabel?: string; /** Classes merged onto the scroll container. */ className?: string; /** Classes merged onto the inner content element that holds the items. */ contentClassName?: string; /** Classes merged onto each item wrapper, not loading or error cards. */ itemClassName?: string; /** Override any built-in string. */ labels?: Partial; } type MasonryMetrics = { columns: number; width: number; }; function useMasonryMetrics({ elementRef, gap, maxColumns, minColumnWidth, }: { elementRef: React.RefObject; gap: number; maxColumns: number; minColumnWidth: number; }) { const [metrics, setMetrics] = useState({ columns: 1, width: 0, }); useEffect(() => { const element = elementRef.current; if (!element) return; const update = (width: number) => { const columns = Math.min( maxColumns, Math.max(1, Math.floor((width + gap) / (minColumnWidth + gap))), ); setMetrics((current) => current.columns === columns && current.width === width ? current : { columns, width }, ); }; update(element.getBoundingClientRect().width); const observer = new ResizeObserver(([entry]) => { update(entry.contentRect.width); }); observer.observe(element); return () => observer.disconnect(); }, [elementRef, gap, maxColumns, minColumnWidth]); return metrics; } function DefaultLoadingItem({ index }: { index: number }) { return (