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

fix: fixed the issues of repeated action triggering #6597

Merged
merged 14 commits into from
Feb 5, 2025
26 changes: 21 additions & 5 deletions packages/components/src/actions/ActionList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Dispatch, ReactNode, SetStateAction } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';

import { debounce } from 'lodash';
import { useIntl } from 'react-intl';
import { type GestureResponderEvent } from 'react-native';
import { useMedia, withStaticProperties } from 'tamagui';
import { useDebouncedCallback } from 'use-debounce';

import { dismissKeyboard } from '@onekeyhq/shared/src/keyboard';
import { ETranslations } from '@onekeyhq/shared/src/locale';
Expand Down Expand Up @@ -42,6 +42,9 @@ export interface IActionListItemProps {
shortcutKeys?: string[] | EShortcutEvents;
}

// Duration to prevent rapid re-triggering of the action list
const PROCESSING_RESET_DELAY = 350;

export function ActionListItem({
icon,
iconProps,
Expand Down Expand Up @@ -338,16 +341,25 @@ const showActionList = (
/>,
);
};
const debouncedShowActionList = debounce(
showActionList,
PROCESSING_RESET_DELAY,
);

function ActionListFrame({
estimatedContentHeight,
...props
}: Omit<IActionListProps, 'estimatedContentHeight'> & {
estimatedContentHeight?: () => Promise<number>;
}) {
const isProcessing = useRef(false);

const { gtMd } = useMedia();
const { disabled, renderTrigger, ...popoverProps } = props;
const handleActionListOpen = useDebouncedCallback(() => {
const handleActionListOpen = () => {
if (isProcessing.current) return;

isProcessing.current = true;
if (estimatedContentHeight) {
void estimatedContentHeight().then((height) => {
showActionList({
imyuanx marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -358,7 +370,11 @@ function ActionListFrame({
} else {
showActionList(popoverProps);
}
}, 250);

setTimeout(() => {
isProcessing.current = false;
}, PROCESSING_RESET_DELAY);
};

if (gtMd) {
return <BasicActionList {...props} />;
Expand All @@ -371,6 +387,6 @@ function ActionListFrame({
}

export const ActionList = withStaticProperties(ActionListFrame, {
show: showActionList,
show: debouncedShowActionList,
huhuanming marked this conversation as resolved.
Show resolved Hide resolved
Item: ActionListItem,
});
Loading