mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-01 11:56:25 +02:00
feat: implement long-press functionality across chat and document components for enhanced user interaction and integrate custom hook for reusability
This commit is contained in:
parent
3af9962abc
commit
b07f8699f6
5 changed files with 233 additions and 129 deletions
39
surfsense_web/hooks/use-long-press.ts
Normal file
39
surfsense_web/hooks/use-long-press.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { useCallback, useRef } from "react";
|
||||
|
||||
const LONG_PRESS_DELAY = 500;
|
||||
|
||||
export function useLongPress(onLongPress: () => void, delay = LONG_PRESS_DELAY) {
|
||||
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const triggeredRef = useRef(false);
|
||||
|
||||
const start = useCallback(() => {
|
||||
triggeredRef.current = false;
|
||||
timerRef.current = setTimeout(() => {
|
||||
triggeredRef.current = true;
|
||||
onLongPress();
|
||||
}, delay);
|
||||
}, [onLongPress, delay]);
|
||||
|
||||
const cancel = useCallback(() => {
|
||||
if (timerRef.current) {
|
||||
clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handlers = {
|
||||
onTouchStart: start,
|
||||
onTouchEnd: cancel,
|
||||
onTouchMove: cancel,
|
||||
};
|
||||
|
||||
const wasLongPress = useCallback(() => {
|
||||
if (triggeredRef.current) {
|
||||
triggeredRef.current = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}, []);
|
||||
|
||||
return { handlers, wasLongPress };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue