Skip to content

Commit

Permalink
debounce logging for search inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
BrodyHughes committed Jan 17, 2025
1 parent 4ef0ea4 commit 2281e4d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/analytics/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export const event = {
* Called when the user completes a Revoke Approcal flow and submits the transaction.
*/
revokeSubmitted: 'revoke.submitted',
/*
* Called when the user searches in swap, send or command K
*/
searchQuery: 'search_query',
/**
* Called when a user enters the send flow
*/
Expand Down Expand Up @@ -759,6 +763,20 @@ export type EventProperties = {
[event.pointsViewed]: undefined;
[event.popupOpened]: undefined;
[event.settingsAnalyticsTrackingDisabled]: undefined;
[event.searchQuery]: {
/**
* Where the search was initiated from
*/
location: 'swap' | 'send' | 'commandk';
/**
* The query string that was searched.
*/
query: string;
/**
* The length of the query string.
*/
queryLength: number;
};
[event.revokeSubmitted]: {
/**
* Symbol of the asset being sent.
Expand Down
1 change: 0 additions & 1 deletion src/entries/popup/hooks/useSearchCurrencyLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ export function useSearchCurrencyLists({
const a = popularAssets.filter((asset) =>
queryMatchesAsset(query, asset),
);
console.log('popularAssets', { a });
return a;
},
});
Expand Down
19 changes: 19 additions & 0 deletions src/entries/popup/pages/send/SendTokenInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { motion } from 'framer-motion';
import { debounce } from 'lodash';
import React, {
ChangeEvent,
Dispatch,
Expand Down Expand Up @@ -248,6 +249,8 @@ export const SendTokenInput = React.forwardRef<
const [inputValue, setInputValue] = useState('');
const inputRef = useRef<HTMLInputElement>(null);
const { testnetMode } = useTestnetModeStore();
const [lastLoggedValue, setLastLoggedValue] = useState('');
const debouncedLogRef = useRef<ReturnType<typeof debounce>>();

useImperativeHandle(forwardedRef, () => ({
blur: () => {
Expand Down Expand Up @@ -283,9 +286,25 @@ export const SendTokenInput = React.forwardRef<
[selectAssetAddressAndChain, selectNft],
);

useEffect(() => {
debouncedLogRef.current = debounce((value: string) => {
if (value !== lastLoggedValue && value.trim() !== '') {
// analytics here
console.log('Debounced value:', value);
setLastLoggedValue(value);
}
}, 500);

return () => {
debouncedLogRef.current?.cancel();
};
}, [lastLoggedValue]);

const onInputValueChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value;
setInputValue(e.target.value);
debouncedLogRef.current?.(newValue);
},
[setInputValue],
);
Expand Down
22 changes: 21 additions & 1 deletion src/entries/popup/pages/swap/SwapTokenInput/TokenInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { debounce } from 'lodash';
import React, {
ChangeEvent,
ReactElement,
useCallback,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react';

Expand Down Expand Up @@ -117,6 +119,8 @@ export const TokenInput = React.forwardRef<
forwardedRef,
) {
const [dropdownVisible, setDropdownVisible] = useState(false);
const [lastLoggedValue, setLastLoggedValue] = useState('');
const debouncedLogRef = useRef<ReturnType<typeof debounce>>();

const prevDropdownVisible = usePrevious(dropdownVisible);

Expand Down Expand Up @@ -147,9 +151,25 @@ export const TokenInput = React.forwardRef<
dropdownVisible ? inputRef?.current?.blur() : inputRef?.current?.focus();
}, [dropdownVisible, inputRef, onDropdownOpen, selectAsset]);

useEffect(() => {
debouncedLogRef.current = debounce((value: string) => {
if (value !== lastLoggedValue && value.trim() !== '') {
// analytics here
console.log('Debounced value:', value);
setLastLoggedValue(value);
}
}, 500);

return () => {
debouncedLogRef.current?.cancel();
};
}, [lastLoggedValue]);

const onInputValueChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setAssetFilter(e.target.value);
const newValue = e.target.value;
setAssetFilter(newValue);
debouncedLogRef.current?.(newValue);
},
[setAssetFilter],
);
Expand Down

0 comments on commit 2281e4d

Please sign in to comment.