mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-19 06:04:05 +00:00
refactor: simplify flat rundown
This commit is contained in:
committed by
Carlos Valente
parent
93c913ec5a
commit
8daf3313f2
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useMemo, useRef, useState } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { EntryId, OntimeEntry, Rundown } from 'ontime-types';
|
import { EntryId, OntimeEntry, Rundown } from 'ontime-types';
|
||||||
|
|
||||||
@@ -8,8 +8,6 @@ import { fetchCurrentRundown } from '../api/rundown';
|
|||||||
import { useSelectedEventId } from '../hooks/useSocket';
|
import { useSelectedEventId } from '../hooks/useSocket';
|
||||||
import { ExtendedEntry, getFlatRundownMetadata, getRundownMetadata } from '../utils/rundownMetadata';
|
import { ExtendedEntry, getFlatRundownMetadata, getRundownMetadata } from '../utils/rundownMetadata';
|
||||||
|
|
||||||
import useProjectData from './useProjectData';
|
|
||||||
|
|
||||||
// revision is -1 so that the remote revision is higher
|
// revision is -1 so that the remote revision is higher
|
||||||
const cachedRundownPlaceholder: Rundown = {
|
const cachedRundownPlaceholder: Rundown = {
|
||||||
id: 'default',
|
id: 'default',
|
||||||
@@ -46,32 +44,13 @@ export function useRundownWithMetadata() {
|
|||||||
*/
|
*/
|
||||||
export function useFlatRundown() {
|
export function useFlatRundown() {
|
||||||
const { data, status } = useRundown();
|
const { data, status } = useRundown();
|
||||||
const { data: projectData } = useProjectData();
|
|
||||||
|
|
||||||
const loadedProject = useRef<string>('');
|
const flatRundown = useMemo(() => {
|
||||||
const [prevRevision, setPrevRevision] = useState<number>(-1);
|
if (data.revision === -1) {
|
||||||
const [flatRundown, setFlatRundown] = useState<OntimeEntry[]>([]);
|
return [];
|
||||||
|
|
||||||
// update data whenever the revision changes
|
|
||||||
useEffect(() => {
|
|
||||||
if (data.revision !== -1 && data.revision !== prevRevision) {
|
|
||||||
const flatRundown = data.flatOrder
|
|
||||||
.map((id) => data.entries[id])
|
|
||||||
.filter((entry): entry is OntimeEntry => entry !== undefined);
|
|
||||||
setFlatRundown(flatRundown);
|
|
||||||
setPrevRevision(data.revision);
|
|
||||||
}
|
}
|
||||||
}, [data.entries, data.flatOrder, data.revision, prevRevision]);
|
return data.flatOrder.map((id) => data.entries[id]).filter((entry): entry is OntimeEntry => entry !== undefined);
|
||||||
|
}, [data]);
|
||||||
// TODO: should we have a project id field?
|
|
||||||
// TODO(v4): cleanup as part of load multiple rundowns
|
|
||||||
// invalidate current version if project changes
|
|
||||||
useEffect(() => {
|
|
||||||
if (projectData?.title !== loadedProject.current) {
|
|
||||||
setPrevRevision(-1);
|
|
||||||
loadedProject.current = projectData?.title ?? '';
|
|
||||||
}
|
|
||||||
}, [projectData]);
|
|
||||||
|
|
||||||
return { data: flatRundown, rundownId: data.id, status };
|
return { data: flatRundown, rundownId: data.id, status };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user