Skip to content

Commit

Permalink
feat(front): clamp percent progress
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Nov 9, 2024
1 parent 6c91805 commit 2fcf897
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion gui/frontend/src/services/api/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ export async function progressListener(
let maxNum = 0;
let unlisten: (() => void) | null = null;
const eventHandler: EventCallback<Payload> = (event) => {
/** file count to % */
const toPercentage = (num: number) => (num * 100) / maxNum;
/** To 0 ~ 100 */
const clamped = (progress: number) => Math.max(0, Math.min(100, progress));

if (maxNum === 0) {
maxNum = event.payload.index;
} else {
setProgress(toPercentage(event.payload.index));
const percent = toPercentage(event.payload.index);
setProgress(clamped(percent));
}
};

Expand Down

0 comments on commit 2fcf897

Please sign in to comment.