Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(web): move doubleClick with debounce to utils #811

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
Loading