chore: remove addition of numberOfResults from PgSearchEngine

Signed-off-by: Phil Berryman <phil@berryman.org.uk>
This commit is contained in:
Phil Berryman
2022-11-25 12:15:27 -08:00
parent 29ebc43a0b
commit 38f46e6d5c
4 changed files with 14 additions and 9 deletions
@@ -17,24 +17,34 @@
import React, { useEffect } from 'react';
import { useAnalytics } from '@backstage/core-plugin-api';
import { useSearch } from '../../context';
import usePrevious from 'react-use/lib/usePrevious';
/**
* 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;
useEffect(() => {
if (term) {
// Capture analytics search event with search term provided as value
if (term && hasFinishedLoading) {
// Capture analytics search event with search term and numberOfResults (provided as value)
analytics.captureEvent('search', term, {
value: numberOfResults,
});
}
}, [analytics, term, numberOfResults]);
}, [analytics, term, numberOfResults, hasFinishedLoading]);
return <>{children}</>;
};