mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 22:49:18 +00:00
refactor: stabilise filtering function
This commit is contained in:
committed by
Carlos Valente
parent
ef3d541eb3
commit
fc2e76db0a
@@ -65,6 +65,10 @@ export function useFlatRundownWithMetadata() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides access to a partial rundown based on a filter callback
|
* Provides access to a partial rundown based on a filter callback
|
||||||
|
*
|
||||||
|
* Callers MUST memoize the callback with useCallback to prevent
|
||||||
|
* re-filtering on every render.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
export function usePartialRundown(cb: (event: ExtendedEntry<OntimeEntry>) => boolean) {
|
export function usePartialRundown(cb: (event: ExtendedEntry<OntimeEntry>) => boolean) {
|
||||||
const { data, status } = useFlatRundownWithMetadata();
|
const { data, status } = useFlatRundownWithMetadata();
|
||||||
|
|||||||
@@ -1,4 +1,14 @@
|
|||||||
import { createContext, PropsWithChildren, RefObject, use, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
import {
|
||||||
|
createContext,
|
||||||
|
PropsWithChildren,
|
||||||
|
RefObject,
|
||||||
|
use,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useLayoutEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
import { EntryId, isOntimeEvent, OntimeEntry, OntimeEvent } from 'ontime-types';
|
import { EntryId, isOntimeEvent, OntimeEntry, OntimeEvent } from 'ontime-types';
|
||||||
|
|
||||||
import { usePartialRundown } from '../../../common/hooks-query/useRundown';
|
import { usePartialRundown } from '../../../common/hooks-query/useRundown';
|
||||||
@@ -22,14 +32,20 @@ interface ScheduleProviderProps {
|
|||||||
|
|
||||||
export const ScheduleProvider = ({ children, selectedEventId }: PropsWithChildren<ScheduleProviderProps>) => {
|
export const ScheduleProvider = ({ children, selectedEventId }: PropsWithChildren<ScheduleProviderProps>) => {
|
||||||
const { cycleInterval, stopCycle, filter } = useScheduleOptions();
|
const { cycleInterval, stopCycle, filter } = useScheduleOptions();
|
||||||
const { data: events } = usePartialRundown((entry: ExtendedEntry<OntimeEntry>) => {
|
|
||||||
if (filter) {
|
const filterCallback = useCallback(
|
||||||
// custom keys are prepended with custom-
|
(entry: ExtendedEntry<OntimeEntry>) => {
|
||||||
const customKey = filter.startsWith('custom-') ? filter.slice('custom-'.length) : filter;
|
if (filter) {
|
||||||
return isOntimeEvent(entry) && Boolean(entry.custom[customKey]);
|
// custom keys are prepended with custom-
|
||||||
}
|
const customKey = filter.startsWith('custom-') ? filter.slice('custom-'.length) : filter;
|
||||||
return isOntimeEvent(entry);
|
return isOntimeEvent(entry) && Boolean(entry.custom[customKey]);
|
||||||
});
|
}
|
||||||
|
return isOntimeEvent(entry);
|
||||||
|
},
|
||||||
|
[filter],
|
||||||
|
);
|
||||||
|
|
||||||
|
const { data: events } = usePartialRundown(filterCallback);
|
||||||
|
|
||||||
const [firstIndex, setFirstIndex] = useState(-1);
|
const [firstIndex, setFirstIndex] = useState(-1);
|
||||||
const [numPages, setNumPages] = useState(0);
|
const [numPages, setNumPages] = useState(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user