Skip to content

Commit

Permalink
useDragLeave: Only dispatch updateHoveringPath when needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Nov 26, 2024
1 parent c895983 commit efcc745
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/hooks/useDragLeave.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { debounce } from 'lodash'
import { useEffect, useRef } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { updateHoveringPathActionCreator } from '../actions/updateHoveringPath'
import Thunk from '../@types/Thunk'
import { updateHoveringPathActionCreator as updateHoveringPath } from '../actions/updateHoveringPath'

const DEBOUNCE_DELAY = 50
let hoverCount = 0
let debouncedSetHoveringPath: ReturnType<typeof debounce> | null = null

/** Clears state.hoveringPath if it is set. */
const clearHoveringPath: Thunk = (dispatch, getState) => {
if (getState().hoveringPath) {
dispatch(updateHoveringPath({ path: undefined }))
}
}

/**
* Hook to capture the dragleave event and dispatch the dragInProgress action.
* Hook to capture the dragleave event and dispatch the updateHoveringPath action.
*/
const useDragLeave = ({ isDeepHovering, canDropThought }: { isDeepHovering: boolean; canDropThought: boolean }) => {
const dispatch = useDispatch()
Expand All @@ -20,7 +28,7 @@ const useDragLeave = ({ isDeepHovering, canDropThought }: { isDeepHovering: bool
if (!debouncedSetHoveringPath) {
debouncedSetHoveringPath = debounce(() => {
// Only set hoveringPath to undefined if hoverCount is still zero
dispatch(updateHoveringPathActionCreator({ path: undefined }))
dispatch(clearHoveringPath)
}, DEBOUNCE_DELAY)
}

Expand All @@ -34,7 +42,7 @@ const useDragLeave = ({ isDeepHovering, canDropThought }: { isDeepHovering: bool

// If canDrop is false return
if (!canDropThought) {
dispatch(updateHoveringPathActionCreator({ path: undefined }))
dispatch(clearHoveringPath)
return
}

Expand Down

0 comments on commit efcc745

Please sign in to comment.