refactor: unify progress bar logic

This commit is contained in:
Carlos Valente
2024-08-09 19:59:15 +02:00
committed by Carlos Valente
parent c3fb3061f6
commit 347f333b3e
9 changed files with 49 additions and 74 deletions
@@ -0,0 +1,23 @@
import { MaybeNumber } from 'ontime-types';
import { clamp } from './math';
/**
* Returns completion percentage of a progress bar
* This code assumes the current time and duration have addedTime already applied
*/
export function getProgress(current: MaybeNumber, duration: MaybeNumber) {
if (current === null || duration === null) {
return 0;
}
if (current <= 0) {
return 100;
}
if (current >= duration) {
return 0;
}
return clamp(((duration - current) / duration) * 100, 0, 100);
}