This repository was archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Add file transfer to Connect #1225
Merged
Merged
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ea2bfd7
Refactor `FileTransfer`
gzdunek 672f18b
Do not show hover on disabled `ButtonIcon`
gzdunek af3d61a
Use new `FileTransfer` in Web UI
gzdunek 4c60cae
Use new `FileTransfer` in Connect
gzdunek 09c9298
Update protobuf files
gzdunek ee465da
Remove old `FileTransfer`
gzdunek c3cde59
Replace `extractFilesFromFileList` with `Array.from`
gzdunek c5b348b
Fix `files` deps
gzdunek aa8afe8
Merge branch 'master' into gzdunek/refactor-file-transfer
gzdunek 6b491cd
Use `div` for `Dropzone` element
gzdunek daceb3d
Simplify condition
gzdunek 160881e
Remove unnecessary deps, use `unique` for id
gzdunek e3b4d7c
Change test names
gzdunek f4cb53b
Use `waitForElementToBeRemoved`
gzdunek d1a9ad4
Add comment to `TransferHandlers`, update tests names
gzdunek 9d6a06c
Revert "use `unique` for id"
gzdunek 3a358bd
Change `ButtonIcon` disabled state styles
gzdunek 40b5beb
Add `retryWithRelogin` download/upload
gzdunek 89e6e1f
Merge branch 'master' into gzdunek/refactor-file-transfer
gzdunek ee91444
Send `hostname` instead of `serverUri`
gzdunek 4c0fca9
Update protobuf files
gzdunek c136931
Return `FileTransferListeners` from handlers instead of passing them …
gzdunek 66e58aa
Get rid of `FileTransferRequest.AsObject`
gzdunek c7449fd
Rename `FileTransferClient` to `FileTransferService`
gzdunek 106d5cc
Merge branch 'master' into gzdunek/refactor-file-transfer
gzdunek a9cba6f
Remove `inputCss`
gzdunek 884c4a6
Update snapshots with fixed `ButtonIcon` hover and focus styles
gzdunek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
packages/shared/components/FileTransfer/FileTransfer.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| /** | ||
| * Copyright 2022 Gravitational, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { act, fireEvent, render, screen, waitFor } from 'design/utils/testing'; | ||
|
|
||
| import { FileTransfer, TransferHandlers } from './FileTransfer'; | ||
| import { FileTransferContextProvider } from './FileTransferContextProvider'; | ||
| import { | ||
| FileTransferDialogDirection, | ||
| FileTransferListeners, | ||
| } from './FileTransferStateless'; | ||
|
|
||
| test('click opens correct dialog', () => { | ||
| render( | ||
| <FileTransferContextProvider | ||
| openedDialog={FileTransferDialogDirection.Download} | ||
| > | ||
| <FileTransfer beforeClose={undefined} transferHandlers={undefined} /> | ||
| </FileTransferContextProvider> | ||
| ); | ||
| expect(screen.getByText('Download Files')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test('downloads component changes when file transfer callbacks are called', async () => { | ||
| let listenersMock: FileTransferListeners = { | ||
| onComplete(): void {}, | ||
| onError(): void {}, | ||
| onProgress(): void {}, | ||
| }; | ||
|
|
||
| const handler: TransferHandlers = { | ||
| getDownloader: async () => fileTransferListeners => { | ||
| listenersMock = fileTransferListeners; | ||
| }, | ||
| getUploader: async () => undefined, | ||
| }; | ||
| render( | ||
| <FileTransferContextProvider | ||
| openedDialog={FileTransferDialogDirection.Download} | ||
| > | ||
| <FileTransfer beforeClose={undefined} transferHandlers={handler} /> | ||
| </FileTransferContextProvider> | ||
| ); | ||
| fireEvent.change(screen.getByLabelText('File Path'), { | ||
| target: { value: '/Users/g/file.txt' }, | ||
| }); | ||
| fireEvent.click(screen.getByText('Download')); | ||
| const listItem = await screen.findByRole('listitem'); | ||
| expect(listItem).toHaveTextContent('/Users/g/file.txt'); | ||
|
|
||
| act(() => listenersMock.onProgress(50)); | ||
| expect(listItem).toHaveTextContent('50%'); | ||
|
|
||
| act(() => listenersMock.onComplete()); | ||
| expect(listItem).toContainElement(screen.getByTitle('Transfer completed')); | ||
|
|
||
| act(() => listenersMock.onError(new Error('Network error'))); | ||
| expect(listItem).toHaveTextContent('Network error'); | ||
| }); | ||
|
|
||
| test('onAbort is called when user cancels upload', async () => { | ||
| let abortControllerMock: AbortController; | ||
|
|
||
| const handler: TransferHandlers = { | ||
| getDownloader: async () => (fileTransferListeners, abortController) => { | ||
| abortControllerMock = abortController; | ||
| }, | ||
| getUploader: async () => undefined, | ||
| }; | ||
| render( | ||
| <FileTransferContextProvider | ||
| openedDialog={FileTransferDialogDirection.Download} | ||
| > | ||
| <FileTransfer beforeClose={undefined} transferHandlers={handler} /> | ||
| </FileTransferContextProvider> | ||
| ); | ||
| fireEvent.change(screen.getByLabelText('File Path'), { | ||
| target: { value: '/Users/g/file.txt' }, | ||
| }); | ||
| fireEvent.click(screen.getByText('Download')); | ||
| fireEvent.click(await screen.findByTitle('Cancel')); | ||
| expect(abortControllerMock.signal.aborted).toBeTruthy(); | ||
| }); | ||
|
|
||
| test('file is not added when transferHandler does not return anything', async () => { | ||
| const handler: TransferHandlers = { | ||
| getDownloader: async () => undefined, | ||
| getUploader: async () => undefined, | ||
| }; | ||
| const filePath = '/Users/g/file.txt'; | ||
|
|
||
| render( | ||
| <FileTransferContextProvider | ||
| openedDialog={FileTransferDialogDirection.Download} | ||
| > | ||
| <FileTransfer beforeClose={undefined} transferHandlers={handler} /> | ||
| </FileTransferContextProvider> | ||
| ); | ||
| fireEvent.change(screen.getByLabelText('File Path'), { | ||
| target: { value: filePath }, | ||
| }); | ||
| fireEvent.click(screen.getByText('Download')); | ||
| expect(screen.queryByText('/Users/g/file.txt')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| describe('handleAfterClose', () => { | ||
| const getSetup = async () => { | ||
| const handleBeforeClose = jest.fn(); | ||
| const handleAfterClose = jest.fn(); | ||
| const handler: TransferHandlers = { | ||
| getDownloader: async () => () => {}, | ||
| getUploader: async () => undefined, | ||
| }; | ||
|
|
||
| render( | ||
| <FileTransferContextProvider | ||
| openedDialog={FileTransferDialogDirection.Download} | ||
| > | ||
| <FileTransfer | ||
| beforeClose={handleBeforeClose} | ||
| afterClose={handleAfterClose} | ||
| transferHandlers={handler} | ||
| /> | ||
| </FileTransferContextProvider> | ||
| ); | ||
|
|
||
| fireEvent.change(screen.getByLabelText('File Path'), { | ||
| target: { value: '~/abc' }, | ||
| }); | ||
|
|
||
| fireEvent.click(screen.getByText('Download')); | ||
| await screen.findByRole('listitem'); | ||
|
|
||
| return { handleBeforeClose, handleAfterClose }; | ||
| }; | ||
|
|
||
| test('is not called when closing the dialog has been aborted (by returning false from handleBeforeClose)', async () => { | ||
| const { handleBeforeClose, handleAfterClose } = await getSetup(); | ||
| handleBeforeClose.mockReturnValue(Promise.resolve(false)); | ||
| fireEvent.click(screen.getByTitle('Close')); | ||
| expect(handleBeforeClose).toHaveBeenCalled(); | ||
| expect(handleAfterClose).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('is called when closing the dialog has been confirmed (by returning true from handleBeforeClose)', async () => { | ||
| const { handleBeforeClose, handleAfterClose } = await getSetup(); | ||
| handleBeforeClose.mockReturnValue(Promise.resolve(true)); | ||
| fireEvent.click(screen.getByTitle('Close')); | ||
| expect(handleBeforeClose).toHaveBeenCalled(); | ||
|
|
||
| // wait for dialog to close | ||
| await waitFor(() => { | ||
| expect( | ||
| screen.queryByTestId('file-transfer-container') | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
|
JanKaczmarkiewicz marked this conversation as resolved.
Outdated
|
||
| expect(handleAfterClose).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
136 changes: 136 additions & 0 deletions
136
packages/shared/components/FileTransfer/FileTransfer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /** | ||
| * Copyright 2022 Gravitational, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import { useFileTransferContext } from './FileTransferContextProvider'; | ||
| import { useFilesStore } from './useFilesStore'; | ||
| import { | ||
| FileTransferDialogDirection, | ||
| FileTransferStateless, | ||
| RunFileTransfer, | ||
| } from './FileTransferStateless'; | ||
|
|
||
| interface FileTransferProps { | ||
| backgroundColor?: string; | ||
| transferHandlers: TransferHandlers; | ||
|
|
||
| /** | ||
| * `beforeClose` is called when an attempt to close the dialog was made | ||
| * and there is a file transfer in progress. | ||
| * Returning `true` will close the dialog, returning `false` will not. | ||
| */ | ||
| beforeClose?(): Promise<boolean> | boolean; | ||
|
|
||
| afterClose?(): void; | ||
| } | ||
|
|
||
| export interface TransferHandlers { | ||
| getDownloader: (sourcePath: string) => Promise<RunFileTransfer | undefined>; | ||
| getUploader: ( | ||
| destinationPath: string, | ||
| file: File | ||
| ) => Promise<RunFileTransfer | undefined>; | ||
| } | ||
|
|
||
| export function FileTransfer(props: FileTransferProps) { | ||
| const { openedDialog, closeDialog } = useFileTransferContext(); | ||
|
|
||
| async function handleCloseDialog( | ||
| isAnyTransferInProgress: boolean | ||
| ): Promise<void> { | ||
| const runCloseCallbacks = () => { | ||
| closeDialog(); | ||
| props.afterClose?.(); | ||
| }; | ||
|
|
||
| if (!isAnyTransferInProgress || !props.beforeClose) { | ||
| runCloseCallbacks(); | ||
| return; | ||
| } | ||
|
|
||
| if (await props.beforeClose()) { | ||
| runCloseCallbacks(); | ||
| } | ||
| } | ||
|
|
||
| if (!openedDialog) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <FileTransferDialog | ||
| openedDialog={openedDialog} | ||
| backgroundColor={props.backgroundColor} | ||
| transferHandlers={props.transferHandlers} | ||
| onCloseDialog={handleCloseDialog} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export function FileTransferDialog( | ||
| props: Pick<FileTransferProps, 'transferHandlers' | 'backgroundColor'> & { | ||
| openedDialog: FileTransferDialogDirection; | ||
| onCloseDialog(isAnyTransferInProgress: boolean): void; | ||
| } | ||
| ) { | ||
| const filesStore = useFilesStore(); | ||
|
|
||
| async function handleAddDownload(sourcePath: string): Promise<void> { | ||
| const runFileTransfer = await props.transferHandlers.getDownloader( | ||
| sourcePath | ||
| ); | ||
| if (runFileTransfer) { | ||
| filesStore.add({ | ||
| name: sourcePath, | ||
| runFileTransfer, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async function handleAddUpload( | ||
| destinationPath: string, | ||
| file: File | ||
| ): Promise<void> { | ||
| const runFileTransfer = await props.transferHandlers.getUploader( | ||
| destinationPath, | ||
| file | ||
| ); | ||
| if (runFileTransfer) { | ||
| filesStore.add({ | ||
| name: file.name, | ||
| runFileTransfer, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| function handleClose(): void { | ||
| props.onCloseDialog(filesStore.isAnyTransferInProgress()); | ||
| } | ||
|
|
||
| return ( | ||
| <FileTransferStateless | ||
| openedDialog={props.openedDialog} | ||
| files={filesStore.files} | ||
| onStart={filesStore.start} | ||
| onCancel={filesStore.cancel} | ||
| backgroundColor={props.backgroundColor} | ||
| onClose={handleClose} | ||
| onAddUpload={handleAddUpload} | ||
| onAddDownload={handleAddDownload} | ||
| /> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.