@codecademy/brand 3.26.0-alpha.9d629b471a.0 → 3.26.0-alpha.c0e3f304ed.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,46 +0,0 @@
1
- import { useEffect, useRef } from 'react';
2
- import { useSearchTrackingContext } from '../SearchTrackingProvider';
3
- import { isSimilarSetOfStrings } from '../../utils/string-similarity';
4
- import { usePrevious } from 'react-use';
5
- export const useSearchTracking = ({
6
- onSearchAsYouType
7
- }) => {
8
- const {
9
- onSearchAsYouTypeParams
10
- } = useSearchTrackingContext();
11
-
12
- // store the previous search event params
13
- const prevSearchEventParams = usePrevious(onSearchAsYouTypeParams);
14
- const lastTrackedQueryRef = useRef('');
15
- useEffect(() => {
16
- // skip search tracking entirely if tracking function is not provided
17
- if (!onSearchAsYouType) {
18
- return;
19
- }
20
-
21
- // skip empty queries
22
- if (onSearchAsYouTypeParams?.query.length === 0) {
23
- return;
24
- }
25
- let searchEventDelay;
26
-
27
- // scenario 1: track when user's search has no results
28
- if (onSearchAsYouTypeParams?.resultsCount === 0) {
29
- searchEventDelay = setTimeout(() => {
30
- onSearchAsYouType(onSearchAsYouTypeParams.query, onSearchAsYouTypeParams.searchId, 0, onSearchAsYouTypeParams.queryLoadTime);
31
- lastTrackedQueryRef.current = onSearchAsYouTypeParams.query;
32
- }, 1000);
33
- }
34
-
35
- // scenario 2: track when user's search has meaningful edits
36
- if (prevSearchEventParams && onSearchAsYouTypeParams?.query !== prevSearchEventParams.query && prevSearchEventParams.query !== '' && prevSearchEventParams.resultsCount > 0 && prevSearchEventParams.query !== lastTrackedQueryRef.current && !isSimilarSetOfStrings(onSearchAsYouTypeParams?.query ?? '', prevSearchEventParams.query)) {
37
- searchEventDelay = setTimeout(() => {
38
- onSearchAsYouType(prevSearchEventParams.query, prevSearchEventParams.searchId, prevSearchEventParams.resultsCount, prevSearchEventParams.queryLoadTime);
39
- lastTrackedQueryRef.current = prevSearchEventParams.query;
40
- }, 1500);
41
- }
42
- return () => {
43
- clearTimeout(searchEventDelay);
44
- };
45
- }, [onSearchAsYouType, onSearchAsYouTypeParams, prevSearchEventParams]);
46
- };
@@ -1,17 +0,0 @@
1
- /**
2
- * Determines whether two sets of words are similar based on the Jaccard index and
3
- * a specified similarity threshold.
4
- *
5
- * @param a - A set of strings to compare against the second set of strings.
6
- * @param b - A set of strings to compare against the first set of strings.
7
- * @param threshold - A float between 0 and 1, where 1 means the strings are
8
- * identical and 0 means the strings are completely different.
9
- *
10
- * The threshold is used to determine if the strings are similar.
11
- * By default, if the threshold is 0.8, then the strings are considered similar
12
- * if at least 80% of the words in the first set of strings are also in the
13
- * second set of strings.
14
- *
15
- * @returns - It returns true if the strings are similar, false otherwise.
16
- */
17
- export declare const isSimilarSetOfStrings: (a: string, b: string, threshold?: number) => boolean;
@@ -1,30 +0,0 @@
1
- const transformSanitizedStringToArray = s => s.trim().toLowerCase().split(/\s+/);
2
-
3
- /**
4
- * Determines whether two sets of words are similar based on the Jaccard index and
5
- * a specified similarity threshold.
6
- *
7
- * @param a - A set of strings to compare against the second set of strings.
8
- * @param b - A set of strings to compare against the first set of strings.
9
- * @param threshold - A float between 0 and 1, where 1 means the strings are
10
- * identical and 0 means the strings are completely different.
11
- *
12
- * The threshold is used to determine if the strings are similar.
13
- * By default, if the threshold is 0.8, then the strings are considered similar
14
- * if at least 80% of the words in the first set of strings are also in the
15
- * second set of strings.
16
- *
17
- * @returns - It returns true if the strings are similar, false otherwise.
18
- */
19
- export const isSimilarSetOfStrings = (a, b, threshold = 0.8) => {
20
- const setA = Array.from(new Set(transformSanitizedStringToArray(a)));
21
- const setB = Array.from(new Set(transformSanitizedStringToArray(b)));
22
-
23
- // manually calculating intersection and union to support older browsers that may not have
24
- // the Set.intersection and Set.union methods
25
- const intersection = new Set([...setA].filter(s => setB.includes(s)));
26
- const union = new Set([...setA, ...setB]);
27
-
28
- // the Jaccard similarity is the ratio between the intersection and union of the two sets of words
29
- return threshold <= intersection.size / union.size;
30
- };