mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-31 20:09:11 +00:00
refactor(rundown): resolve rundown data from a scope context
The editor and cuesheet had three overlapping notions of which rundown they were showing: loaded-only data hooks, a prop-drilled RundownSource, and a selection store which read the loaded rundown out of the query cache. Rendering two rundowns at once was not possible. Introduce a rundown scope: a context carrying the rundown identity (id, isLoaded and a selection store) which the utility hooks read from. useRundown, useEntry and friends keep their signatures and call sites, they simply resolve their rundown from the enclosing scope. The app mounts a scope at the root which follows the loaded rundown, so runtime views are unaffected; the editor and cuesheet mount an editable scope which also binds entry actions to the same rundown. - useEventSelection becomes a per-scope store factory, so panels hold independent cursors and the store is handed its rundown instead of reaching for the loaded one - the cuesheet selection is generalised to useRundownSelection with a namespace, so each surface persists its own choice - the clipboard records the rundown an entry was copied from, leaving room for pasting across rundowns - removes useScopedRundown and the prop-drilled RundownSource Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011a5cbVjNC5XXF88b2PkUCa
This commit is contained in:
@@ -5,9 +5,9 @@ import { useEffect, useMemo } from 'react';
|
||||
import { queryRefetchIntervalSlow } from '../../ontimeConfig';
|
||||
import { CURRENT_RUNDOWN_QUERY_KEY, getRundownQueryKey } from '../api/constants';
|
||||
import { fetchCurrentRundown, fetchRundown } from '../api/rundown';
|
||||
import { useRundownScope } from '../context/RundownScopeContext';
|
||||
import { useSelectedEventId } from '../hooks/useSocket';
|
||||
import { ExtendedEntry, getFlatRundownMetadata, getRundownMetadata } from '../utils/rundownMetadata';
|
||||
import { useProjectRundowns } from './useProjectRundowns';
|
||||
|
||||
// revision is -1 so that the remote revision is higher
|
||||
const cachedRundownPlaceholder: Rundown = {
|
||||
@@ -20,42 +20,60 @@ const cachedRundownPlaceholder: Rundown = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Normalised rundown data for the currently loaded rundown.
|
||||
* Provides access to a specific rundown by ID.
|
||||
*
|
||||
* Bootstraps via the `/current` alias so the first paint is a single round-trip,
|
||||
* independent of the project rundown list. Once the loaded id is known, the
|
||||
* query key swaps to the id-keyed cache that is shared with `useRundownById`.
|
||||
* Without an ID we do not yet know which rundown is loaded, so we bootstrap via
|
||||
* the `/current` alias to keep the first paint to a single round-trip, then seed
|
||||
* the id-keyed cache that every other reader shares.
|
||||
*/
|
||||
export default function useRundown() {
|
||||
export function useRundownById(rundownId: string | null | undefined) {
|
||||
const queryClient = useQueryClient();
|
||||
const {
|
||||
data: { loaded: loadedRundownId },
|
||||
} = useProjectRundowns();
|
||||
const id = rundownId ?? '';
|
||||
const isBootstrap = id === '';
|
||||
|
||||
const { data, status, isError, refetch, isFetching } = useQuery<Rundown>({
|
||||
queryKey: loadedRundownId ? getRundownQueryKey(loadedRundownId) : CURRENT_RUNDOWN_QUERY_KEY,
|
||||
queryFn: ({ signal }) => fetchCurrentRundown({ signal }),
|
||||
queryKey: isBootstrap ? CURRENT_RUNDOWN_QUERY_KEY : getRundownQueryKey(id),
|
||||
queryFn: ({ signal }) => (isBootstrap ? fetchCurrentRundown({ signal }) : fetchRundown(id, { signal })),
|
||||
placeholderData: (previousData, _previousQuery) => previousData,
|
||||
refetchInterval: queryRefetchIntervalSlow,
|
||||
});
|
||||
|
||||
// Seed the id-keyed cache when fetching via the bootstrap alias
|
||||
useEffect(() => {
|
||||
if (!data || loadedRundownId) return;
|
||||
if (!data || !isBootstrap) return;
|
||||
queryClient.setQueryData(getRundownQueryKey(data.id), data);
|
||||
}, [data, loadedRundownId, queryClient]);
|
||||
}, [data, isBootstrap, queryClient]);
|
||||
|
||||
// Once we have the ID, drop the temporary current cache
|
||||
useEffect(() => {
|
||||
if (!loadedRundownId) return;
|
||||
if (isBootstrap) return;
|
||||
queryClient.removeQueries({ queryKey: CURRENT_RUNDOWN_QUERY_KEY, exact: true });
|
||||
}, [loadedRundownId, queryClient]);
|
||||
}, [isBootstrap, queryClient]);
|
||||
|
||||
return { data: data ?? cachedRundownPlaceholder, status, isError, refetch, isFetching };
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalised rundown data for the rundown of the enclosing scope
|
||||
*/
|
||||
export default function useRundown() {
|
||||
const { rundownId } = useRundownScope();
|
||||
return useRundownById(rundownId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime state only describes the loaded rundown,
|
||||
* a scope pointed elsewhere must not show a playing event
|
||||
*/
|
||||
export function useScopedSelectedEventId(): EntryId | null {
|
||||
const { isLoaded } = useRundownScope();
|
||||
const selectedEventId = useSelectedEventId();
|
||||
return isLoaded ? selectedEventId : null;
|
||||
}
|
||||
|
||||
export function useRundownWithMetadata() {
|
||||
const { data, status } = useRundown();
|
||||
const selectedEventId = useSelectedEventId();
|
||||
const selectedEventId = useScopedSelectedEventId();
|
||||
const rundownMetadata = useMemo(() => getRundownMetadata(data, selectedEventId), [data, selectedEventId]);
|
||||
return { data, status, rundownMetadata };
|
||||
}
|
||||
@@ -79,7 +97,7 @@ export function useFlatRundown() {
|
||||
|
||||
export function useFlatRundownWithMetadata() {
|
||||
const { data, status } = useRundown();
|
||||
const selectedEventId = useSelectedEventId();
|
||||
const selectedEventId = useScopedSelectedEventId();
|
||||
|
||||
const rundownWithMetadata = useMemo(() => getFlatRundownMetadata(data, selectedEventId), [data, selectedEventId]);
|
||||
return { data: rundownWithMetadata, status };
|
||||
@@ -119,21 +137,3 @@ export function useRundownAuxData() {
|
||||
}, [data]);
|
||||
return { data: filteredData, status };
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to a specific rundown by ID.
|
||||
* When rundownId is null/undefined the query is disabled and returns the placeholder.
|
||||
*/
|
||||
export function useRundownById(rundownId: string | null | undefined) {
|
||||
const enabled = Boolean(rundownId);
|
||||
|
||||
const { data, status, isError, refetch, isFetching } = useQuery<Rundown>({
|
||||
queryKey: getRundownQueryKey(rundownId ?? ''),
|
||||
queryFn: ({ signal }) => fetchRundown(rundownId!, { signal }),
|
||||
enabled,
|
||||
placeholderData: (previousData, _previousQuery) => previousData,
|
||||
refetchInterval: queryRefetchIntervalSlow,
|
||||
});
|
||||
|
||||
return { data: data ?? cachedRundownPlaceholder, status, isError, refetch, isFetching };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user