Skip to content

Commit a59f4d7

Browse files
authored
chore(StorageBrowser) : Generalize table resolver for default actions (#6566)
* update table resolvers * update to use createTableResolver * cleanup * address feedback * Revert "update to use createTableResolver" This reverts commit c699150. * cleanup * address feedback
1 parent f8d5bc1 commit a59f4d7

File tree

14 files changed

+155
-219
lines changed

14 files changed

+155
-219
lines changed

packages/react-storage/src/components/StorageBrowser/actions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type {
4545
LocationItemType,
4646
LocationPermissions,
4747
LocationType,
48+
OptionalFileData,
4849
TaskData,
4950
TaskHandler,
5051
TaskHandlerInput,

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ControlsContextProvider } from '../../../controls';
55
import { useDisplayText } from '../../../displayText';
66

77
import { useResolveTableData } from '../../hooks/useResolveTableData';
8-
import { COPY_TABLE_KEYS, COPY_TABLE_RESOLVERS } from '../../utils';
8+
import { FILE_DATA_ITEM_TABLE_KEYS, COPY_TABLE_RESOLVERS } from '../../utils';
99

1010
import { FoldersMessageProvider } from './FoldersMessageControl';
1111
import { FoldersPaginationProvider } from './FoldersPaginationControl';
@@ -65,10 +65,14 @@ export function CopyViewProvider({
6565
onSelectFolder,
6666
} = folders;
6767

68-
const tableData = useResolveTableData(COPY_TABLE_KEYS, COPY_TABLE_RESOLVERS, {
69-
items,
70-
props: { displayText, isProcessing, onTaskRemove },
71-
});
68+
const tableData = useResolveTableData(
69+
FILE_DATA_ITEM_TABLE_KEYS,
70+
COPY_TABLE_RESOLVERS,
71+
{
72+
items,
73+
props: { displayText, isProcessing, onTaskRemove },
74+
}
75+
);
7276

7377
const isActionStartDisabled =
7478
isProcessing || isProcessingComplete || !destination?.current;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ControlsContextProvider } from '../../../controls/context';
44
import { useDisplayText } from '../../../displayText';
55

66
import { useResolveTableData } from '../../hooks/useResolveTableData';
7-
import { DELETE_TABLE_KEYS, DELETE_TABLE_RESOLVERS } from '../../utils';
7+
import { FILE_DATA_ITEM_TABLE_KEYS, DELETE_TABLE_RESOLVERS } from '../../utils';
88

99
import type { DeleteViewProviderProps } from './types';
1010

@@ -42,7 +42,7 @@ export function DeleteViewProvider({
4242
: undefined;
4343

4444
const tableData = useResolveTableData(
45-
DELETE_TABLE_KEYS,
45+
FILE_DATA_ITEM_TABLE_KEYS,
4646
DELETE_TABLE_RESOLVERS,
4747
{
4848
items,

packages/react-storage/src/components/StorageBrowser/views/utils/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export {
2-
COPY_TABLE_KEYS,
32
COPY_TABLE_RESOLVERS,
4-
DELETE_TABLE_KEYS,
3+
FILE_DATA_ITEM_TABLE_KEYS,
54
DELETE_TABLE_RESOLVERS,
65
UPLOAD_TABLE_KEYS,
76
UPLOAD_TABLE_RESOLVERS,

packages/react-storage/src/components/StorageBrowser/views/utils/tableResolvers/__testUtils__/tasks.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@ import { MULTIPART_UPLOAD_THRESHOLD_BYTES } from '../../../../actions/handlers/c
22
import type { TaskStatus } from '../../../../tasks';
33

44
import type {
5+
FileDataTask,
56
CopyActionTask,
67
DeleteActionTask,
78
UploadActionTask,
89
} from '../types';
910

10-
type MockCopyOrDeleteTaskStatus = Exclude<TaskStatus, 'OVERWRITE_PREVENTED'>;
11+
type MockFileDataTaskStatus = Exclude<TaskStatus, 'OVERWRITE_PREVENTED'>;
1112

12-
type MockCopyTasks = {
13-
[K in MockCopyOrDeleteTaskStatus]: Omit<CopyActionTask, 'status'> & {
14-
status: K;
15-
};
16-
};
17-
18-
type MockDeleteTasks = {
19-
[K in MockCopyOrDeleteTaskStatus]: Omit<DeleteActionTask, 'status'> & {
13+
type MockFileDataTasks = {
14+
[K in MockFileDataTaskStatus]: Omit<FileDataTask, 'status'> & {
2015
status: K;
2116
};
2217
};
@@ -26,7 +21,7 @@ type MockUploadTasks = {
2621
};
2722

2823
const MOCK_PREFIX = 'test-folder/';
29-
const MOCK_COPY_OR_DELETE_TASK_BASE = {
24+
const MOCK_FILE_DATA_ITEM_BASE = {
3025
fileKey: 'file1.txt',
3126
lastModified: new Date(),
3227
size: 1000,
@@ -35,12 +30,12 @@ const MOCK_COPY_OR_DELETE_TASK_BASE = {
3530

3631
const MOCK_COPY_DESTINATION_PREFIX = 'some-destination-folder/';
3732
const MOCK_COPY_DATA: Omit<CopyActionTask['data'], 'id'> = {
38-
...MOCK_COPY_OR_DELETE_TASK_BASE,
33+
...MOCK_FILE_DATA_ITEM_BASE,
3934
key: `${MOCK_COPY_DESTINATION_PREFIX}${MOCK_PREFIX}file1.txt`,
4035
sourceKey: `${MOCK_PREFIX}file1.txt`,
4136
};
4237

43-
export const MOCK_COPY_TASKS: MockCopyTasks = {
38+
export const MOCK_COPY_TASKS: MockFileDataTasks = {
4439
CANCELED: {
4540
data: {
4641
...MOCK_COPY_DATA,
@@ -94,11 +89,11 @@ export const MOCK_COPY_TASKS: MockCopyTasks = {
9489
};
9590

9691
const MOCK_DELETE_DATA: Omit<DeleteActionTask['data'], 'id'> = {
97-
...MOCK_COPY_OR_DELETE_TASK_BASE,
92+
...MOCK_FILE_DATA_ITEM_BASE,
9893
key: `${MOCK_PREFIX}file1.txt`,
9994
};
10095

101-
export const MOCK_DELETE_TASKS: MockDeleteTasks = {
96+
export const MOCK_DELETE_TASKS: MockFileDataTasks = {
10297
CANCELED: {
10398
data: {
10499
...MOCK_DELETE_DATA,

packages/react-storage/src/components/StorageBrowser/views/utils/tableResolvers/__tests__/copyActionTask.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { capitalize } from '@aws-amplify/ui';
22
import { CopyViewDisplayText } from '../../../../displayText';
33

44
import { MOCK_COPY_TASKS } from '../__testUtils__/tasks';
5-
import { STATUS_ICONS, STATUS_LABELS } from '../constants';
5+
import {
6+
FILE_DATA_ITEM_TABLE_KEYS,
7+
STATUS_ICONS,
8+
STATUS_LABELS,
9+
} from '../constants';
610
import { CopyActionTask, CopyTableResolverProps } from '../types';
7-
import { COPY_TABLE_KEYS, COPY_TABLE_RESOLVERS } from '../copyResolvers';
11+
import { COPY_TABLE_RESOLVERS } from '../copyResolvers';
812

913
const mockDisplayText: CopyViewDisplayText = {
1014
tableColumnNameHeader: 'Name',
@@ -28,7 +32,7 @@ describe('COPY_TABLE_RESOLVERS', () => {
2832
beforeEach(jest.clearAllMocks);
2933

3034
describe('getCell', () => {
31-
it.each(COPY_TABLE_KEYS)(
35+
it.each(FILE_DATA_ITEM_TABLE_KEYS)(
3236
'returns the expect cell `key` for a "%s" table `key`',
3337
(key) => {
3438
const data = {
@@ -235,7 +239,7 @@ describe('COPY_TABLE_RESOLVERS', () => {
235239

236240
describe('getHeader', () => {
237241
// filter out cancel, does not allow sorting
238-
it.each(COPY_TABLE_KEYS.filter((key) => key !== 'cancel'))(
242+
it.each(FILE_DATA_ITEM_TABLE_KEYS.filter((key) => key !== 'cancel'))(
239243
'returns the expect header data for a %s column',
240244
(key) => {
241245
const data = { key, props: mockProps };

packages/react-storage/src/components/StorageBrowser/views/utils/tableResolvers/__tests__/deleteActionTask.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { capitalize } from '@aws-amplify/ui';
22
import { DeleteViewDisplayText } from '../../../../displayText';
33

44
import { MOCK_DELETE_TASKS } from '../__testUtils__/tasks';
5-
import { STATUS_ICONS, STATUS_LABELS } from '../constants';
5+
import {
6+
FILE_DATA_ITEM_TABLE_KEYS,
7+
STATUS_ICONS,
8+
STATUS_LABELS,
9+
} from '../constants';
610
import { DeleteActionTask, DeleteTableResolverProps } from '../types';
7-
import { DELETE_TABLE_KEYS, DELETE_TABLE_RESOLVERS } from '../deleteResolvers';
11+
import { DELETE_TABLE_RESOLVERS } from '../deleteResolvers';
812

913
const mockDisplayText: DeleteViewDisplayText = {
1014
tableColumnNameHeader: 'Name',
@@ -28,7 +32,7 @@ describe('DELETE_TABLE_RESOLVERS', () => {
2832
beforeEach(jest.clearAllMocks);
2933

3034
describe('getCell', () => {
31-
it.each(DELETE_TABLE_KEYS)(
35+
it.each(FILE_DATA_ITEM_TABLE_KEYS)(
3236
'returns the expect cell `key` for a "%s" table `key`',
3337
(key) => {
3438
const data = {
@@ -235,7 +239,7 @@ describe('DELETE_TABLE_RESOLVERS', () => {
235239

236240
describe('getHeader', () => {
237241
// filter out cancel, does not allow sorting
238-
it.each(DELETE_TABLE_KEYS.filter((key) => key !== 'cancel'))(
242+
it.each(FILE_DATA_ITEM_TABLE_KEYS.filter((key) => key !== 'cancel'))(
239243
'returns the expect header data for a %s column',
240244
(key) => {
241245
const data = { key, props: mockProps };

packages/react-storage/src/components/StorageBrowser/views/utils/tableResolvers/__tests__/utils.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
MOCK_UPLOAD_TASKS_SINGLE_PART,
55
} from '../__testUtils__/tasks';
66
import {
7-
getCopyOrDeleteCancelCellContent,
7+
getFileDataCancelCellContent,
88
getFileSize,
99
getFileType,
1010
getUploadCellFolder,
@@ -23,7 +23,7 @@ describe('table resolver utils', () => {
2323

2424
it('returns the expected values for a QUEUED task prior to processing', () => {
2525
const item = tasks.QUEUED;
26-
const output = getCopyOrDeleteCancelCellContent({
26+
const output = getFileDataCancelCellContent({
2727
item,
2828
key: 'cancel',
2929
props: { ...props, isProcessing: false },
@@ -39,7 +39,7 @@ describe('table resolver utils', () => {
3939

4040
it('returns the expected values for a QUEUED task during processing', () => {
4141
const item = tasks.QUEUED;
42-
const output = getCopyOrDeleteCancelCellContent({
42+
const output = getFileDataCancelCellContent({
4343
item,
4444
key: 'cancel',
4545
props: { ...props, isProcessing: true },
@@ -55,7 +55,7 @@ describe('table resolver utils', () => {
5555

5656
it('returns the expected values for a PENDING task', () => {
5757
const item = tasks.PENDING;
58-
const output = getCopyOrDeleteCancelCellContent({
58+
const output = getFileDataCancelCellContent({
5959
item,
6060
key: 'cancel',
6161
props: { ...props, isProcessing: true },
@@ -71,7 +71,7 @@ describe('table resolver utils', () => {
7171

7272
it('returns the expected values for a CANCELED task', () => {
7373
const item = tasks.CANCELED;
74-
const output = getCopyOrDeleteCancelCellContent({
74+
const output = getFileDataCancelCellContent({
7575
item,
7676
key: 'cancel',
7777
props: { ...props, isProcessing: true },
@@ -87,7 +87,7 @@ describe('table resolver utils', () => {
8787

8888
it('returns the expected values for a COMPLETE task', () => {
8989
const item = tasks.COMPLETE;
90-
const output = getCopyOrDeleteCancelCellContent({
90+
const output = getFileDataCancelCellContent({
9191
item,
9292
key: 'cancel',
9393
props: { ...props, isProcessing: true },
@@ -103,7 +103,7 @@ describe('table resolver utils', () => {
103103

104104
it('calls onTaskRemove with the expected values for a removable task', () => {
105105
const item = tasks.QUEUED;
106-
const output = getCopyOrDeleteCancelCellContent({
106+
const output = getFileDataCancelCellContent({
107107
item,
108108
key: 'cancel',
109109
props: { ...props, isProcessing: false },
@@ -131,7 +131,7 @@ describe('table resolver utils', () => {
131131

132132
it('calls onTaskRemove with the expected values for a cancelable task', () => {
133133
const item = tasks.QUEUED;
134-
const output = getCopyOrDeleteCancelCellContent({
134+
const output = getFileDataCancelCellContent({
135135
item,
136136
key: 'cancel',
137137
props: { ...props, isProcessing: true },

packages/react-storage/src/components/StorageBrowser/views/utils/tableResolvers/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ export const STATUS_ICONS = {
1515
CANCELED: 'action-canceled',
1616
QUEUED: 'action-queued',
1717
} as const;
18+
19+
export const FILE_DATA_ITEM_TABLE_KEYS = [
20+
'name',
21+
'folder',
22+
'type',
23+
'size',
24+
'status',
25+
'cancel',
26+
] as const;

packages/react-storage/src/components/StorageBrowser/views/utils/tableResolvers/copyResolvers.ts

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,13 @@
11
import { capitalize, noop } from '@aws-amplify/ui';
22

3-
import type { WithKey } from '../../../components/types';
43
import { isCopyViewDisplayTextKey } from '../../../displayText';
54

6-
import { STATUS_ICONS, STATUS_LABELS } from './constants';
7-
import type {
8-
CopyActionTask,
9-
CopyTableKey,
10-
CopyTaskTableResolvers,
11-
} from './types';
12-
import {
13-
getCopyCellFolder,
14-
getCopyOrDeleteCancelCellContent,
15-
getFileSize,
16-
getFileType,
17-
} from './utils';
5+
import { STATUS_LABELS } from './constants';
6+
import type { FileDataTaskTableResolvers, GetFileDataCell } from './types';
7+
import { cancel, folder, getFileDataCellKey, name, size, type } from './utils';
188

19-
export const COPY_TABLE_KEYS = [
20-
'name',
21-
'folder',
22-
'type',
23-
'size',
24-
'status',
25-
'cancel',
26-
] as const;
27-
28-
type GetCopyTaskCell = CopyTaskTableResolvers['getCell'];
29-
30-
const getCopyCellKey = ({
31-
key,
32-
item,
33-
}: WithKey<{ item: CopyActionTask }, CopyTableKey>) => `${key}-${item.data.id}`;
34-
35-
const name: GetCopyTaskCell = (data) => {
36-
const key = getCopyCellKey(data);
37-
38-
const { item } = data;
39-
const text = item.data.fileKey;
40-
const icon = STATUS_ICONS[item.status];
41-
42-
return { key, type: 'text', content: { icon, text } };
43-
};
44-
45-
const folder: GetCopyTaskCell = (data) => {
46-
const key = getCopyCellKey(data);
47-
const text = getCopyCellFolder(data.item);
48-
49-
return { key, type: 'text', content: { text } };
50-
};
51-
52-
const type: GetCopyTaskCell = (data) => {
53-
const key = getCopyCellKey(data);
54-
const { fileKey } = data.item.data;
55-
56-
const text = getFileType(fileKey);
57-
58-
return { key, type: 'text', content: { text } };
59-
};
60-
61-
const size: GetCopyTaskCell = (data) => {
62-
const key = getCopyCellKey(data);
63-
const { size: value } = data.item.data;
64-
const displayValue = getFileSize(value);
65-
66-
return { key, type: 'number', content: { value, displayValue } };
67-
};
68-
69-
const status: GetCopyTaskCell = (data) => {
70-
const key = getCopyCellKey(data);
9+
const status: GetFileDataCell = (data) => {
10+
const key = getFileDataCellKey(data);
7111
const {
7212
item: { status },
7313
props: { displayText },
@@ -82,13 +22,6 @@ const status: GetCopyTaskCell = (data) => {
8222
return { key, type: 'text', content: { text } };
8323
};
8424

85-
const cancel: GetCopyTaskCell = (data) => {
86-
const key = getCopyCellKey(data);
87-
const content = getCopyOrDeleteCancelCellContent(data);
88-
89-
return { key, type: 'button', content };
90-
};
91-
9225
const COPY_CELL_RESOLVERS = {
9326
name,
9427
folder,
@@ -104,10 +37,10 @@ const COPY_CELL_RESOLVERS = {
10437
* keep TS happy as "progress" headers were included in display text interfaces
10538
* and cannot be removed from the tables without a breaking change
10639
*/
107-
progress: noop as GetCopyTaskCell,
40+
progress: noop as GetFileDataCell,
10841
};
10942

110-
export const COPY_TABLE_RESOLVERS: CopyTaskTableResolvers = {
43+
export const COPY_TABLE_RESOLVERS: FileDataTaskTableResolvers = {
11144
getCell: (data) => COPY_CELL_RESOLVERS[data.key](data),
11245
getHeader: ({ key, props: { displayText } }) => {
11346
const text = displayText[`tableColumn${capitalize(key)}Header`];

0 commit comments

Comments
 (0)