2022-07-20 13:13:50 +00:00
|
|
|
import React, { memo, useCallback, useEffect, useRef } from "react";
|
2022-07-15 08:58:58 +00:00
|
|
|
import AutoSizer from "react-virtualized-auto-sizer";
|
|
|
|
import InfiniteLoader from "react-window-infinite-loader";
|
|
|
|
import { VariableSizeList as List, areEqual } from "react-window";
|
2022-08-09 10:43:24 +00:00
|
|
|
//import Scroll from "./Scroll";
|
2022-07-15 08:58:58 +00:00
|
|
|
|
|
|
|
const GridComponent = ({
|
|
|
|
hasMoreFiles,
|
|
|
|
filesLength,
|
|
|
|
itemCount,
|
|
|
|
loadMoreItems,
|
|
|
|
onScroll,
|
|
|
|
countTilesInRow,
|
2022-07-20 13:13:50 +00:00
|
|
|
selectedFolderId,
|
2022-07-15 08:58:58 +00:00
|
|
|
children,
|
|
|
|
className,
|
2022-08-09 10:43:24 +00:00
|
|
|
listRef,
|
2022-07-15 08:58:58 +00:00
|
|
|
}) => {
|
2022-07-20 13:13:50 +00:00
|
|
|
const gridRef = useRef(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-08-09 10:43:24 +00:00
|
|
|
//TODO: inf-scroll it is slow
|
2022-07-20 13:13:50 +00:00
|
|
|
//console.log("resetAfterIndex");
|
|
|
|
|
|
|
|
gridRef?.current?.resetAfterIndex(0);
|
|
|
|
}, [selectedFolderId]);
|
|
|
|
|
2022-07-15 08:58:58 +00:00
|
|
|
const isItemLoaded = useCallback(
|
|
|
|
(index) => {
|
|
|
|
return !hasMoreFiles || index * countTilesInRow < filesLength;
|
|
|
|
},
|
|
|
|
[filesLength, hasMoreFiles, countTilesInRow]
|
|
|
|
);
|
|
|
|
|
2022-07-20 09:31:57 +00:00
|
|
|
const renderTile = memo(({ index, style }) => {
|
|
|
|
return <div style={style}>{children[index]}</div>;
|
2022-07-15 08:58:58 +00:00
|
|
|
}, areEqual);
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
const fileHeight = 220 + horizontalGap;
|
2022-07-20 09:31:57 +00:00
|
|
|
const titleHeight = 20 + headerMargin;
|
2022-07-15 08:58:58 +00:00
|
|
|
|
|
|
|
return isFolder ? folderHeight : isFile ? fileHeight : titleHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderGrid = ({ height, width }) => {
|
|
|
|
return (
|
|
|
|
<InfiniteLoader
|
|
|
|
isItemLoaded={isItemLoaded}
|
|
|
|
itemCount={itemCount}
|
|
|
|
loadMoreItems={loadMoreItems}
|
|
|
|
>
|
|
|
|
{({ onItemsRendered, ref }) => (
|
|
|
|
<List
|
|
|
|
onScroll={onScroll}
|
|
|
|
className={className}
|
|
|
|
height={height}
|
2022-08-09 10:43:24 +00:00
|
|
|
width={width}
|
2022-07-20 09:31:57 +00:00
|
|
|
itemCount={children.length}
|
2022-07-15 08:58:58 +00:00
|
|
|
itemSize={getItemSize}
|
|
|
|
onItemsRendered={onItemsRendered}
|
2022-08-09 10:43:24 +00:00
|
|
|
ref={(refList) => {
|
2022-07-20 13:13:50 +00:00
|
|
|
ref(listRef);
|
2022-08-09 10:43:24 +00:00
|
|
|
gridRef.current = refList;
|
|
|
|
listRef.current = refList;
|
2022-07-20 13:13:50 +00:00
|
|
|
}}
|
2022-08-09 10:43:24 +00:00
|
|
|
//outerElementType={Scroll}
|
2022-07-20 09:31:57 +00:00
|
|
|
overscanCount={5} //TODO: inf-scroll
|
2022-08-09 10:43:24 +00:00
|
|
|
style={{ height: "100% !important" }}
|
2022-07-15 08:58:58 +00:00
|
|
|
>
|
|
|
|
{renderTile}
|
|
|
|
</List>
|
|
|
|
)}
|
|
|
|
</InfiniteLoader>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-07-20 09:31:57 +00:00
|
|
|
//console.log("GridComponent render");
|
|
|
|
|
2022-07-15 08:58:58 +00:00
|
|
|
return <AutoSizer>{renderGrid}</AutoSizer>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default GridComponent;
|