mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 14:39:06 +00:00
b4ad533e6a
* refactor: isolate clock * refactor: isolate private * refactor: remove duplicate * chore: rename loaded > runtime * fix: handle no events loaded
68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
import { OntimeEvent, Playback } from 'ontime-types';
|
|
|
|
import { ViewExtendedTimer } from '../../../common/models/TimeManager.type';
|
|
|
|
export enum TimerMessage {
|
|
toStart = 'to_start',
|
|
waiting = 'waiting',
|
|
running = 'running',
|
|
ended = 'ended',
|
|
unhandled = '',
|
|
}
|
|
|
|
/**
|
|
* Parses string as a title
|
|
*/
|
|
export const sanitiseTitle = (title: string | null) => (title ? title : '{no title}');
|
|
|
|
/**
|
|
* Returns a parsed timer and relevant status message
|
|
*/
|
|
export const fetchTimerData = (
|
|
time: ViewExtendedTimer,
|
|
follow: OntimeEvent,
|
|
selectedId: string | null,
|
|
): { message: TimerMessage; timer: number } => {
|
|
let message;
|
|
let timer;
|
|
|
|
if (selectedId === follow.id) {
|
|
// check that is not running
|
|
message = time.playback === Playback.Pause ? TimerMessage.waiting : TimerMessage.running;
|
|
timer = time.current ?? 0;
|
|
} else if (time.clock < follow.timeStart) {
|
|
// if it hasnt started, we count to start
|
|
message = TimerMessage.toStart;
|
|
timer = follow.timeStart - time.clock;
|
|
} else if (follow.timeStart <= time.clock && time.clock <= follow.timeEnd) {
|
|
// if it has started, we show running timer
|
|
message = TimerMessage.waiting;
|
|
timer = time.current ?? 0;
|
|
} else {
|
|
// running timer timer is not the one we are following
|
|
|
|
if (follow.timeStart > follow.timeEnd) {
|
|
// ends day after
|
|
|
|
if (follow.timeStart > time.clock) {
|
|
// if it hasnt started, we count to start
|
|
message = TimerMessage.toStart;
|
|
timer = follow.timeStart - time.clock;
|
|
} else if (follow.timeStart <= time.clock) {
|
|
// if it has started, we show running timer
|
|
message = TimerMessage.waiting;
|
|
timer = time.current ?? 0;
|
|
} else {
|
|
// if it has ended, we show how long ago
|
|
message = TimerMessage.ended;
|
|
timer = follow.timeEnd;
|
|
}
|
|
} else {
|
|
// if it has ended, we show how long ago
|
|
message = TimerMessage.ended;
|
|
timer = follow.timeEnd;
|
|
}
|
|
}
|
|
return { message, timer };
|
|
};
|