From 8a2f281790bbae222c2b9ec7109b61d90b49dbd6 Mon Sep 17 00:00:00 2001 From: Taso Date: Sun, 17 Jan 2021 15:49:22 -0500 Subject: [PATCH 1/3] enhance(phone): useNuiEvent types and integrated DebugLogger --- phone/src/os/nui-events/hooks/useNuiEvent.ts | 33 +++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/phone/src/os/nui-events/hooks/useNuiEvent.ts b/phone/src/os/nui-events/hooks/useNuiEvent.ts index 8ce77eb..818ff8e 100644 --- a/phone/src/os/nui-events/hooks/useNuiEvent.ts +++ b/phone/src/os/nui-events/hooks/useNuiEvent.ts @@ -1,5 +1,6 @@ -import { useEffect, useRef } from 'react'; +import { MutableRefObject, useEffect, useRef } from 'react'; import { eventNameFactory } from '../utils/nuiUtils'; +import DebugLog from '../../debug/LogDebugEvents'; interface IOptions { capture?: boolean; @@ -7,30 +8,48 @@ interface IOptions { once?: boolean; } +/** + * A hook that manage events listeners for receiving data from the NUI + * @param app The app name in which this hoook is used + * @param method The specific `method` field that should be listened for. + * @param handler The callback function that will handle data relayed by this hook + * @param options Any options to pass to the addEventListener + **/ + export const useNuiEvent = ( - app: any, - method: any, - handler: any, + app: string, + method: string, + handler: Function, options: IOptions = {} ) => { - const savedHandler: any = useRef(); + const savedHandler: MutableRefObject = useRef(); + // Destructure passed options const { capture, passive, once } = options; + // When handler value changes set mutable ref to handler val useEffect(() => { savedHandler.current = handler; }, [handler]); + // Will run every rerender useEffect(() => { const eventName = eventNameFactory(app, method); - const eventListener = (event: any) => { + const eventListener = (event) => { if (savedHandler.current && savedHandler.current.call) { const { data } = event; - console.log(eventName, data); + DebugLog({ + action: `NUI Data Received (${eventName})`, + data: event.data, + level: 1, + }); savedHandler.current(data); } }; + // Why are destructing then restructuring option data? const opts = { capture, passive, once }; + window.addEventListener(eventName, eventListener, opts); + // Remove Event Listener on component cleanup return () => window.removeEventListener(eventName, eventListener, opts); }, [app, method, capture, passive, once]); }; From 2c0657918268248f900028891df1d7f076fdf4eb Mon Sep 17 00:00:00 2001 From: Taso Date: Mon, 18 Jan 2021 12:01:50 -0500 Subject: [PATCH 2/3] enhance(phone/types): Improved types for AppWrapper & AppContent --- phone/src/ui/interface/InterfaceUI.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/phone/src/ui/interface/InterfaceUI.ts b/phone/src/ui/interface/InterfaceUI.ts index c2b2933..eb5f670 100644 --- a/phone/src/ui/interface/InterfaceUI.ts +++ b/phone/src/ui/interface/InterfaceUI.ts @@ -1,13 +1,15 @@ +import { CSSProperties } from 'react'; + export interface AppContentTypes { - children: any; - style?: any; - backdrop?: any; - onClickBackdrop?: any; + children?: JSX.Element | JSX.Element[]; + style?: CSSProperties; + backdrop?: boolean; + onClickBackdrop?: (...args: any[]) => void; } export interface AppWrapperTypes { id?: string; - children: any; - style?: any; - handleClickAway?: any; + children?: JSX.Element | JSX.Element[]; + style?: CSSProperties; + handleClickAway?: (...args: any[]) => void; } From 820354aa2e7da9b800f23de64cdb6a399a6e9517 Mon Sep 17 00:00:00 2001 From: Taso Date: Mon, 18 Jan 2021 12:00:25 -0500 Subject: [PATCH 3/3] Misc(styling): Fixed AppContent shenanigans with height --- phone/src/ui/components/AppContent.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phone/src/ui/components/AppContent.tsx b/phone/src/ui/components/AppContent.tsx index f8069ca..5fb88af 100644 --- a/phone/src/ui/components/AppContent.tsx +++ b/phone/src/ui/components/AppContent.tsx @@ -4,7 +4,7 @@ import { AppContentTypes } from '../interface/InterfaceUI'; const useStyles = makeStyles(() => ({ wrapper: { - height: '70vh', // NOTE: Taso: Hacky change temp, this should be done automatically tbh. Will refactor at some point + flex: 1, overflow: 'auto', }, box: { @@ -39,7 +39,7 @@ export const AppContent = ({ className={classes.backdrop} open={backdrop || false} onClick={onClickBackdrop} - > + /> {children}