🎯 Use case
This file lives under “hooks/”, which covers reusable UI or integration hooks. On the API surface it exposes DOUBLE_PRESS_TIMEOUT_MS and useDoublePress — mainly functions, hooks, or classes. Dependencies touch React UI. What the file header says: Creates a function that calls one function on the first call and another function on the second call within a certain timeout.
Generated from folder role, exports, dependency roots, and inline comments — not hand-reviewed for every path.
🧠 Inline summary
Creates a function that calls one function on the first call and another function on the second call within a certain timeout
📤 Exports (heuristic)
DOUBLE_PRESS_TIMEOUT_MSuseDoublePress
📚 External import roots
Package roots from from "…" (relative paths omitted).
react
🖥️ Source preview
// Creates a function that calls one function on the first call and another
// function on the second call within a certain timeout
import { useCallback, useEffect, useRef } from 'react'
export const DOUBLE_PRESS_TIMEOUT_MS = 800
export function useDoublePress(
setPending: (pending: boolean) => void,
onDoublePress: () => void,
onFirstPress?: () => void,
): () => void {
const lastPressRef = useRef<number>(0)
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined)
const clearTimeoutSafe = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current)
timeoutRef.current = undefined
}
}, [])
// Cleanup timeout on unmount
useEffect(() => {
return () => {
clearTimeoutSafe()
}
}, [clearTimeoutSafe])
return useCallback(() => {
const now = Date.now()
const timeSinceLastPress = now - lastPressRef.current
const isDoublePress =
timeSinceLastPress <= DOUBLE_PRESS_TIMEOUT_MS &&
timeoutRef.current !== undefined
if (isDoublePress) {
// Double press detected
clearTimeoutSafe()
setPending(false)
onDoublePress()
} else {
// First press
onFirstPress?.()
setPending(true)
// Clear any existing timeout and set new one
clearTimeoutSafe()
timeoutRef.current = setTimeout(
(setPending, timeoutRef) => {
setPending(false)
timeoutRef.current = undefined
},
DOUBLE_PRESS_TIMEOUT_MS,
setPending,
timeoutRef,
)
}
lastPressRef.current = now
}, [setPending, onDoublePress, onFirstPress, clearTimeoutSafe])
}