From 1c5ef95c0ac4656853dc056fa81b39682b180755 Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Mon, 29 Apr 2024 16:58:17 -0600 Subject: [PATCH] feat: updates to event mechanism to use message type path instead of eventType propery Adds ITechPassword interface support with hook and state interface --- src/lib/shared/hooks/interfaces/index.ts | 1 + .../interfaces/useIShutdownPromptTimer.ts | 1 + .../hooks/interfaces/useITechPassword.ts | 33 +++++++++++++++++++ .../state/state/IShutdownPromptTimerState.ts | 4 +-- .../types/state/state/ITechPasswordState.ts | 5 +++ src/lib/types/state/state/index.ts | 1 + src/lib/utils/WebsocketContext.ts | 4 +-- src/lib/utils/WebsocketProvider.tsx | 21 ++++++++---- 8 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 src/lib/shared/hooks/interfaces/useITechPassword.ts create mode 100644 src/lib/types/state/state/ITechPasswordState.ts diff --git a/src/lib/shared/hooks/interfaces/index.ts b/src/lib/shared/hooks/interfaces/index.ts index 51bb8eb..8969623 100644 --- a/src/lib/shared/hooks/interfaces/index.ts +++ b/src/lib/shared/hooks/interfaces/index.ts @@ -21,6 +21,7 @@ export * from "./useISetTopBoxcontrols"; export * from "./useIShadesOpenCloseStop"; export * from "./useIShutdownPromptTimer"; export * from "./useISwitchedOutput"; +export * from "./useITechPassword"; export * from "./useITransport"; export * from "./useMobileControlTouchpanelController"; export * from "./useTwoWayDisplayBase"; diff --git a/src/lib/shared/hooks/interfaces/useIShutdownPromptTimer.ts b/src/lib/shared/hooks/interfaces/useIShutdownPromptTimer.ts index c9ed355..3950312 100644 --- a/src/lib/shared/hooks/interfaces/useIShutdownPromptTimer.ts +++ b/src/lib/shared/hooks/interfaces/useIShutdownPromptTimer.ts @@ -36,3 +36,4 @@ export interface IShutdownPromptTimerReturn { shutdownCancel: () => void; } +export type IShutdownPromptTimerEventTypes = 'timerStarted' | 'timerFinished' | 'timerCancelled'; diff --git a/src/lib/shared/hooks/interfaces/useITechPassword.ts b/src/lib/shared/hooks/interfaces/useITechPassword.ts new file mode 100644 index 0000000..9b07cdb --- /dev/null +++ b/src/lib/shared/hooks/interfaces/useITechPassword.ts @@ -0,0 +1,33 @@ +import { useRoomState } from 'src/lib'; +import { ITechPasswordState } from 'src/lib/types/state/state/ITechPasswordState'; +import { useWebsocketContext } from 'src/lib/utils'; + + +export function useITechPassword(key: string): ITechPasswordReturn | undefined { + const { sendMessage } = useWebsocketContext(); + const techPasswordState = useRoomState(key) as ITechPasswordState | undefined; + + if(!techPasswordState) return undefined; + + const validatePassword = (password: string) => { + sendMessage(`/room/${key}/validateTechPassword`, {password}); + }; + + const setPassword = (oldPassword: string, newPassword: string) => { + sendMessage(`/room/${key}/setTechPassword`, {oldPassword, newPassword}); + }; + + return { techPasswordState, validatePassword, setPassword }; +} + +export interface ITechPasswordReturn { + techPasswordState: ITechPasswordState; + validatePassword: (password: string) => void; + setPassword: (oldPassword: string, newPassword: string) => void; +} + +export interface ITechPasswordValidationResponse { + isValid: boolean; +} + +export type ITechPasswordEventTypes = 'passwordChangedSuccessfully' | 'passwordValidationResult'; \ No newline at end of file diff --git a/src/lib/types/state/state/IShutdownPromptTimerState.ts b/src/lib/types/state/state/IShutdownPromptTimerState.ts index 881484c..0e00667 100644 --- a/src/lib/types/state/state/IShutdownPromptTimerState.ts +++ b/src/lib/types/state/state/IShutdownPromptTimerState.ts @@ -1,6 +1,6 @@ -import { DeviceState } from './DeviceState'; +import { RoomState } from './RoomState'; -export interface IShutdownPromptTimerState extends DeviceState { +export interface IShutdownPromptTimerState extends RoomState { secondsRemaining?: number; percentageRemaining?: number; shutdownPromptSeconds: number; diff --git a/src/lib/types/state/state/ITechPasswordState.ts b/src/lib/types/state/state/ITechPasswordState.ts new file mode 100644 index 0000000..a7d0d3a --- /dev/null +++ b/src/lib/types/state/state/ITechPasswordState.ts @@ -0,0 +1,5 @@ +import { RoomState } from './RoomState'; + +export interface ITechPasswordState extends RoomState { + techPasswordLength: number; +} \ No newline at end of file diff --git a/src/lib/types/state/state/index.ts b/src/lib/types/state/state/index.ts index 491463e..f0e9176 100644 --- a/src/lib/types/state/state/index.ts +++ b/src/lib/types/state/state/index.ts @@ -10,6 +10,7 @@ export * from './IHasSelectableItemsState'; export * from './IHasSurroundChannelsState'; export * from './IHasSurroundSoundModesState'; export * from './IShutdownPromptTimerState'; +export * from './ITechPasswordState'; export * from './LevelControlsState'; export * from './LightingState'; export * from './MatrixRoutingState'; diff --git a/src/lib/utils/WebsocketContext.ts b/src/lib/utils/WebsocketContext.ts index ccf53ab..b9a9401 100644 --- a/src/lib/utils/WebsocketContext.ts +++ b/src/lib/utils/WebsocketContext.ts @@ -4,8 +4,8 @@ import { Message, SimpleContent } from '../types'; export interface WebsocketContextType { sendMessage: (type: string, payload: SimpleContent | unknown ) => void; sendSimpleMessage: (type: string, payload: boolean | number | string ) => void; - addEventHandler: (key: string, eventType: string, callback: (data: Message) => void) => void; - removeEventHandler: (key: string, eventType: string) => void; + addEventHandler: (eventType: string, key: string, callback: (data: Message) => void) => void; + removeEventHandler: (eventType: string, key: string) => void; } const WebsocketContext = createContext({ diff --git a/src/lib/utils/WebsocketProvider.tsx b/src/lib/utils/WebsocketProvider.tsx index 533def1..38078f5 100644 --- a/src/lib/utils/WebsocketProvider.tsx +++ b/src/lib/utils/WebsocketProvider.tsx @@ -13,7 +13,7 @@ import { useRoomKey, useWsIsConnected, } from "../store/runtimeConfig/runtimeSelectors"; -import { EventContent, Message } from "../types"; +import { Message } from "../types"; import sessionStorageKeys from "../types/classes/session-storage-keys"; import WebsocketContext from "./WebsocketContext"; import { loadValue, saveValue } from "./joinParamsService"; @@ -90,20 +90,24 @@ const WebsocketProvider = ({ children }: { children: ReactNode }) => { }; const addEventHandler = useCallback( - (key: string, eventType: string, callback: (data: Message) => void) => { + (eventType: string, key: string, callback: (data: Message) => void) => { if (!eventHandlers.current[eventType]) { eventHandlers.current[eventType] = {}; } eventHandlers.current[eventType][key] = callback; + + console.log('event handler added', eventType, key); }, [] ); const removeEventHandler = useCallback( - (key: string, eventType: string) => { + (eventType: string, key: string) => { if (eventHandlers.current[eventType]) { delete eventHandlers.current[eventType][key]; + + console.log('event handler removed', eventType, key); } }, [] @@ -203,9 +207,14 @@ const WebsocketProvider = ({ children }: { children: ReactNode }) => { break; } } else if (message.type.startsWith("/event/")) { - const eventType = (message.content as EventContent).eventType; - if (!eventType) return; - const handlers = eventHandlers.current[eventType]; + console.log('event message received', message); + // const eventType = (message.content as EventContent).eventType; + // if (!eventType) return; + const handlers = eventHandlers.current[message.type]; + + if(!handlers) { + console.log('no handlers found for event type', message.type); + } if (handlers) { Object.values(handlers).forEach((handler) => {