Skip to content

Commit

Permalink
feat: Improve resume flow and decouple it
Browse files Browse the repository at this point in the history
  • Loading branch information
acezard committed Aug 31, 2023
1 parent 1909cd9 commit 7bd34e8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
40 changes: 2 additions & 38 deletions src/drive/web/modules/views/Drive/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import { useQuery, useClient } from 'cozy-client'
import { useVaultClient } from 'cozy-keys-lib'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
import useBreakpoints from 'cozy-ui/transpiled/react/hooks/useBreakpoints'
import { useWebviewIntent } from 'cozy-intent'
import {
removeFileToUploadQueue,
uploadFilesFromNative,
purgeUploadQueue
} from 'drive/web/modules/upload'
import Dropzone from 'drive/web/modules/upload/Dropzone'
import { ModalContext } from 'drive/lib/ModalContext'
import useActions from 'drive/web/modules/actions/useActions'
Expand Down Expand Up @@ -56,6 +50,7 @@ import FabWithMenuContext from 'drive/web/modules/drive/FabWithMenuContext'
import AddMenuProvider from 'drive/web/modules/drive/AddMenu/AddMenuProvider'
import useHead from 'components/useHead'
import { useSelectionContext } from 'drive/web/modules/selection/SelectionProvider'
import { useResumeUploadFromFlagship } from 'drive/web/modules/views/Upload/useUploadFromFlagship'

const desktopExtraColumnsNames = ['carbonCopy', 'electronicSafe']
const mobileExtraColumnsNames = []
Expand All @@ -67,7 +62,6 @@ const DriveView = () => {
const currentFolderId = useCurrentFolderId() || ROOT_DIR_ID
useHead()
const { isSelectionBarVisible } = useSelectionContext()
const webviewIntent = useWebviewIntent()

const { isMobile } = useBreakpoints()
const { isFabDisplayed, setIsFabDisplayed } = useContext(FabContext)
Expand Down Expand Up @@ -179,37 +173,7 @@ const DriveView = () => {
[t]
)

useEffect(() => {
if (!webviewIntent) return

webviewIntent
.call('hasFilesToHandle')
.then(({ filesToHandle, uploadedFiles, uploading }) => {
if (!uploading) return false

actions.dispatch(
uploadFilesFromNative(
filesToHandle,
displayedFolder?.id,
undefined,
{ client },
() => Promise.resolve()
)
)

return uploadedFiles
})
.then(uploadedFiles => {
if (!uploadedFiles) return false

return uploadedFiles.forEach(file => {
return actions.dispatch(removeFileToUploadQueue(file))
})
})
.catch(() => {
return actions.dispatch(purgeUploadQueue())
})
}, [actions, client, displayedFolder?.id, webviewIntent])
useResumeUploadFromFlagship()

useEffect(() => {
if (canWriteToCurrentFolder) {
Expand Down
50 changes: 49 additions & 1 deletion src/drive/web/modules/views/Upload/useUploadFromFlagship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { useWebviewIntent } from 'cozy-intent'

import logger from 'lib/logger'
import { FileFromNative } from './UploadTypes'
import CozyClient from 'cozy-client'
import CozyClient, { useClient } from 'cozy-client'
import { useDispatch } from 'react-redux'
import {
uploadFilesFromNative,
removeFileToUploadQueue,
purgeUploadQueue
} from '../../upload'

const typedLogger = logger as unknown & {
info: (message: string, ...rest: unknown[]) => void
Expand Down Expand Up @@ -104,3 +110,45 @@ export const useUploadFromFlagship = (): UploadFromFlagship => {
}
}
}

export const useResumeUploadFromFlagship = (): void => {
const client = useClient()
const dispatch = useDispatch()
const webviewIntent = useWebviewIntent()
useEffect(() => {
const doResumeCheck = async (): Promise<void> => {
try {
const { filesToHandle, uploadedFiles, uploading } =
(await webviewIntent?.call('hasFilesToHandle')) as {
filesToHandle: FileFromNative[]
uploadedFiles: FileFromNative[]
uploading: boolean
}

if (!uploading) return

dispatch(
uploadFilesFromNative(
filesToHandle.map(file => ({
file: { ...file, name: file.fileName },
isDirectory: false
})),
filesToHandle[0].dirId,
undefined,
{ client },
() => Promise.resolve()
)
)

uploadedFiles.forEach(file => {
dispatch(removeFileToUploadQueue({ ...file, name: file.fileName }))
})
} catch (error) {
typedLogger.info('hasFilesToHandle error', { error })
dispatch(purgeUploadQueue())
}
}

void doResumeCheck()
}, [client, dispatch, webviewIntent])
}

0 comments on commit 7bd34e8

Please sign in to comment.