From ff8ebc09e1a8814bb870e2a6edbbd94a978aa38e Mon Sep 17 00:00:00 2001 From: bedlam343 Date: Wed, 3 Apr 2024 21:35:23 -0700 Subject: [PATCH] basic selector functionality --- src/components/Prototype.tsx | 33 ++++++++++++++++++----- src/prototype/useSelector.ts | 52 +++++++++++++++++++++++++++++++++++- src/types/modalities.ts | 2 +- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/components/Prototype.tsx b/src/components/Prototype.tsx index f6fd848..8b30e0a 100644 --- a/src/components/Prototype.tsx +++ b/src/components/Prototype.tsx @@ -5,9 +5,29 @@ import { useAppDispatch, useAppSelector } from 'src/redux/hooks'; import { addWidget, getWidgetById, getWidgets } from 'src/redux/slices/cmSlice'; import { ONE_SECOND_IN_MS } from 'src/utils/constants'; import type { Widget } from 'src/types/modalities'; +import useSelector from 'src/prototype/useSelector'; +import type { MissileToOwnshipDetected } from 'src/types/schema-types'; + +const dummyMessage: MissileToOwnshipDetected = { + id: 1234, + priority: 10, + kind: 'MissileToOwnshipDetected', + data: { + missileLocation: { lat: 0, lng: 0 }, + survivability: 0, + detectedByAca: undefined, + acaAttackWeapon: undefined, + choiceWeight: 0, + }, +}; const Prototype = () => { const dispatch = useAppDispatch(); + const { message, possibleModalities } = useSelector({ + message: dummyMessage, + }); + + console.log('message:', message, 'possibleModalities:', possibleModalities); // demonstration of using a selector to access redux state const widgets = useAppSelector(getWidgets); @@ -19,12 +39,13 @@ const Prototype = () => { // demonstration of using dispatch function to update redux state const handleAddWidget = () => { + const expirationTime = new Date(); + console.log(expirationTime); + expirationTime.setSeconds( + expirationTime.getSeconds() + (Math.floor(Math.random() * 10) + 5), + ); //set the time to expire to a time between 5 and 15 seconds + console.log(expirationTime); - const expirationTime = new Date() - console.log(expirationTime) - expirationTime.setSeconds(expirationTime.getSeconds()+(Math.floor(Math.random() * 10)+5)) //set the time to expire to a time between 5 and 15 seconds - console.log(expirationTime) - // construct dummy widget const newWidget: Widget = { id: uuid(), @@ -44,7 +65,7 @@ const Prototype = () => { useEffect(() => { // use setInterval to run monitor every second (1000ms) - const interval = setInterval(() => monitor({widgets}), ONE_SECOND_IN_MS); + const interval = setInterval(() => monitor({ widgets }), ONE_SECOND_IN_MS); // clear interval when component unmounts to prevent memory leak return () => clearInterval(interval); }, []); diff --git a/src/prototype/useSelector.ts b/src/prototype/useSelector.ts index 0028711..9e21432 100644 --- a/src/prototype/useSelector.ts +++ b/src/prototype/useSelector.ts @@ -1,4 +1,6 @@ import type { Message } from 'src/types/schema-types'; +import { v4 as uuid } from 'uuid'; +import type { Element } from 'src/types/modalities'; type SelectorProps = { message: Message; @@ -10,7 +12,55 @@ type SelectorProps = { * @returns ??? */ const useSelector = ({ message }: SelectorProps) => { - console.log('useSelector'); + const possibleModalities: Element[] = []; + + // simulation LPD + if (message.kind === 'RequestApprovalToAttack') { + possibleModalities.push({ + id: uuid(), + expiration: new Date().toISOString(), + modality: 'visual', + type: 'button', + }); + } else if (message.kind === 'MissileToOwnshipDetected') { + possibleModalities.push({ + id: uuid(), + expiration: new Date().toISOString(), + modality: 'auditory', + type: 'audio', + }); + possibleModalities.push({ + id: uuid(), + expiration: new Date().toISOString(), + modality: 'visual', + type: 'icon', + }); + } else if (message.kind === 'AcaFuelLow' || message.kind === 'AcaDefect') { + possibleModalities.push({ + id: uuid(), + expiration: new Date().toISOString(), + modality: 'visual', + type: 'table', + }); + possibleModalities.push({ + id: uuid(), + expiration: new Date().toISOString(), + modality: 'visual', + type: 'text', + }); + } else if (message.kind === 'AcaHeadingToBase') { + possibleModalities.push({ + id: uuid(), + expiration: new Date().toISOString(), + modality: 'visual', + type: 'text', + }); + } + + return { + message, + possibleModalities, + }; }; export default useSelector; diff --git a/src/types/modalities.ts b/src/types/modalities.ts index f123a5b..1a0f241 100644 --- a/src/types/modalities.ts +++ b/src/types/modalities.ts @@ -11,7 +11,7 @@ export type Element = { expiration: string; id: string; modality: Modality; - type: 'table' | 'button' | 'text' | 'image' | 'audio'; + type: 'table' | 'button' | 'text' | 'image' | 'audio' | 'icon'; }; export type Widget = {