-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5994c4e
commit d20d063
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apps/renterd/contexts/filesManager/useWarnActiveUploadsOnClose.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |