mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-22 16:48:23 +01:00
383a71fc25
Fixes an infinite loop in the layoutModel atom synchronization that would cause the atom to update indefinitely when the root node is deleted. Also adds a dedicated `disabled` flag for the IconButton decl so we can disable the onClick handler when the button is disabled. Also updates the Magnify toggle button to use this new flag, so that when there's only one leaf in a layout, the magnify button is disabed.
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
// Copyright 2024, Command Line Inc.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
|
|
export const useLongClick = (ref, onClick, onLongClick, disabled = false, ms = 300) => {
|
|
const timerRef = useRef(null);
|
|
const [longClickTriggered, setLongClickTriggered] = useState(false);
|
|
|
|
const startPress = useCallback(
|
|
(e: React.MouseEvent<any>) => {
|
|
if (onLongClick == null) {
|
|
return;
|
|
}
|
|
setLongClickTriggered(false);
|
|
timerRef.current = setTimeout(() => {
|
|
setLongClickTriggered(true);
|
|
onLongClick?.(e);
|
|
}, ms);
|
|
},
|
|
[onLongClick, ms]
|
|
);
|
|
|
|
const stopPress = useCallback(() => {
|
|
clearTimeout(timerRef.current);
|
|
}, []);
|
|
|
|
const handleClick = useCallback(
|
|
(e: React.MouseEvent<any>) => {
|
|
if (longClickTriggered) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
return;
|
|
}
|
|
onClick?.(e);
|
|
},
|
|
[longClickTriggered, onClick]
|
|
);
|
|
|
|
useEffect(() => {
|
|
const element = ref.current;
|
|
|
|
if (!element || disabled) return;
|
|
|
|
element.addEventListener("mousedown", startPress);
|
|
element.addEventListener("mouseup", stopPress);
|
|
element.addEventListener("mouseleave", stopPress);
|
|
element.addEventListener("click", handleClick);
|
|
|
|
return () => {
|
|
element.removeEventListener("mousedown", startPress);
|
|
element.removeEventListener("mouseup", stopPress);
|
|
element.removeEventListener("mouseleave", stopPress);
|
|
element.removeEventListener("click", handleClick);
|
|
};
|
|
}, [ref.current, startPress, stopPress, handleClick]);
|
|
|
|
return ref;
|
|
};
|