-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Upload progress feedback (#1261)
* feat: Upload progress component increments file upload percentage * fix: Linting errors * test: Upload rate calculation * test: Upload rate calculation * test: Upload rate calculation * chore: Code formating * test: Rate calculation * test: Fix miss named object property * test: Add upload progress cleanup function * test: Upload rate calculation integrated into percentage value * fix: Linting errors * test: Update upload rate increment value * test: Update upload rate increment value * test: Rate calculation moved to UploadProgress component * test: Set interval to update progress * cleanup: Commented out code removed, console log statements removed
- Loading branch information
Showing
3 changed files
with
57 additions
and
7 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
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
49 changes: 49 additions & 0 deletions
49
packages/website/modules/zero/components/dropzone/uploadProgress.js
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,49 @@ | ||
import clsx from 'clsx'; | ||
import { useState, useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* @prop {string} [label] | ||
* @prop {number} [progress] | ||
*/ | ||
|
||
const UploadProgress = ({ label, progress }) => { | ||
const [displayValue, setDisplayValue] = useState(0); | ||
const lastChunkProgress = useRef(0); | ||
const lastChunkTime = useRef(0); | ||
const rate = useRef(0.1); | ||
const fps = 1; | ||
const fpsInterval = 1000 / fps; | ||
|
||
const incrementDisplayValue = () => { | ||
setDisplayValue(displayValue => displayValue + rate.current); | ||
} | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
incrementDisplayValue(); | ||
}, 1000); | ||
return () => { | ||
clearInterval(interval); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (lastChunkProgress.current === 0 || lastChunkTime.current === 0) { | ||
rate.current = 0.1; | ||
} else { | ||
rate.current = Math.min(3, Math.max(0.1, ((progress - lastChunkProgress.current) / (Date.now() - lastChunkTime.current)) * 100)); | ||
} | ||
setDisplayValue(progress); | ||
lastChunkProgress.current = progress; | ||
lastChunkTime.current = Date.now(); | ||
}, [progress]); | ||
|
||
return ( | ||
<div className="loading-c"> | ||
<span className="loading-label">{label}</span> | ||
<span className="loading-count">{Math.min(displayValue, 99.9).toFixed(1)}%</span> | ||
</div> | ||
); | ||
} | ||
|
||
export default UploadProgress; |