|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useState, useRef } from 'react'; |
2 | 2 | import { useDebouncedCallback } from 'use-debounce'; |
3 | 3 | import { Link, useHistory } from 'react-router-dom'; |
4 | 4 | import { Combobox, ComboboxInput } from '@reach/combobox'; |
@@ -163,17 +163,38 @@ const SearchInputGroup = () => { |
163 | 163 | new URLSearchParams(window.location.search).get('query') || '' |
164 | 164 | ); |
165 | 165 |
|
| 166 | + const lastTrackedQuery = useRef<string | null>(null); |
| 167 | + |
166 | 168 | const search = (queryString: string) => { |
| 169 | + // Track search start when executing a new search query |
| 170 | + if (lastTrackedQuery.current !== queryString && queryString.length >= 1) { |
| 171 | + track('Dashboard - Topbar - Search'); |
| 172 | + lastTrackedQuery.current = queryString; |
| 173 | + } |
| 174 | + |
167 | 175 | history.push(dashboardUrls.search(queryString, activeTeam)); |
168 | 176 | }; |
169 | 177 | const [debouncedSearch] = useDebouncedCallback(search, 200); |
170 | 178 |
|
| 179 | + const onFocus = () => { |
| 180 | + track('Dashboard - Topbar - Search Focus'); |
| 181 | + }; |
| 182 | + |
171 | 183 | const onChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
172 | | - setQuery(event.target.value); |
173 | | - if (event.target.value.length >= 1) { |
174 | | - debouncedSearch(event.target.value); |
| 184 | + const newValue = event.target.value; |
| 185 | + |
| 186 | + setQuery(newValue); |
| 187 | + |
| 188 | + // Reset tracking when query becomes empty |
| 189 | + if (newValue.length === 0) { |
| 190 | + lastTrackedQuery.current = null; |
| 191 | + } |
| 192 | + |
| 193 | + if (newValue.length >= 1) { |
| 194 | + debouncedSearch(newValue); |
175 | 195 | } |
176 | | - if (!event.target.value) { |
| 196 | + |
| 197 | + if (!newValue) { |
177 | 198 | history.push(dashboardUrls.recent(activeTeam)); |
178 | 199 | } |
179 | 200 | }; |
@@ -214,6 +235,7 @@ const SearchInputGroup = () => { |
214 | 235 | as={Input} |
215 | 236 | value={query} |
216 | 237 | onChange={onChange} |
| 238 | + onFocus={onFocus} |
217 | 239 | // onKeyPress={handleEnter} |
218 | 240 | placeholder="Search in workspace" |
219 | 241 | icon="search" |
|
0 commit comments