Skip to content

Commit

Permalink
chore(web): move doubleClick with debounce to utils (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyshx committed Nov 14, 2023
1 parent 770edb9 commit 80b65fa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
@@ -1,10 +1,11 @@
import { ReactNode, useCallback, useRef, useState } from "react";
import { ReactNode, useCallback, useState } from "react";

import TextInput from "@reearth/beta/components/fields/common/TextInput";
import Icon from "@reearth/beta/components/Icon";
import * as Popover from "@reearth/beta/components/Popover";
import Text from "@reearth/beta/components/Text";
import type { LayerStyleNameUpdateProps } from "@reearth/beta/features/Editor/useLayerStyles";
import useDoubleClick from "@reearth/beta/utils/use-double-click";
import { styled } from "@reearth/services/theme";

type Props = {
Expand Down Expand Up @@ -32,31 +33,11 @@ const LayerStyleCard: React.FC<Props> = ({
const [isHovered, setIsHovered] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [newName, setNewName] = useState(name);
const clickTimeoutRef = useRef<NodeJS.Timeout>();
const clickedNameCount = useRef(0);

const handleClick = useCallback(() => {
console.log(clickedNameCount.current);
if (clickTimeoutRef.current) {
clearTimeout(clickTimeoutRef.current);
}
clickTimeoutRef.current = setTimeout(() => {
clickedNameCount.current = 0;
onSelect?.(!selected);
}, 100);
}, [onSelect, selected]);

const handleNameClick = useCallback(() => {
const newClickCount = clickedNameCount.current + 1;
console.log(newClickCount);
clickedNameCount.current = newClickCount;
// if (clickTimeoutRef.current) {
// clearTimeout(clickTimeoutRef.current);
// }
if (clickedNameCount.current === 2) {
setIsEditing(true);
}
}, []);

const [handleSingleClick, handleDoubleClick] = useDoubleClick(
() => onSelect?.(!selected),
() => setIsEditing(true),
);

const handleMouseEnter = useCallback(() => {
setIsHovered(true);
Expand Down Expand Up @@ -100,7 +81,7 @@ const LayerStyleCard: React.FC<Props> = ({
<Wrapper
className={className}
selected={selected}
onClick={handleClick}
onClick={handleSingleClick}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}>
<MainWrapper>
Expand Down Expand Up @@ -129,7 +110,7 @@ const LayerStyleCard: React.FC<Props> = ({
onExit={handleEditExit}
/>
) : (
<StyleName size="footnote" onDoubleClick={handleNameClick}>
<StyleName size="footnote" onDoubleClick={handleDoubleClick}>
{name}
</StyleName>
)}
Expand Down
Expand Up @@ -8,6 +8,7 @@ import type {
LayerNameUpdateProps,
LayerVisibilityUpdateProps,
} from "@reearth/beta/features/Editor/useLayers";
import useDoubleClick from "@reearth/beta/utils/use-double-click";
import { styled } from "@reearth/services/theme";

type LayerItemProps = {
Expand Down Expand Up @@ -36,28 +37,13 @@ const LayerItem = ({
const [newValue, setNewValue] = useState(layerTitle);
const [isVisible, setIsVisible] = useState(visible);
const [value, setValue] = useState(isVisible ? "V" : "");
const [clickTimeoutId, setClickTimeoutId] = useState<any>(null);

const handleActionMenuToggle = useCallback(() => setActionOpen(prev => !prev), []);

const handleClick = useCallback(() => {
if (clickTimeoutId) {
clearTimeout(clickTimeoutId);
setClickTimeoutId(null);
}
const timeoutId = setTimeout(() => {
onSelect();
}, 200);
setClickTimeoutId(timeoutId);
}, [onSelect, clickTimeoutId]);

const handleLayerTitleClick = useCallback(() => {
if (clickTimeoutId) {
clearTimeout(clickTimeoutId);
setClickTimeoutId(null);
}
setIsEditing(true);
}, [clickTimeoutId]);
const [handleSingleClick, handleDoubleClick] = useDoubleClick(
() => onSelect?.(),
() => setIsEditing(true),
);

const handleChange = useCallback((newTitle: string) => setNewValue(newTitle), []);

Expand Down Expand Up @@ -96,7 +82,7 @@ const LayerItem = ({
isSelected={isSelected}
isOpenAction={isActionOpen}
actionPlacement="bottom-end"
onItemClick={handleClick}
onItemClick={handleSingleClick}
onActionClick={handleActionMenuToggle}
onOpenChange={isOpen => setActionOpen(!!isOpen)}
actionContent={
Expand All @@ -122,7 +108,7 @@ const LayerItem = ({
onBlur={handleEditExit}
/>
) : (
<LayerTitle onDoubleClick={handleLayerTitleClick}>{layerTitle}</LayerTitle>
<LayerTitle onDoubleClick={handleDoubleClick}>{layerTitle}</LayerTitle>
)}
<HideLayer onClick={handleUpdateVisibility}>
<Text size="footnote">{value}</Text>
Expand Down
33 changes: 33 additions & 0 deletions web/src/beta/utils/use-double-click.ts
@@ -0,0 +1,33 @@
import { useCallback, useRef } from "react";

const useDoubleClick = (
onClick: (() => void) | undefined,
onDoubleClick: (() => void) | undefined,
delay = 200,
): [() => void, () => void] => {
const timerRef = useRef<NodeJS.Timeout | null>(null);

const singleClickHandler = useCallback(() => {
if (timerRef.current) {
clearTimeout(timerRef.current);
timerRef.current = null;
} else if (onClick) {
timerRef.current = setTimeout(() => {
onClick();
timerRef.current = null;
}, delay);
}
}, [onClick, delay]);

const doubleClickHandler = useCallback(() => {
if (timerRef.current) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
onDoubleClick?.();
}, [onDoubleClick]);

return [singleClickHandler, doubleClickHandler];
};

export default useDoubleClick;

0 comments on commit 80b65fa

Please sign in to comment.