2022-08-15 14:41:39 +00:00
|
|
|
import React, { useCallback, useEffect, createRef } from "react";
|
2022-08-12 11:45:14 +00:00
|
|
|
import { InfiniteLoader, WindowScroller } from "react-virtualized";
|
2022-08-12 07:35:32 +00:00
|
|
|
import { StyledList } from "./StyledInfiniteLoader";
|
2022-07-15 08:58:58 +00:00
|
|
|
|
|
|
|
const GridComponent = ({
|
|
|
|
hasMoreFiles,
|
|
|
|
filesLength,
|
|
|
|
itemCount,
|
|
|
|
loadMoreItems,
|
|
|
|
onScroll,
|
|
|
|
countTilesInRow,
|
|
|
|
children,
|
|
|
|
className,
|
2022-08-12 07:35:32 +00:00
|
|
|
scroll,
|
2022-07-15 08:58:58 +00:00
|
|
|
}) => {
|
2022-08-15 14:41:39 +00:00
|
|
|
const loaderRef = createRef();
|
2022-08-15 14:22:50 +00:00
|
|
|
|
2022-07-15 08:58:58 +00:00
|
|
|
const isItemLoaded = useCallback(
|
2022-08-12 07:35:32 +00:00
|
|
|
({ index }) => {
|
2022-08-12 14:52:21 +00:00
|
|
|
return !hasMoreFiles || (index + 1) * countTilesInRow < filesLength;
|
2022-07-15 08:58:58 +00:00
|
|
|
},
|
|
|
|
[filesLength, hasMoreFiles, countTilesInRow]
|
|
|
|
);
|
|
|
|
|
2022-08-12 07:35:32 +00:00
|
|
|
const renderTile = ({ index, style, key }) => {
|
2022-08-10 08:37:14 +00:00
|
|
|
return (
|
2022-08-12 07:35:32 +00:00
|
|
|
<div className="window-item" style={style} key={key}>
|
2022-08-10 08:37:14 +00:00
|
|
|
{children[index]}
|
|
|
|
</div>
|
|
|
|
);
|
2022-08-12 07:35:32 +00:00
|
|
|
};
|
2022-07-15 08:58:58 +00:00
|
|
|
|
2022-08-12 07:35:32 +00:00
|
|
|
const getItemSize = ({ index }) => {
|
2022-07-20 09:31:57 +00:00
|
|
|
const itemClassNames = children[index]?.props?.className;
|
|
|
|
const isFile = itemClassNames?.includes("isFile");
|
|
|
|
const isFolder = itemClassNames?.includes("isFolder");
|
2022-08-24 11:09:58 +00:00
|
|
|
const isRoom = itemClassNames?.includes("isRoom");
|
2022-08-17 12:11:48 +00:00
|
|
|
const isFolderHeader = itemClassNames?.includes("folder_header");
|
2022-07-15 08:58:58 +00:00
|
|
|
|
|
|
|
const horizontalGap = 16;
|
|
|
|
const verticalGap = 14;
|
2022-07-20 09:31:57 +00:00
|
|
|
const headerMargin = 15;
|
2022-07-15 08:58:58 +00:00
|
|
|
|
|
|
|
const folderHeight = 64 + verticalGap;
|
2022-08-24 11:09:58 +00:00
|
|
|
const roomHeight = 122 + verticalGap;
|
2022-07-15 08:58:58 +00:00
|
|
|
const fileHeight = 220 + horizontalGap;
|
2022-08-17 12:11:48 +00:00
|
|
|
const titleHeight = 20 + headerMargin + (isFolderHeader ? 0 : 11);
|
2022-07-15 08:58:58 +00:00
|
|
|
|
2022-08-24 11:09:58 +00:00
|
|
|
if (isRoom) return roomHeight;
|
|
|
|
if (isFolder) return folderHeight;
|
|
|
|
if (isFile) return fileHeight;
|
|
|
|
return titleHeight;
|
2022-07-15 08:58:58 +00:00
|
|
|
};
|
|
|
|
|
2022-08-12 07:35:32 +00:00
|
|
|
return (
|
|
|
|
<InfiniteLoader
|
|
|
|
isRowLoaded={isItemLoaded}
|
|
|
|
rowCount={itemCount}
|
|
|
|
loadMoreRows={loadMoreItems}
|
2022-08-15 14:22:50 +00:00
|
|
|
ref={loaderRef}
|
2022-08-12 07:35:32 +00:00
|
|
|
>
|
|
|
|
{({ onRowsRendered, registerChild }) => (
|
|
|
|
<WindowScroller scrollElement={scroll}>
|
2022-08-12 11:45:14 +00:00
|
|
|
{({ height, isScrolling, onChildScroll, scrollTop }) => {
|
2022-08-12 12:31:21 +00:00
|
|
|
if (height === undefined) {
|
|
|
|
height = scroll.getBoundingClientRect().height;
|
|
|
|
}
|
|
|
|
|
2022-08-12 11:45:14 +00:00
|
|
|
const width =
|
|
|
|
document.getElementById("tileContainer")?.getBoundingClientRect()
|
|
|
|
.width ?? 0;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledList
|
|
|
|
autoHeight
|
|
|
|
height={height}
|
|
|
|
onRowsRendered={onRowsRendered}
|
|
|
|
ref={registerChild}
|
|
|
|
rowCount={children.length}
|
|
|
|
rowHeight={getItemSize}
|
|
|
|
rowRenderer={renderTile}
|
|
|
|
width={width}
|
|
|
|
className={className}
|
|
|
|
isScrolling={isScrolling}
|
|
|
|
onChildScroll={onChildScroll}
|
|
|
|
scrollTop={scrollTop}
|
|
|
|
overscanRowCount={3}
|
|
|
|
onScroll={onScroll}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
2022-08-12 07:35:32 +00:00
|
|
|
</WindowScroller>
|
|
|
|
)}
|
|
|
|
</InfiniteLoader>
|
|
|
|
);
|
2022-07-15 08:58:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default GridComponent;
|