Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: list view (#565) #566

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/components/Views/DriveListView/DriveListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,52 @@ import { UpdateDriveProps } from '@interfaces/handlers';
interface DriveListViewProps extends UpdateDriveProps {
directories: DirectoryItem[];
files: FileItem[];
driveSort: string;
directoryOnClick: (directory: DirectoryItem) => void;
fileOnClick: (data: FileItem) => void;
}

const DriveListView: FC<DriveListViewProps> = ({
directories,
files,
driveSort,
directoryOnClick,
fileOnClick,
updateDrive,
}) => {
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10);
const startIndex = page * rowsPerPage;
const endIndex = startIndex + rowsPerPage;

const folderLength = directories.length;
const folderStartIndex = Math.min(page * rowsPerPage, folderLength);
const folderEndIndex = Math.min(folderStartIndex + rowsPerPage, folderLength);

const someFoldersAreDisplayed = folderEndIndex - folderStartIndex > 0;
const fileStartIndex = someFoldersAreDisplayed
? 0
: page * rowsPerPage - folderLength;
const fileEndIndex = someFoldersAreDisplayed
? rowsPerPage - (folderEndIndex - folderStartIndex)
: fileStartIndex + rowsPerPage;

const { pageDirectories, pageFiles } = useMemo(
() => ({
pageDirectories: (directories || []).slice(startIndex, endIndex),
pageFiles: (files || []).slice(
startIndex - directories.length,
endIndex - directories.length
pageDirectories: (directories || []).slice(
folderStartIndex,
folderEndIndex
),
pageFiles: (files || []).slice(fileStartIndex, fileEndIndex),
}),
[directories, files, startIndex, endIndex]
// eslint-disable-next-line react-hooks/exhaustive-deps
[
directories,
files,
driveSort,
folderStartIndex,
folderEndIndex,
fileStartIndex,
fileEndIndex,
]
);

const handlePageUp = () => {
Expand Down
1 change: 1 addition & 0 deletions src/pages/drive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ const Drive: FC = () => {
<DriveListView
directories={handleSort(directories)}
files={handleSort(files)}
driveSort={driveSort}
directoryOnClick={handleDirectoryOnClick}
fileOnClick={handleFileOnClick}
updateDrive={handleUpdateDrive}
Expand Down
Loading