import { useEffect, useLayoutEffect, useRef, useState } from 'react' import { UseTableResult } from '../IVTable/useTable' import MediaGridItem from './MediaGridItem' import { GridItem } from '@interval/sdk/dist/ioSchema' import { TableControls } from '../IVTable/DesktopTable' import useWindowSize from '~/utils/useWindowSize' function useGrid({ idealColumnWidth, gap, data, }: { idealColumnWidth: number gap: number data: UseTableResult['data'] }) { const { width, height } = useWindowSize() const firstItemRef = useRef(null) const [itemHeight, setItemHeight] = useState(200) const minWidth = idealColumnWidth - idealColumnWidth / 8 const columnCount = Math.floor(width / (minWidth + gap)) const columnWidth = Math.floor(height / columnCount - gap) useLayoutEffect(() => { if (!firstItemRef.current) return setItemHeight(firstItemRef.current.getBoundingClientRect().height) }, [data]) return { columnCount, columnWidth, itemHeight, firstItemRef, } } const MIN_USER_COLUMN_WIDTH = 100 export type IVMediaGridProps = { idealColumnWidth: number table: UseTableResult } export default function IVMediaGrid(props: IVMediaGridProps) { const [lastRowsCount, setLastRowsCount] = useState(10) const { columnCount, itemHeight, firstItemRef } = useGrid({ idealColumnWidth: Math.max(MIN_USER_COLUMN_WIDTH, props.idealColumnWidth), data: props.table.data, gap: 32, }) const [isStuck, setIsStuck] = useState(false) const headerEl = useRef(null) useEffect(() => { if (!props.table.currentPage.length) return setLastRowsCount(props.table.currentPage.length) }, [props.table.currentPage.length]) // toggle .rounded-t-lg when the header becomes stuck/unstuck useEffect(() => { if (!headerEl.current) return const observer = new IntersectionObserver( ([e]) => setIsStuck(e.intersectionRatio < 1), { threshold: [1] } ) observer.observe(headerEl.current) }, []) return (
{props.table.currentPage.length > 0 ? (
{props.table.currentPage.map((item, idx) => ( ))}
) : ( <> {props.table.isFetching ? ( // loading state consists of empty rows x the previous number of rows
{Array.from({ length: lastRowsCount }).map((_, idx) => (
 
))}
) : (
{props.table.searchQuery ? (

No records found matching '{props.table.searchQuery}'

) : (

No records found

)}
)} )}
) }