diff --git a/apps/client/src/common/hooks/useEventAction.ts b/apps/client/src/common/hooks/useEventAction.ts index 629b236af3..8d9a599131 100644 --- a/apps/client/src/common/hooks/useEventAction.ts +++ b/apps/client/src/common/hooks/useEventAction.ts @@ -9,6 +9,8 @@ import { parseUserTime, reorderArray, swapEventData, + validateEndAction, + validateTimerType, } from 'ontime-utils'; import { RUNDOWN } from '../api/constants'; @@ -32,7 +34,15 @@ import { useEditorSettings } from '../stores/editorSettings'; */ export const useEventAction = () => { const queryClient = useQueryClient(); - const { defaultPublic, linkPrevious, defaultDuration, defaultWarnTime, defaultDangerTime } = useEditorSettings(); + const { + defaultPublic, + linkPrevious, + defaultDuration, + defaultWarnTime, + defaultDangerTime, + defaultTimerType, + defaultEndAction, + } = useEditorSettings(); /** * Calls mutation to add new event @@ -46,17 +56,17 @@ export const useEventAction = () => { networkMode: 'always', }); + // options to any new block (event / delay / block) type BaseOptions = { after?: string; }; + // options to blocks of type OntimeEvent type EventOptions = BaseOptions & Partial<{ defaultPublic: boolean; linkPrevious: boolean; lastEventId: string; - defaultWarnTime: number; - defaultDangerTime: number; }>; /** @@ -68,13 +78,12 @@ export const useEventAction = () => { // ************* CHECK OPTIONS specific to events if (isOntimeEvent(newEvent)) { + // merge creation time options with event settings const applicationOptions = { after: options?.after, defaultPublic: options?.defaultPublic ?? defaultPublic, lastEventId: options?.lastEventId, linkPrevious: options?.linkPrevious ?? linkPrevious, - defaultWarnTime, - defaultDangerTime, }; if (applicationOptions.linkPrevious && applicationOptions?.lastEventId) { @@ -89,6 +98,7 @@ export const useEventAction = () => { } } + // Override event with options from editor settings if (applicationOptions.defaultPublic) { newEvent.isPublic = true; } @@ -104,6 +114,14 @@ export const useEventAction = () => { if (newEvent.timeWarning === undefined) { newEvent.timeWarning = parseUserTime(defaultWarnTime); } + + if (newEvent.timerType === undefined) { + newEvent.timerType = validateTimerType(defaultTimerType); + } + + if (newEvent.endAction === undefined) { + newEvent.endAction = validateEndAction(defaultEndAction); + } } // handle adding options that concern all event type @@ -117,7 +135,17 @@ export const useEventAction = () => { logAxiosError('Failed adding event', error); } }, - [_addEventMutation, defaultDangerTime, defaultDuration, defaultPublic, defaultWarnTime, linkPrevious, queryClient], + [ + _addEventMutation, + defaultDangerTime, + defaultDuration, + defaultEndAction, + defaultPublic, + defaultTimerType, + defaultWarnTime, + linkPrevious, + queryClient, + ], ); /** diff --git a/apps/client/src/common/stores/editorSettings.ts b/apps/client/src/common/stores/editorSettings.ts index 405c9718d0..f30749236b 100644 --- a/apps/client/src/common/stores/editorSettings.ts +++ b/apps/client/src/common/stores/editorSettings.ts @@ -1,3 +1,5 @@ +import { EndAction, TimerType } from 'ontime-types'; +import { validateEndAction, validateTimerType } from 'ontime-utils'; import { create } from 'zustand'; import { booleanFromLocalStorage } from '../utils/localStorage'; @@ -8,11 +10,15 @@ type EditorSettingsStore = { defaultWarnTime: string; defaultDangerTime: string; defaultPublic: boolean; + defaultTimerType: TimerType; + defaultEndAction: EndAction; setDefaultDuration: (defaultDuration: string) => void; setLinkPrevious: (linkPrevious: boolean) => void; setWarnTime: (warnTime: string) => void; setDangerTime: (dangerTime: string) => void; setDefaultPublic: (defaultPublic: boolean) => void; + setDefaultTimerType: (defaultTimerType: TimerType) => void; + setDefaultEndAction: (defaultEndAction: EndAction) => void; }; export const editorSettingsDefaults = { @@ -21,6 +27,8 @@ export const editorSettingsDefaults = { warnTime: '00:02:00', // 120000 same as backend dangerTime: '00:01:00', // 60000 same as backend isPublic: true, + timerType: TimerType.CountDown, + endAction: EndAction.None, }; enum EditorSettingsKeys { @@ -29,6 +37,8 @@ enum EditorSettingsKeys { DefaultWarnTime = 'ontime-default-warn-time', DefaultDangerTime = 'ontime-default-danger-time', DefaultPublic = 'ontime-default-public', + DefaultTimerType = 'ontime-default-timer-type', + DefaultEndAction = 'ontime-default-end-action', } export const useEditorSettings = create((set) => { @@ -38,6 +48,14 @@ export const useEditorSettings = create((set) => { defaultWarnTime: localStorage.getItem(EditorSettingsKeys.DefaultWarnTime) ?? editorSettingsDefaults.warnTime, defaultDangerTime: localStorage.getItem(EditorSettingsKeys.DefaultDangerTime) ?? editorSettingsDefaults.dangerTime, defaultPublic: booleanFromLocalStorage(EditorSettingsKeys.DefaultPublic, editorSettingsDefaults.isPublic), + defaultTimerType: validateTimerType( + localStorage.getItem(EditorSettingsKeys.DefaultTimerType), + editorSettingsDefaults.timerType, + ), + defaultEndAction: validateEndAction( + localStorage.getItem(EditorSettingsKeys.DefaultEndAction), + editorSettingsDefaults.endAction, + ), setDefaultDuration: (defaultDuration) => set(() => { @@ -65,5 +83,15 @@ export const useEditorSettings = create((set) => { localStorage.setItem(EditorSettingsKeys.DefaultPublic, String(defaultPublic)); return { defaultPublic }; }), + setDefaultTimerType: (defaultTimerType) => + set(() => { + localStorage.setItem(EditorSettingsKeys.DefaultTimerType, String(defaultTimerType)); + return { defaultTimerType }; + }), + setDefaultEndAction: (defaultEndAction) => + set(() => { + localStorage.setItem(EditorSettingsKeys.DefaultEndAction, String(defaultEndAction)); + return { defaultEndAction }; + }), }; }); diff --git a/apps/client/src/features/app-settings/panel/interface-panel/EditorSettingsForm.tsx b/apps/client/src/features/app-settings/panel/interface-panel/EditorSettingsForm.tsx index e621cce86a..325b845306 100644 --- a/apps/client/src/features/app-settings/panel/interface-panel/EditorSettingsForm.tsx +++ b/apps/client/src/features/app-settings/panel/interface-panel/EditorSettingsForm.tsx @@ -7,17 +7,26 @@ import { editorSettingsDefaults, useEditorSettings } from '../../../../common/st import * as Panel from '../PanelUtils'; export default function EditorSettingsForm() { - const eventSettings = useEditorSettings((state) => state); + const { + defaultDuration, + linkPrevious, + defaultWarnTime, + defaultDangerTime, + defaultPublic, + defaultTimerType, + defaultEndAction, + setDefaultDuration, + setLinkPrevious, + setWarnTime, + setDangerTime, + setDefaultPublic, + setDefaultTimerType, + setDefaultEndAction, + } = useEditorSettings((state) => state); - const setDefaultDuration = eventSettings.setDefaultDuration; - const setLinkPrevious = eventSettings.setLinkPrevious; - const setWarnTime = eventSettings.setWarnTime; - const setDangerTime = eventSettings.setDangerTime; - const setDefaultPublic = eventSettings.setDefaultPublic; - - const durationInMs = parseUserTime(eventSettings.defaultDuration); - const warnTimeInMs = parseUserTime(eventSettings.defaultWarnTime); - const dangerTimeInMs = parseUserTime(eventSettings.defaultDangerTime); + const durationInMs = parseUserTime(defaultDuration); + const warnTimeInMs = parseUserTime(defaultWarnTime); + const dangerTimeInMs = parseUserTime(defaultDangerTime); return ( @@ -44,13 +53,19 @@ export default function EditorSettingsForm() { setLinkPrevious(event.target.checked)} /> - setDefaultTimerType(event.target.value as TimerType)} + > @@ -59,7 +74,13 @@ export default function EditorSettingsForm() { - setDefaultEndAction(event.target.value as EndAction)} + > @@ -93,7 +114,7 @@ export default function EditorSettingsForm() { setDefaultPublic(event.target.checked)} />