mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-22 15:39:11 +00:00
fix: prevent stale data on project change
This commit is contained in:
committed by
Carlos Valente
parent
9440ad17d2
commit
6e8734c73f
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { NormalisedRundown, OntimeRundown, RundownCached } from 'ontime-types';
|
import { NormalisedRundown, OntimeRundown, RundownCached } from 'ontime-types';
|
||||||
|
|
||||||
@@ -6,6 +6,8 @@ import { queryRefetchIntervalSlow } from '../../ontimeConfig';
|
|||||||
import { RUNDOWN } from '../api/constants';
|
import { RUNDOWN } from '../api/constants';
|
||||||
import { fetchNormalisedRundown } from '../api/rundown';
|
import { fetchNormalisedRundown } from '../api/rundown';
|
||||||
|
|
||||||
|
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 = { order: [] as string[], rundown: {} as NormalisedRundown, revision: -1 };
|
const cachedRundownPlaceholder = { order: [] as string[], rundown: {} as NormalisedRundown, revision: -1 };
|
||||||
|
|
||||||
@@ -24,7 +26,9 @@ export default function useRundown() {
|
|||||||
|
|
||||||
export function useFlatRundown() {
|
export function useFlatRundown() {
|
||||||
const { data, status } = useRundown();
|
const { data, status } = useRundown();
|
||||||
|
const { data: projectData } = useProjectData();
|
||||||
|
|
||||||
|
const loadedProject = useRef<string>('');
|
||||||
const [prevRevision, setPrevRevision] = useState<number>(-1);
|
const [prevRevision, setPrevRevision] = useState<number>(-1);
|
||||||
const [flatRunDown, setFlatRunDown] = useState<OntimeRundown>([]);
|
const [flatRunDown, setFlatRunDown] = useState<OntimeRundown>([]);
|
||||||
|
|
||||||
@@ -37,5 +41,14 @@ export function useFlatRundown() {
|
|||||||
}
|
}
|
||||||
}, [data.order, data.revision, data.rundown, prevRevision]);
|
}, [data.order, data.revision, data.rundown, prevRevision]);
|
||||||
|
|
||||||
|
// TODO: should we have a project id field?
|
||||||
|
// invalidate current version if project changes
|
||||||
|
useEffect(() => {
|
||||||
|
if (projectData?.title !== loadedProject.current) {
|
||||||
|
setPrevRevision(-1);
|
||||||
|
loadedProject.current = projectData?.title ?? '';
|
||||||
|
}
|
||||||
|
}, [projectData]);
|
||||||
|
|
||||||
return { data: flatRunDown, status };
|
return { data: flatRunDown, status };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user