-
Notifications
You must be signed in to change notification settings - Fork 131
feat: Handle file upload from Flagship #2969
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
Merged
acezard
merged 17 commits into
master
from
refactor--updated-upload-component-to-typescript
Oct 23, 2023
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
27bb930
feat: Update manifest to add acceptFromFlagship
acezard d01f57c
refactor: updated upload component to typescript
acezard 63717e4
feat: stabilize with post-me
acezard 67a82ec
feat: add native upload resume
acezard bebe91a
fix: Update jest setup and handle undefined dir
acezard 84d330d
feat: Improve resume flow and decouple it
acezard a17bcf2
feat: disable unstable feature
acezard 19f851d
fix: handle empty filesToUpload as error
acezard 4e5adda
fix: make post-me update file status directly
acezard 94427c6
feat: activate resume upload
acezard 3038ca5
fix: rename method to getFilesToHandle
acezard ce5928b
feat: change max file size to 100MB
acezard 96ce541
refactor: Improve code clarity
acezard f7eff39
refactor: Move getErrorMessage in helpers
acezard c42e0db
fix: replace reset files with resetupload
acezard d92c61c
refactor: Rename uploadFiles to uploadFile
acezard fc7b072
feat: Update max file size for upload
acezard 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,5 +20,8 @@ | |
| "react/display-name": "off" | ||
| } | ||
| } | ||
| ] | ||
| ], | ||
| "parserOptions": { | ||
| "project": "tsconfig.json" | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| module.exports = { | ||
| presets: ['cozy-app', '@babel/env'] | ||
| presets: ['cozy-app'] | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| declare module 'cozy-ui/*' | ||
|
|
||
| declare module 'cozy-ui/transpiled/react' { | ||
| export const Alerter: { | ||
| error: (message: string) => void | ||
| } | ||
|
|
||
| export const logger: { | ||
| info: (message: string, ...rest: unknown[]) => void | ||
| } | ||
| } | ||
|
|
||
| declare module 'cozy-ui/transpiled/react/providers/I18n' { | ||
| export const useI18n: () => { | ||
| t: (key: string, options?: Record<string, unknown>) => string | ||
| } | ||
| } | ||
|
|
||
| declare module 'cozy-ui/transpiled/react/deprecated/Alerter' { | ||
| const Alerter: { | ||
| error: (message: string) => void | ||
| } | ||
|
|
||
| export default Alerter | ||
| } |
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 was deleted.
Oops, something went wrong.
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,40 @@ | ||
| import { makeStyles } from 'cozy-ui/transpiled/react/styles' | ||
|
|
||
| /* eslint-disable */ | ||
| export const useFabStyles = makeStyles(() => ({ | ||
| root: { | ||
| position: 'fixed', | ||
| right: ({ right = '1rem' }) => right, | ||
| bottom: ({ bottom = '1rem' }) => bottom | ||
| } | ||
| })) | ||
| /* eslint-enable */ | ||
|
|
||
| interface ErrorWithMessage { | ||
| message: string | ||
| } | ||
|
|
||
| const isErrorWithMessage = (error: unknown): error is ErrorWithMessage => { | ||
| return ( | ||
| typeof error === 'object' && | ||
| error !== null && | ||
| 'message' in error && | ||
| typeof (error as Record<string, unknown>).message === 'string' | ||
| ) | ||
| } | ||
|
|
||
| const toErrorWithMessage = (maybeError: unknown): ErrorWithMessage => { | ||
| if (isErrorWithMessage(maybeError)) return maybeError | ||
|
|
||
| try { | ||
| return new Error(JSON.stringify(maybeError)) | ||
| } catch { | ||
| // fallback in case there's an error stringifying the maybeError | ||
| // like with circular references for example. | ||
| return new Error(String(maybeError)) | ||
| } | ||
| } | ||
|
|
||
| export const getErrorMessage = (error: unknown): string => { | ||
| return toErrorWithMessage(error).message | ||
| } |
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
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,44 @@ | ||
| import { UseQueryReturnValue } from 'cozy-client/types/types' | ||
|
|
||
| export interface Folder { | ||
| _id: string | ||
| } | ||
|
|
||
| export interface FileForQueue { | ||
| name: string | ||
| file?: { name: string } | ||
| isDirectory?: false | ||
| } | ||
|
|
||
| export interface FileFromNative { | ||
| name: string | ||
| file: { | ||
| weblink: null | ||
| text: null | ||
| filePath: string | ||
| contentUri: string | ||
| subject: null | ||
| extension: string | ||
| fileName: string | ||
| mimeType: string | ||
| dirId?: string | ||
| conflictStrategy?: string | ||
| } | ||
| status: number | ||
| } | ||
|
|
||
| export interface UploadFromFlagship { | ||
| items?: FileFromNative['file'][] | ||
| uploadFilesFromFlagship: (fileOptions: { | ||
| name: string | ||
| dirId: string | ||
| conflictStrategy: string | ||
| }) => Promise<void> | ||
| resetFilesToHandle: () => Promise<void> | ||
| onClose: () => Promise<void> | ||
| uploadInProgress: boolean | ||
| contentQuery: UseQueryReturnValue | ||
| folderQuery: UseQueryReturnValue | ||
| setFolder: React.Dispatch<React.SetStateAction<Folder>> | ||
| folder: Folder | ||
| } |
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,86 @@ | ||
| import { WebviewService } from 'cozy-intent' | ||
| import logger from 'cozy-logger' | ||
|
|
||
| import { | ||
| RECEIVE_UPLOAD_ERROR, | ||
| RECEIVE_UPLOAD_SUCCESS | ||
| } from 'drive/web/modules/upload' | ||
| import type { | ||
| FileFromNative, | ||
| FileForQueue | ||
| } from 'drive/web/modules/views/Upload/UploadTypes' | ||
|
|
||
| export const generateForQueue = ( | ||
| files: FileFromNative['file'][] | ||
| ): FileForQueue[] => { | ||
| // @ts-expect-error fix types | ||
| return files.map(file => ({ file: file, isDirectory: false })) | ||
| } | ||
|
|
||
| export const onFileUploaded = ( | ||
| data: { | ||
| file: FileFromNative | ||
| isSuccess: boolean | ||
| }, | ||
| dispatch: (arg0: { type: string; file: FileFromNative }) => void | ||
| ): void => { | ||
| if (!data.file) return | ||
|
|
||
| // Status 2 means file status is "uploaded", any other status should be considered as an error, | ||
| // though it is not intended to receive something else than "uploaded" (2) or "error" (3) | ||
| // See OsReceiveFileStatus enum in cozy-flagship | ||
| if (data.file.status === 2) { | ||
| dispatch({ type: RECEIVE_UPLOAD_SUCCESS, file: data.file }) | ||
| } else { | ||
| dispatch({ type: RECEIVE_UPLOAD_ERROR, file: data.file }) | ||
| } | ||
| } | ||
|
|
||
| export const shouldRender = ( | ||
| items?: FileFromNative['file'][] | ||
| ): items is FileFromNative['file'][] => !!items && items.length > 0 | ||
|
|
||
| export const getFilesToHandle = async ( | ||
| webviewIntent: WebviewService | ||
| ): Promise<Array<FileFromNative['file'] & { name: string }>> => { | ||
| logger('info', 'getFilesToHandle called') | ||
|
|
||
| const files = (await webviewIntent?.call( | ||
| 'getFilesToHandle' | ||
| )) as unknown as FileFromNative[] | ||
|
|
||
| if (files?.length === 0) throw new Error('No files to upload') | ||
|
|
||
| if (files.length > 0) { | ||
| logger('info', 'getFilesToHandle success') | ||
|
|
||
| return files.map(fileFromNative => ({ | ||
| ...fileFromNative.file, | ||
| name: fileFromNative.file.fileName | ||
| })) | ||
| } else { | ||
| logger('info', 'getFilesToHandle no files to upload') | ||
| throw new Error('No files to upload') | ||
| } | ||
| } | ||
|
|
||
| export const sendFilesToHandle = async ( | ||
| filesForQueue: FileForQueue[], | ||
| webviewIntent: WebviewService, | ||
| folder: { _id: string } | ||
| ): Promise<void> => { | ||
| for (const file of filesForQueue) { | ||
| if (!file.file) throw new Error('No file to upload') | ||
|
|
||
| const fileOptions = { | ||
| name: file.file.name, | ||
| dirId: folder._id | ||
| } | ||
|
|
||
| logger('info', 'uploadFilesFromFlagship called') | ||
|
|
||
| await webviewIntent?.call('uploadFile', JSON.stringify({ fileOptions })) | ||
|
|
||
| logger('info', 'uploadFilesFromFlagship success') | ||
| } | ||
| } |
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.