From f9a5c55c07582b206a435585a6e6e1698eeaabde Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Sat, 31 Aug 2024 10:23:42 +0200 Subject: [PATCH] refactor: reactive options --- .../viewers/timeline/TimelinePage.tsx | 10 ++--- .../viewers/timeline/timeline.utils.ts | 45 +++++++++++-------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/apps/client/src/features/viewers/timeline/TimelinePage.tsx b/apps/client/src/features/viewers/timeline/TimelinePage.tsx index 0cd335b32..06b4d9dfd 100644 --- a/apps/client/src/features/viewers/timeline/TimelinePage.tsx +++ b/apps/client/src/features/viewers/timeline/TimelinePage.tsx @@ -13,7 +13,7 @@ import SuperscriptTime from '../common/superscript-time/SuperscriptTime'; import Section from './timeline-section/TimelineSection'; import Timeline from './Timeline'; import { getTimelineOptions } from './timeline.options'; -import { getFormattedTimeToStart, getScopedRundown, getUpcomingEvents } from './timeline.utils'; +import { getFormattedTimeToStart, getUpcomingEvents, useScopedRundown } from './timeline.utils'; import './TimelinePage.scss'; @@ -34,15 +34,11 @@ interface TimelinePageProps { export default function TimelinePage(props: TimelinePageProps) { const { backstageEvents, general, selectedId, settings, time, viewSettings } = props; const { shouldRender } = useRuntimeStylesheet(viewSettings?.overrideStyles && overrideStylesURL); - + // holds copy of the rundown with only relevant events + const scopedRundown = useScopedRundown(backstageEvents, selectedId); const { getLocalizedString } = useTranslation(); const clock = formatTime(time.clock); - // holds copy of the rundown with only relevant events - const scopedRundown = useMemo(() => { - return getScopedRundown(backstageEvents, selectedId); - }, [backstageEvents, selectedId]); - const { now, next, followedBy } = useMemo(() => { return getUpcomingEvents(scopedRundown, selectedId); }, [scopedRundown, selectedId]); diff --git a/apps/client/src/features/viewers/timeline/timeline.utils.ts b/apps/client/src/features/viewers/timeline/timeline.utils.ts index 8157085a9..665545c8f 100644 --- a/apps/client/src/features/viewers/timeline/timeline.utils.ts +++ b/apps/client/src/features/viewers/timeline/timeline.utils.ts @@ -1,3 +1,5 @@ +import { useMemo } from 'react'; +import { useSearchParams } from 'react-router-dom'; import { isOntimeEvent, MaybeString, OntimeEvent, OntimeRundown } from 'ontime-types'; import { dayInMs, @@ -87,29 +89,34 @@ export function getStatusLabel(timeToStart: number, status: ProgressStatus): str return formatDuration(timeToStart); } -export function getScopedRundown(rundown: OntimeRundown, selectedEventId: MaybeString): OntimeRundown { - if (rundown.length === 0) { - return []; - } +export function useScopedRundown(rundown: OntimeRundown, selectedEventId: MaybeString): OntimeRundown { + const [searchParams] = useSearchParams(); - const params = new URL(document.location.href).searchParams; - const hideBackstage = isStringBoolean(params.get('hideBackstage')); - const hidePast = isStringBoolean(params.get('hidePast')); - - let scopedRundown = [...rundown]; - - if (hidePast && selectedEventId) { - const currentIndex = rundown.findIndex((event) => event.id === selectedEventId); - if (currentIndex >= 0) { - scopedRundown = scopedRundown.slice(currentIndex); + const data = useMemo(() => { + if (rundown.length === 0) { + return []; } - } - if (hideBackstage) { - scopedRundown = scopedRundown.filter((event) => !isOntimeEvent(event) || event.isPublic); - } + const hideBackstage = isStringBoolean(searchParams.get('hideBackstage')); + const hidePast = isStringBoolean(searchParams.get('hidePast')); - return scopedRundown; + let scopedRundown = [...rundown]; + + if (hidePast && selectedEventId) { + const currentIndex = rundown.findIndex((event) => event.id === selectedEventId); + if (currentIndex >= 0) { + scopedRundown = scopedRundown.slice(currentIndex); + } + } + + if (hideBackstage) { + scopedRundown = scopedRundown.filter((event) => !isOntimeEvent(event) || event.isPublic); + } + + return scopedRundown; + }, [rundown, searchParams, selectedEventId]); + + return data; } type UpcomingEvents = {