From 8992302bf8f6841c076d498ebabc9d44a84b57f1 Mon Sep 17 00:00:00 2001 From: Ben Fornefeld Date: Mon, 13 Jan 2025 11:57:31 +0100 Subject: [PATCH] fix duplicate event captures when arrow keys for result navigation are used --- apps/web/src/components/Search.tsx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/Search.tsx b/apps/web/src/components/Search.tsx index e5332e19b..3dcc4f969 100644 --- a/apps/web/src/components/Search.tsx +++ b/apps/web/src/components/Search.tsx @@ -46,11 +46,16 @@ function useAutocomplete({ close }: { close: () => void }) { >({}) const captureSearchEvent = useDebounceCallback( - (query: string, results_count: number) => + (query: string, results_count: number) => { + // return when length <= one, because this occurs when query is + // erased & last event on debounce stack is let through with 1 char + if (query.length <= 1) return + posthog.capture('searched docs', { query, results_count, - }), + }) + }, 500 ) @@ -95,7 +100,7 @@ function useAutocomplete({ close }: { close: () => void }) { onStateChange({ state }) { setAutocompleteState(state) - if (state.query) { + if (state.query && state.status === 'loading') { captureSearchEvent( state.query, state.collections[0]?.items.length || 0