refactor: extract timer data to hook

This commit is contained in:
Carlos Valente
2025-07-23 21:39:20 +02:00
committed by Carlos Valente
parent 78184b23b7
commit 20ee391525
8 changed files with 117 additions and 165 deletions
@@ -1,24 +1,28 @@
import { MaybeNumber, MaybeString, OntimeEvent, TimerType } from 'ontime-types';
import { MaybeNumber, MaybeString, OntimeEvent, TimerState, TimerType } from 'ontime-types';
import { MILLIS_PER_MINUTE, MILLIS_PER_SECOND, millisToString, removeLeadingZero, removeSeconds } from 'ontime-utils';
import type { ViewExtendedTimer } from '../../../common/models/TimeManager.type';
import { timerPlaceholder, timerPlaceholderMin } from '../../../common/utils/styleUtils';
import { formatTime } from '../../../common/utils/time';
type TimerTypeParams = Pick<ViewExtendedTimer, 'countToEnd' | 'timerType' | 'current' | 'elapsed' | 'clock'>;
/**
* Gathers all options that affect which timer is displayed and selects the correct data source to display
* it also handles edge cases such as freezing on end
*/
export function getTimerByType(
freezeEnd: boolean,
timerObject?: TimerTypeParams,
timerTypeNow: TimerType,
countToEndNow: boolean,
clock: number,
timerObject: Pick<TimerState, 'current' | 'elapsed'>,
timerTypeOverride?: TimerType,
): number | null {
if (!timerObject) {
return null;
}
const viewTimerType = timerTypeOverride ?? timerObject.timerType;
const viewTimerType = timerTypeOverride ?? timerTypeNow;
if (timerObject.countToEnd) {
if (countToEndNow) {
if (timerObject.current === null) {
return null;
}
@@ -34,7 +38,7 @@ export function getTimerByType(
case TimerType.CountUp:
return Math.abs(timerObject.elapsed ?? 0);
case TimerType.Clock:
return timerObject.clock;
return clock;
case TimerType.None:
return null;
default: {