import { useResponsive } from 'antd-style'; import { ReactNode, memo, useMemo } from 'react'; import { Flexbox } from 'react-layout-kit'; import Grid from './Grid'; import { MAX_SIZE_DESKTOP, MAX_SIZE_MOBILE } from './style'; interface GalleyGridProps { items: T[]; renderItem: (props: T) => ReactNode; } const GalleyGrid = memo(({ items, renderItem: Render }) => { const { mobile } = useResponsive(); const { firstRow, lastRow } = useMemo(() => { if (items.length === 4) { return { firstRow: items.slice(0, 2), lastRow: items.slice(2, 4), }; } const firstCol = items.length > 4 ? 3 : items.length; return { firstRow: items.slice(0, firstCol), lastRow: items.slice(firstCol, items.length), }; }, [items]); const { gap, max } = useMemo(() => { let scale = firstRow.length * (firstRow.length / items.length); scale = scale < 1 ? 1 : scale; return { gap: mobile ? 4 : 6, max: (mobile ? MAX_SIZE_MOBILE : MAX_SIZE_DESKTOP) * scale, }; }, [mobile, items]); return ( {firstRow.map((i, index) => ( ))} {lastRow.length > 0 && ( {lastRow.map((i, index) => ( ))} )} ); }); export default GalleyGrid;