Skip to content

Commit 7bd34e8

Browse files
committed
feat: Improve resume flow and decouple it
1 parent 1909cd9 commit 7bd34e8

2 files changed

Lines changed: 51 additions & 39 deletions

File tree

src/drive/web/modules/views/Drive/index.jsx

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ import { useQuery, useClient } from 'cozy-client'
99
import { useVaultClient } from 'cozy-keys-lib'
1010
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
1111
import useBreakpoints from 'cozy-ui/transpiled/react/hooks/useBreakpoints'
12-
import { useWebviewIntent } from 'cozy-intent'
13-
import {
14-
removeFileToUploadQueue,
15-
uploadFilesFromNative,
16-
purgeUploadQueue
17-
} from 'drive/web/modules/upload'
1812
import Dropzone from 'drive/web/modules/upload/Dropzone'
1913
import { ModalContext } from 'drive/lib/ModalContext'
2014
import useActions from 'drive/web/modules/actions/useActions'
@@ -56,6 +50,7 @@ import FabWithMenuContext from 'drive/web/modules/drive/FabWithMenuContext'
5650
import AddMenuProvider from 'drive/web/modules/drive/AddMenu/AddMenuProvider'
5751
import useHead from 'components/useHead'
5852
import { useSelectionContext } from 'drive/web/modules/selection/SelectionProvider'
53+
import { useResumeUploadFromFlagship } from 'drive/web/modules/views/Upload/useUploadFromFlagship'
5954

6055
const desktopExtraColumnsNames = ['carbonCopy', 'electronicSafe']
6156
const mobileExtraColumnsNames = []
@@ -67,7 +62,6 @@ const DriveView = () => {
6762
const currentFolderId = useCurrentFolderId() || ROOT_DIR_ID
6863
useHead()
6964
const { isSelectionBarVisible } = useSelectionContext()
70-
const webviewIntent = useWebviewIntent()
7165

7266
const { isMobile } = useBreakpoints()
7367
const { isFabDisplayed, setIsFabDisplayed } = useContext(FabContext)
@@ -179,37 +173,7 @@ const DriveView = () => {
179173
[t]
180174
)
181175

182-
useEffect(() => {
183-
if (!webviewIntent) return
184-
185-
webviewIntent
186-
.call('hasFilesToHandle')
187-
.then(({ filesToHandle, uploadedFiles, uploading }) => {
188-
if (!uploading) return false
189-
190-
actions.dispatch(
191-
uploadFilesFromNative(
192-
filesToHandle,
193-
displayedFolder?.id,
194-
undefined,
195-
{ client },
196-
() => Promise.resolve()
197-
)
198-
)
199-
200-
return uploadedFiles
201-
})
202-
.then(uploadedFiles => {
203-
if (!uploadedFiles) return false
204-
205-
return uploadedFiles.forEach(file => {
206-
return actions.dispatch(removeFileToUploadQueue(file))
207-
})
208-
})
209-
.catch(() => {
210-
return actions.dispatch(purgeUploadQueue())
211-
})
212-
}, [actions, client, displayedFolder?.id, webviewIntent])
176+
useResumeUploadFromFlagship()
213177

214178
useEffect(() => {
215179
if (canWriteToCurrentFolder) {

src/drive/web/modules/views/Upload/useUploadFromFlagship.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { useWebviewIntent } from 'cozy-intent'
55

66
import logger from 'lib/logger'
77
import { FileFromNative } from './UploadTypes'
8-
import CozyClient from 'cozy-client'
8+
import CozyClient, { useClient } from 'cozy-client'
9+
import { useDispatch } from 'react-redux'
10+
import {
11+
uploadFilesFromNative,
12+
removeFileToUploadQueue,
13+
purgeUploadQueue
14+
} from '../../upload'
915

1016
const typedLogger = logger as unknown & {
1117
info: (message: string, ...rest: unknown[]) => void
@@ -104,3 +110,45 @@ export const useUploadFromFlagship = (): UploadFromFlagship => {
104110
}
105111
}
106112
}
113+
114+
export const useResumeUploadFromFlagship = (): void => {
115+
const client = useClient()
116+
const dispatch = useDispatch()
117+
const webviewIntent = useWebviewIntent()
118+
useEffect(() => {
119+
const doResumeCheck = async (): Promise<void> => {
120+
try {
121+
const { filesToHandle, uploadedFiles, uploading } =
122+
(await webviewIntent?.call('hasFilesToHandle')) as {
123+
filesToHandle: FileFromNative[]
124+
uploadedFiles: FileFromNative[]
125+
uploading: boolean
126+
}
127+
128+
if (!uploading) return
129+
130+
dispatch(
131+
uploadFilesFromNative(
132+
filesToHandle.map(file => ({
133+
file: { ...file, name: file.fileName },
134+
isDirectory: false
135+
})),
136+
filesToHandle[0].dirId,
137+
undefined,
138+
{ client },
139+
() => Promise.resolve()
140+
)
141+
)
142+
143+
uploadedFiles.forEach(file => {
144+
dispatch(removeFileToUploadQueue({ ...file, name: file.fileName }))
145+
})
146+
} catch (error) {
147+
typedLogger.info('hasFilesToHandle error', { error })
148+
dispatch(purgeUploadQueue())
149+
}
150+
}
151+
152+
void doResumeCheck()
153+
}, [client, dispatch, webviewIntent])
154+
}

0 commit comments

Comments
 (0)