Skip to content

Commit

Permalink
fix duplicate event captures when arrow keys for result navigation ar…
Browse files Browse the repository at this point in the history
…e used
  • Loading branch information
ben-fornefeld committed Jan 13, 2025
1 parent fff4cb1 commit 8992302
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions apps/web/src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8992302

Please sign in to comment.