search-react: minor search bar analytics refactor

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-10-24 14:17:26 +02:00
parent b24a1cb45d
commit a539643cba
2 changed files with 11 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-react': patch
---
Minor refactor of search bar analytics capture
@@ -19,23 +19,22 @@ import { useAnalytics } from '@backstage/core-plugin-api';
import { useSearch } from '../../context';
import usePrevious from 'react-use/lib/usePrevious';
function useFallingEdge(next: boolean) {
const prev = usePrevious(next);
return prev && !next;
}
/**
* Capture search event on term change.
*/
export const TrackSearch = ({ children }: { children: React.ReactChild }) => {
const useHasChanged = (value: any) => {
const previousVal = usePrevious(value);
return previousVal !== value;
};
const analytics = useAnalytics();
const { term, result } = useSearch();
const numberOfResults = result.value?.numberOfResults ?? undefined;
// Stops the analtyics event from firing before the new search engine response is returned
const hasStartedLoading = useHasChanged(result.loading);
const hasFinishedLoading = hasStartedLoading && !result.loading;
const hasFinishedLoading = useFallingEdge(result.loading);
useEffect(() => {
if (term && hasFinishedLoading) {