Skip to content

Commit

Permalink
fix: prevent stale data on project change
Browse files Browse the repository at this point in the history
  • Loading branch information
cpvalente committed Nov 6, 2024
1 parent 9ba9428 commit 3e63f1d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/client/src/common/hooks-query/useRundown.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { NormalisedRundown, OntimeRundown, RundownCached } from 'ontime-types';

import { queryRefetchIntervalSlow } from '../../ontimeConfig';
import { RUNDOWN } from '../api/constants';
import { fetchNormalisedRundown } from '../api/rundown';

import useProjectData from './useProjectData';

// revision is -1 so that the remote revision is higher
const cachedRundownPlaceholder = { order: [] as string[], rundown: {} as NormalisedRundown, revision: -1 };

Expand All @@ -24,7 +26,9 @@ export default function useRundown() {

export function useFlatRundown() {
const { data, status } = useRundown();
const { data: projectData } = useProjectData();

const loadedProject = useRef<string>('');
const [prevRevision, setPrevRevision] = useState<number>(-1);
const [flatRunDown, setFlatRunDown] = useState<OntimeRundown>([]);

Expand All @@ -37,5 +41,14 @@ export function useFlatRundown() {
}
}, [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 };
}

0 comments on commit 3e63f1d

Please sign in to comment.