Skip to content

Commit 6f45d97

Browse files
authored
feat(storage-browser): paginate upload view (#6628)
* feat(storage-browser): paginate upload view
1 parent 70e0d1e commit 6f45d97

File tree

8 files changed

+46
-3
lines changed

8 files changed

+46
-3
lines changed

.changeset/cold-comics-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@aws-amplify/ui-react-storage": patch
3+
---
4+
5+
feat(storage-browser): paginate upload view

packages/react-storage/src/components/StorageBrowser/actions/handlers/listLocationItems.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const listLocationItemsHandler: ListLocationItemsHandler = async (
150150
result.push(
151151
...(exclude ? items.filter((item) => item.type !== exclude) : items)
152152
);
153-
} while (nextNextToken && result.length < pageSize);
153+
} while (nextNextToken && result.length < _pageSize);
154154

155155
return { items: result, nextToken: nextNextToken };
156156
};

packages/react-storage/src/components/StorageBrowser/views/LocationActionView/UploadView/UploadView.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
MessageControl,
1616
StatusDisplayControl,
1717
TitleControl,
18+
PaginationControl,
1819
} from '../../../controls';
1920

2021
import { UploadViewProvider } from './UploadViewProvider';
@@ -32,6 +33,7 @@ export const UploadView: UploadViewType = ({ className, ...props }) => {
3233
<ViewElement className={`${STORAGE_BROWSER_BLOCK}__controls`}>
3334
<OverwriteToggleControl />
3435
<ViewElement className={`${STORAGE_BROWSER_BLOCK}__buttons`}>
36+
<PaginationControl />
3537
<AddFolderControl />
3638
<AddFilesControl />
3739
</ViewElement>
@@ -65,6 +67,7 @@ UploadView.Provider = UploadViewProvider;
6567

6668
UploadView.AddFiles = AddFilesControl;
6769
UploadView.AddFolder = AddFolderControl;
70+
UploadView.Pagination = PaginationControl;
6871
UploadView.Cancel = ActionCancelControl;
6972
UploadView.Destination = ActionDestinationControl;
7073
UploadView.DropZone = DropZoneControl;

packages/react-storage/src/components/StorageBrowser/views/LocationActionView/UploadView/UploadViewProvider.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export function UploadViewProvider({
3232
} = displayText;
3333

3434
const {
35+
hasNextPage,
36+
highestPageVisited,
37+
page,
3538
isOverwritingEnabled,
3639
isProcessing,
3740
isProcessingComplete,
@@ -42,6 +45,7 @@ export function UploadViewProvider({
4245
onActionStart,
4346
onActionCancel,
4447
onDropFiles,
48+
onPaginate,
4549
onActionExit,
4650
onTaskRemove,
4751
onSelectFiles,
@@ -92,6 +96,11 @@ export function UploadViewProvider({
9296
isOverwriteToggleDisabled: isProcessing || isProcessingComplete,
9397
isOverwritingEnabled,
9498
overwriteToggleLabel,
99+
paginationData: {
100+
page,
101+
hasNextPage,
102+
highestPageVisited,
103+
},
95104
destination: location,
96105
message: actionCompleteMessage ?? filesValidationMessage,
97106
statusCounts,
@@ -105,6 +114,7 @@ export function UploadViewProvider({
105114
onActionCancel={onActionCancel}
106115
onActionExit={onActionExit}
107116
onActionStart={onActionStart}
117+
onPaginate={onPaginate}
108118
onAddFiles={() => {
109119
onSelectFiles('FILE');
110120
}}

packages/react-storage/src/components/StorageBrowser/views/LocationActionView/UploadView/__tests__/UploadView.spec.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const onActionExit = jest.fn();
3131
const onDropFiles = jest.fn();
3232
const onSelectFiles = jest.fn();
3333
const onToggleOverwrite = jest.fn();
34+
const onPaginate = jest.fn();
3435

3536
const callbacks = {
3637
onActionCancel,
@@ -39,6 +40,7 @@ const callbacks = {
3940
onActionExit,
4041
onSelectFiles,
4142
onToggleOverwrite,
43+
onPaginate,
4244
};
4345

4446
const statusCounts = { ...INITIAL_STATUS_COUNTS };
@@ -75,6 +77,9 @@ const initialViewState: UploadViewState = {
7577
isProcessingComplete: false,
7678
isProcessing: false,
7779
tasks: [],
80+
page: 1,
81+
hasNextPage: false,
82+
highestPageVisited: 1,
7883
invalidFiles: undefined,
7984
statusCounts,
8085
};

packages/react-storage/src/components/StorageBrowser/views/LocationActionView/UploadView/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import type {
88
} from '../types';
99

1010
export interface UploadViewState extends ActionViewState<UploadHandlerData> {
11+
hasNextPage: boolean;
12+
highestPageVisited: number;
1113
isOverwritingEnabled: boolean;
1214
onDropFiles: (files: File[]) => void;
1315
onSelectFiles: (type: 'FILE' | 'FOLDER') => void;
1416
onToggleOverwrite: () => void;
17+
onPaginate: (page: number) => void;
18+
page: number;
1519
invalidFiles: FileItems | undefined;
1620
}
1721

@@ -38,6 +42,7 @@ export interface UploadViewType
3842
Statuses: () => React.JSX.Element | null;
3943
TasksTable: () => React.JSX.Element | null;
4044
Title: () => React.JSX.Element | null;
45+
Pagination: () => React.JSX.Element | null;
4146
}
4247

4348
export interface UseUploadViewOptions {

packages/react-storage/src/components/StorageBrowser/views/LocationActionView/UploadView/useUploadView.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { useAction } from '../../../useAction';
88

99
import { DEFAULT_OVERWRITE_ENABLED } from './constants';
1010
import type { UploadViewState, UseUploadViewOptions } from './types';
11+
import { usePaginate } from '../../hooks/usePaginate';
1112

13+
const DEFAULT_PAGE_SIZE = 100;
1214
export const useUploadView = (
1315
options?: UseUploadViewOptions
1416
): UploadViewState => {
@@ -38,6 +40,14 @@ export const useUploadView = (
3840
handleUploads,
3941
] = useAction('upload', { items });
4042

43+
const {
44+
currentPage,
45+
handlePaginate,
46+
pageItems: pageTasks,
47+
} = usePaginate({
48+
items: tasks,
49+
});
50+
4151
const onDropFiles = (files: File[]) => {
4252
if (files) {
4353
fileItemsDispatch({ type: 'ADD_FILES', files });
@@ -79,11 +89,15 @@ export const useUploadView = (
7989
location,
8090
invalidFiles,
8191
statusCounts,
82-
tasks,
92+
tasks: pageTasks,
93+
page: currentPage,
94+
hasNextPage: currentPage * DEFAULT_PAGE_SIZE < items.length,
95+
highestPageVisited: Math.ceil(items.length / DEFAULT_PAGE_SIZE),
8396
onActionCancel,
8497
onActionExit,
8598
onActionStart,
8699
onDropFiles,
100+
onPaginate: handlePaginate,
87101
onTaskRemove,
88102
onSelectFiles,
89103
onToggleOverwrite,

packages/react-storage/src/components/StorageBrowser/views/hooks/usePaginate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export const usePaginate = <T>({
5151

5252
if (isFunction(onPaginate)) onPaginate(page);
5353

54-
if (page > currentPage) visitedRef.current = page;
54+
if (page > currentPage && page > highestPageVisited)
55+
visitedRef.current = page;
5556

5657
setCurrentPage(page);
5758
},

0 commit comments

Comments
 (0)