mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 06:29:07 +00:00
Small cleanup (#2114)
* refactor: propper memo for `ScheduleContext` value * chore: removed unused file `cuesheet.utils.ts` * fix: memoize event in ScheduleProvider
This commit is contained in:
committed by
GitHub
parent
42147d4d5b
commit
f12d3ca59c
@@ -7,6 +7,7 @@ import {
|
|||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
useLayoutEffect,
|
useLayoutEffect,
|
||||||
|
useMemo,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
@@ -140,25 +141,23 @@ export const ScheduleProvider = ({ children, selectedEventId }: PropsWithChildre
|
|||||||
return () => clearInterval(paginator.current);
|
return () => clearInterval(paginator.current);
|
||||||
}, [cycleInterval, numPages, stopCycle, visiblePage]);
|
}, [cycleInterval, numPages, stopCycle, visiblePage]);
|
||||||
|
|
||||||
let selectedEventIndex = events.findIndex((event) => event.id === selectedEventId);
|
|
||||||
|
|
||||||
// we want to show the event after the current
|
// we want to show the event after the current
|
||||||
const viewEvents = events.slice(selectedEventIndex + 1);
|
const viewEvents = useMemo(() => {
|
||||||
selectedEventIndex = 0;
|
const selectedEventIndex = events.findIndex((event) => event.id === selectedEventId);
|
||||||
|
return (events as ExtendedEntry<OntimeEvent>[]).slice(selectedEventIndex + 1);
|
||||||
|
}, [events, selectedEventId]);
|
||||||
|
|
||||||
return (
|
const value = useMemo(() => {
|
||||||
<ScheduleContext
|
return {
|
||||||
value={{
|
events: viewEvents,
|
||||||
events: viewEvents as ExtendedEntry<OntimeEvent>[],
|
selectedEventId,
|
||||||
selectedEventId,
|
numPages,
|
||||||
numPages,
|
visiblePage,
|
||||||
visiblePage,
|
containerRef,
|
||||||
containerRef,
|
};
|
||||||
}}
|
}, [viewEvents, selectedEventId, numPages, visiblePage, containerRef]);
|
||||||
>
|
|
||||||
{children}
|
return <ScheduleContext value={value}>{children}</ScheduleContext>;
|
||||||
</ScheduleContext>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useSchedule = () => {
|
export const useSchedule = () => {
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
import { parseField } from '../cuesheet.utils';
|
|
||||||
|
|
||||||
describe('parseField()', () => {
|
|
||||||
it('returns a string from given millis on timeStart, TimeEnd and duration', () => {
|
|
||||||
const testData1 = 1000;
|
|
||||||
const testData2 = 60000;
|
|
||||||
const testData3 = 600000;
|
|
||||||
expect(parseField('timeStart', testData1)).toBe('00:00:01');
|
|
||||||
expect(parseField('timeEnd', testData2)).toBe('00:01:00');
|
|
||||||
expect(parseField('duration', testData3)).toBe('00:10:00');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns an empty string on undefined fields', () => {
|
|
||||||
// @ts-expect-error -- testing user data with missing fields
|
|
||||||
expect(parseField('title')).toBe('');
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('simply returns any other value in any other field', () => {
|
|
||||||
const testFields = [
|
|
||||||
{ field: 'nothing', value: '123' },
|
|
||||||
{ field: 'title', value: 'test' },
|
|
||||||
{ field: 'note', value: 'test' },
|
|
||||||
{ field: 'colour', value: 'test' },
|
|
||||||
];
|
|
||||||
|
|
||||||
testFields.forEach((testCase) => {
|
|
||||||
test(`${testCase.field}:${testCase.value}`, () => {
|
|
||||||
expect(parseField(testCase.field, testCase.value)).toBe(testCase.value);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
import { CustomFields, MaybeNumber, OntimeEntryCommonKeys } from 'ontime-types';
|
|
||||||
import { millisToString } from 'ontime-utils';
|
|
||||||
|
|
||||||
type CsvHeaderKey = OntimeEntryCommonKeys | keyof CustomFields;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @description parses a field for export
|
|
||||||
* @param {string} field
|
|
||||||
* @param {*} data
|
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
|
|
||||||
export const parseField = (field: CsvHeaderKey, data: unknown): string => {
|
|
||||||
if (field === 'timeStart' || field === 'timeEnd' || field === 'duration') {
|
|
||||||
return millisToString(data as MaybeNumber, { fallback: '' });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (field === 'skip') {
|
|
||||||
return data ? 'x' : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return String(data ?? '');
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user