Skip to content

Commit

Permalink
feat: show download progress in file preview modal (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara authored May 5, 2022
1 parent bb4fd24 commit 2c807e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
21 changes: 18 additions & 3 deletions app/assets/javascripts/Components/Files/FilePreviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WebApplication } from '@/UIModels/Application'
import { concatenateUint8Arrays } from '@/Utils/ConcatenateUint8Arrays'
import { DialogContent, DialogOverlay } from '@reach/dialog'
import { SNFile } from '@standardnotes/snjs'
import { NoPreviewIllustration } from '@standardnotes/stylekit'
import { addToast, NoPreviewIllustration, ToastType } from '@standardnotes/stylekit'
import { FunctionComponent } from 'preact'
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
import { getFileIconComponent } from '@/Components/AttachedFilesPopover/PopoverFileItem'
Expand All @@ -28,15 +28,20 @@ export const FilePreviewModal: FunctionComponent<Props> = ({ application, files,
const [objectUrl, setObjectUrl] = useState<string>()
const [isFilePreviewable, setIsFilePreviewable] = useState(false)
const [isLoadingFile, setIsLoadingFile] = useState(true)
const [fileDownloadProgress, setFileDownloadProgress] = useState(0)
const [showFileInfoPanel, setShowFileInfoPanel] = useState(false)
const currentFileIdRef = useRef<string>()
const closeButtonRef = useRef<HTMLButtonElement>(null)

const getObjectUrl = useCallback(async () => {
try {
const chunks: Uint8Array[] = []
await application.files.downloadFile(file, async (decryptedChunk: Uint8Array) => {
setFileDownloadProgress(0)
await application.files.downloadFile(file, async (decryptedChunk, progress) => {
chunks.push(decryptedChunk)
if (progress) {
setFileDownloadProgress(Math.round(progress.percentComplete))
}
})
const finalDecryptedBytes = concatenateUint8Arrays(chunks)
setObjectUrl(
Expand Down Expand Up @@ -150,6 +155,10 @@ export const FilePreviewModal: FunctionComponent<Props> = ({ application, files,
className="mr-4"
onClick={() => {
application.getArchiveService().downloadData(objectUrl, file.name)
addToast({
type: ToastType.Success,
message: 'Successfully downloaded file',
})
}}
>
Download
Expand All @@ -168,7 +177,13 @@ export const FilePreviewModal: FunctionComponent<Props> = ({ application, files,
<div className="flex flex-grow min-h-0">
<div className="flex flex-grow items-center justify-center relative max-w-full">
{isLoadingFile ? (
<div className="sk-spinner w-5 h-5 spinner-info"></div>
<div className="flex flex-col items-center">
<div className="flex items-center">
<div className="sk-spinner w-5 h-5 spinner-info mr-3"></div>
<div className="text-base font-semibold">{fileDownloadProgress}%</div>
</div>
<span className="mt-3">Loading file...</span>
</div>
) : objectUrl ? (
<PreviewComponent file={file} objectUrl={objectUrl} />
) : (
Expand Down
11 changes: 4 additions & 7 deletions app/assets/javascripts/UIModels/AppState/FilesState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ export class FilesState {
}

if (progress) {
const progressPercent = Number.isInteger(progress.percentComplete)
? progress.percentComplete
: progress.percentComplete.toFixed(2)
const progressPercent = Math.floor(progress.percentComplete)

updateToast(downloadingToastId, {
message: `Downloading file "${file.name}" (${progressPercent}%)`,
progress: progress.percentComplete,
progress: progressPercent,
})
}
})
Expand Down Expand Up @@ -136,10 +134,9 @@ export class FilesState {
const onChunk = async (chunk: Uint8Array, index: number, isLast: boolean) => {
await this.application.files.pushBytesForUpload(operation, chunk, index, isLast)

const progress = operation.getProgress().percentComplete
const formattedProgress = Number.isInteger(progress) ? progress : progress.toFixed(2)
const progress = Math.round(operation.getProgress().percentComplete)
updateToast(toastId, {
message: `Uploading file "${file.name}" (${formattedProgress}%)`,
message: `Uploading file "${file.name}" (${progress}%)`,
progress,
})
}
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Application < Rails::Application
img_src: ["'self'", '* data:', 'blob:'],
manifest_src: %w('self'),
media_src: %w('self' blob: *.standardnotes.com),
object_src: %w('self'),
object_src: %w('self' blob: *.standardnotes.com),
plugin_types: %w(),
script_src: %w('self' 'unsafe-inline' 'unsafe-eval'),
style_src: %w(* 'unsafe-inline'),
Expand Down

0 comments on commit 2c807e2

Please sign in to comment.