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

[WIP] debounce logging for search inputs #1808

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
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
Loading