From 1a2f0700044e00e7c65104717c4c4f22dc93d4f1 Mon Sep 17 00:00:00 2001 From: Mark Mankarious Date: Thu, 10 Aug 2023 17:19:22 +0100 Subject: [PATCH] chore: tightens typings on interaction reducers --- src/components/Node/Node.tsx | 2 +- src/interaction/reducers/Cursor.ts | 1 + src/interaction/reducers/DragItems.ts | 2 +- src/interaction/reducers/Lasso.ts | 4 ++-- src/interaction/reducers/Pan.ts | 6 +++--- src/interaction/useInteractionManager.ts | 3 +++ src/types/interactions.ts | 8 +++++--- 7 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/components/Node/Node.tsx b/src/components/Node/Node.tsx index 8a5b292..59f129b 100644 --- a/src/components/Node/Node.tsx +++ b/src/components/Node/Node.tsx @@ -110,7 +110,7 @@ export const Node = ({ node, iconUrl }: Props) => { /> diff --git a/src/interaction/reducers/Cursor.ts b/src/interaction/reducers/Cursor.ts index 8814789..54c3ce8 100644 --- a/src/interaction/reducers/Cursor.ts +++ b/src/interaction/reducers/Cursor.ts @@ -2,6 +2,7 @@ import { SidebarTypeEnum, InteractionReducer } from 'src/types'; import { CoordsUtils, filterNodesByTile } from 'src/utils'; export const Cursor: InteractionReducer = { + type: 'CURSOR', mousemove: (draftState) => { if (draftState.mode.type !== 'CURSOR') return; diff --git a/src/interaction/reducers/DragItems.ts b/src/interaction/reducers/DragItems.ts index da5c621..d54c5cc 100644 --- a/src/interaction/reducers/DragItems.ts +++ b/src/interaction/reducers/DragItems.ts @@ -2,6 +2,7 @@ import { CoordsUtils } from 'src/utils'; import { InteractionReducer } from 'src/types'; export const DragItems: InteractionReducer = { + type: 'DRAG_ITEMS', mousemove: (draftState) => { if (draftState.mode.type !== 'DRAG_ITEMS') return; @@ -24,7 +25,6 @@ export const DragItems: InteractionReducer = { }); } }, - mousedown: () => {}, mouseup: (draftState) => { draftState.mode = { type: 'CURSOR', showCursor: true, mousedown: null }; } diff --git a/src/interaction/reducers/Lasso.ts b/src/interaction/reducers/Lasso.ts index ce34ea3..d0780d5 100644 --- a/src/interaction/reducers/Lasso.ts +++ b/src/interaction/reducers/Lasso.ts @@ -2,6 +2,7 @@ import { CoordsUtils, isWithinBounds } from 'src/utils'; import { InteractionReducer } from 'src/types'; export const Lasso: InteractionReducer = { + type: 'LASSO', mousemove: (draftState) => { if (draftState.mode.type !== 'LASSO') return; @@ -74,6 +75,5 @@ export const Lasso: InteractionReducer = { showCursor: true, mousedown: null }; - }, - mouseup: () => {} + } }; diff --git a/src/interaction/reducers/Pan.ts b/src/interaction/reducers/Pan.ts index 0d0e4ca..f8d3fad 100644 --- a/src/interaction/reducers/Pan.ts +++ b/src/interaction/reducers/Pan.ts @@ -2,6 +2,8 @@ import { CoordsUtils } from 'src/utils'; import { InteractionReducer } from 'src/types'; export const Pan: InteractionReducer = { + type: 'PAN', + entry: () => {}, mousemove: (draftState) => { if (draftState.mode.type !== 'PAN') return; @@ -13,7 +15,5 @@ export const Pan: InteractionReducer = { ) : draftState.scroll.position; } - }, - mousedown: () => {}, - mouseup: () => {} + } }; diff --git a/src/interaction/useInteractionManager.ts b/src/interaction/useInteractionManager.ts index dd54ea5..842d4a8 100644 --- a/src/interaction/useInteractionManager.ts +++ b/src/interaction/useInteractionManager.ts @@ -68,6 +68,9 @@ export const useInteractionManager = () => { return; const reducerAction = reducer[e.type]; + + if (!reducerAction) return; + const componentOffset = rendererRef.current?.getBoundingClientRect(); const offset: Coords = { x: componentOffset?.left ?? 0, diff --git a/src/types/interactions.ts b/src/types/interactions.ts index a9266a6..efe16ba 100644 --- a/src/types/interactions.ts +++ b/src/types/interactions.ts @@ -20,7 +20,9 @@ export interface State { export type InteractionReducerAction = (state: Draft) => void; export type InteractionReducer = { - mousemove: InteractionReducerAction; - mousedown: InteractionReducerAction; - mouseup: InteractionReducerAction; + type: string; + entry?: InteractionReducerAction; + mousemove?: InteractionReducerAction; + mousedown?: InteractionReducerAction; + mouseup?: InteractionReducerAction; };