Skip to content

Commit

Permalink
Fix Preview loading if interrupted (#1738)
Browse files Browse the repository at this point in the history
* ensure no error is returned

* ensure download remains active

* remove debugger

* lint
  • Loading branch information
FSM1 committed Nov 16, 2021
1 parent 5b3a1e1 commit 118337d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ export const useGetFile = () => {
return content

} catch (error) {
setIsDownloading(false)

// If no error is thrown, this was due to a cancellation by the user.
if (error) {
console.error(error)
setError(t`There was an error getting the preview.`)
setIsDownloading(false)
}
}
}, [bucket, getFileContent])
Expand Down
26 changes: 13 additions & 13 deletions packages/files-ui/src/Contexts/FilesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@chainsafe/files-api-client"
import React, { useCallback, useEffect } from "react"
import { useState } from "react"
import { decryptFile, encryptFile } from "../Utils/encryption"
import { decryptFile, encryptFile } from "../Utils/encryption"
import { ToastParams, useToasts } from "@chainsafe/common-components"
import axios, { CancelToken } from "axios"
import { plural, t } from "@lingui/macro"
Expand Down Expand Up @@ -159,7 +159,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {

let encryptionKey = ""

switch(bucket.type) {
switch (bucket.type) {
case "csf":
case "trash": {
encryptionKey = personalEncryptionKey
Expand All @@ -168,7 +168,8 @@ const FilesProvider = ({ children }: FilesContextProps) => {
case "share": {
encryptionKey = await getKeyForSharedBucket(bucket)
break
}}
}
}

return encryptionKey
}, [getKeyForSharedBucket, personalEncryptionKey, userId])
Expand Down Expand Up @@ -481,7 +482,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
}
} catch (error) {
if (axios.isCancel(error)) {
return Promise.reject(error)
return Promise.reject()
} else {
console.error(error)
return Promise.reject(error)
Expand All @@ -493,7 +494,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
itemsToDownload: FileSystemItem[],
currentPath: string,
bucketId: string
): Promise<FileSystemItemPath[]> => {
): Promise<FileSystemItemPath[]> => {
return await itemsToDownload.reduce(
async (acc: Promise<FileSystemItemPath[]>, item: FileSystemItem): Promise<FileSystemItemPath[]> => {
if (item.isFolder) {
Expand Down Expand Up @@ -566,7 +567,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
}
})

if(file) {
if (file) {
const fileArrayBuffer = await file.arrayBuffer()
const fullPath = getPathWithFile(item.path, item.name)
const relativeFilePath = getRelativePath(fullPath, currentPath)
Expand Down Expand Up @@ -662,7 +663,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
return Promise.resolve()
} catch (error: any) {
console.error(error)
let errorMessage = `${t`An error occurred: `} ${typeof(error) === "string"
let errorMessage = `${t`An error occurred: `} ${typeof (error) === "string"
? error
: error?.error?.message || ""}`
if (axios.isCancel(error)) {
Expand All @@ -680,7 +681,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
}
}, [getFileContent, addToast, updateToast])

const createSharedFolder = useCallback(async (name: string, writerUsers?: SharedFolderUser[], readerUsers?: SharedFolderUser[]) => {
const createSharedFolder = useCallback(async (name: string, writerUsers?: SharedFolderUser[], readerUsers?: SharedFolderUser[]) => {
if (!publicKey) return

const bucketEncryptionKey = Buffer.from(
Expand Down Expand Up @@ -806,7 +807,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
}
})

if(file) {
if (file) {
await encryptAndUploadFiles(
destinationBucket,
[new File([file], item.name, { type: item.content_type })],
Expand Down Expand Up @@ -841,7 +842,7 @@ const FilesProvider = ({ children }: FilesContextProps) => {
? t`${successCount} files transferred successfully, ${totalFileNumber - successCount} failed`
: t`${inSharedBucket ? "Copying" : "Sharing"} failed`,
type: successCount ? "success" : "error",
progress: undefined,
progress: undefined,
isClosable: true
}, true)
setTransfersInProgress(false)
Expand All @@ -855,15 +856,14 @@ const FilesProvider = ({ children }: FilesContextProps) => {
: t`${inSharedBucket ? "Copying" : "Sharing"} failed`
if (axios.isCancel(error)) {
errorMessage = successCount
? t`${
inSharedBucket ? "Copying" : "Sharing"
? t`${inSharedBucket ? "Copying" : "Sharing"
} cancelled - ${successCount} files ${inSharedBucket ? "copied" : "shared"} successfully`
: t`${inSharedBucket ? "Copying" : "Sharing"} cancelled`
}
updateToast(toastId, {
title: errorMessage,
type: "error",
progress: undefined,
progress: undefined,
isClosable: true
}, true)
}
Expand Down

0 comments on commit 118337d

Please sign in to comment.