Skip to content

Commit

Permalink
feat: renterd warn active uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Feb 27, 2024
1 parent 5994c4e commit d20d063
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-nails-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'renterd': minor
---

The browser now warns the user if they have active uploads and try to close the tab.
4 changes: 4 additions & 0 deletions apps/renterd/contexts/filesManager/uploads.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { MultipartUpload } from '../../lib/multipartUpload'
import { MiBToBytes } from '@siafoundation/units'
import { useMutate } from '@siafoundation/react-core'
import { useRedundancySettings } from '../../hooks/useRedundancySettings'
import { useWarnActiveUploadsOnClose } from './useWarnActiveUploadsOnClose'

const maxConcurrentUploads = 5
const maxConcurrentPartsPerUpload = 5
Expand Down Expand Up @@ -280,6 +281,9 @@ export function useUploads({ activeDirectoryPath }: Props) {
[uploadsMap]
)

// Abort local uploads when the browser tab is closed
useWarnActiveUploadsOnClose({ uploadsMap })

return {
uploadFiles,
uploadsMap,
Expand Down
30 changes: 30 additions & 0 deletions apps/renterd/contexts/filesManager/useWarnActiveUploadsOnClose.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect } from 'react'
import { UploadsMap } from './types'

export function useWarnActiveUploadsOnClose({
uploadsMap,
}: {
uploadsMap: UploadsMap
}) {
useEffect(() => {
const activeUploads = Object.values(uploadsMap).filter(
(upload) => upload.uploadStatus === 'uploading'
)

const warnUserAboutActiveUploads = (event: BeforeUnloadEvent) => {
if (activeUploads.length > 0) {
const message = `Warning, closing the tab will abort all ${activeUploads.length} active uploads.`
event.returnValue = message // Legacy method for cross browser support
return message // Chrome requires returnValue to be set
}
}

if (activeUploads.length > 0) {
window.addEventListener('beforeunload', warnUserAboutActiveUploads)
}

return () => {
window.removeEventListener('beforeunload', warnUserAboutActiveUploads)
}
}, [uploadsMap])
}

0 comments on commit d20d063

Please sign in to comment.