Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: renterd warn active uploads #504

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
Loading