• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yext / search-ui-react / 10372922433

13 Aug 2024 03:43PM UTC coverage: 85.215% (-0.04%) from 85.252%
10372922433

push

github

web-flow
Merge pull request #453 from yext/dev/merge-v1.6.1-9b282d9-into-develop

Merge main (v1.6.1) into develop

1337 of 1744 branches covered (76.66%)

Branch coverage included in aggregate %.

9 of 11 new or added lines in 2 files covered. (81.82%)

1908 of 2064 relevant lines covered (92.44%)

145.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.18
/src/hooks/useSearchWithNearMeHandling.ts
1
import { useSearchActions, AutocompleteResponse, SearchIntent } from '@yext/search-headless-react';
6✔
2
import { executeSearch, executeAutocomplete } from '../utils/search-operations';
6✔
3
import { updateLocationIfNeeded } from '../utils/location-operations';
6✔
4
import { MutableRefObject, useRef } from 'react';
6✔
5
import { onSearchFunc } from '../components/SearchBar';
6

7
/** The type of a function for executing a query and returning a promise. @public */
8
export type QueryFunc = () => Promise<void>;
9
/**
10
 * A ref which contains a promise of the latest autocomplete response in order get the
11
 * latest search intents.
12
 */
13
export type AutocompleteRef = MutableRefObject<Promise<AutocompleteResponse | undefined> | undefined>;
14

15
/**
16
 * Returns a search action that will handle near me searches, by first checking
17
 * for near me intents using an autocomplete request.
18
 *
19
 * @remarks
20
 * You can optionally use the provided ref to store autocomplete responses, to avoid
21
 * making unnecessary autocomplete requests.
22
 */
23
export function useSearchWithNearMeHandling(
6✔
24
  geolocationOptions?: PositionOptions,
25
  onSearch?: onSearchFunc
26
): [QueryFunc, AutocompleteRef] {
27
  /**
28
   * Allow a query search to wait on the response to the autocomplete request right
29
   * before the search execution in order to retrieve the search intents.
30
   */
31
  const autocompletePromiseRef = useRef<Promise<AutocompleteResponse | undefined>>();
179✔
32
  const searchActions = useSearchActions();
179✔
33

34
  async function executeQuery() {
35
    try {
14✔
36
      let intents: SearchIntent[] = [];
14✔
37
      if (!searchActions.state.location.userLocation) {
14✔
38
        if (!autocompletePromiseRef.current) {
13✔
39
          autocompletePromiseRef.current = executeAutocomplete(searchActions);
11✔
40
        }
41
        const autocompleteResponseBeforeSearch = await autocompletePromiseRef.current;
13✔
42
        intents = autocompleteResponseBeforeSearch?.inputIntents || [];
13!
43
        await updateLocationIfNeeded(searchActions, intents, geolocationOptions);
13✔
44
      }
45
    } catch (e) {
NEW
46
      console.error('Error executing autocomplete before search:', e);
×
NEW
47
      await updateLocationIfNeeded(searchActions, [], geolocationOptions);
×
48
    }
49
    const verticalKey = searchActions.state.vertical.verticalKey ?? '';
14!
50
    const query = searchActions.state.query.input ?? '';
14✔
51
    onSearch
14✔
52
      ? onSearch({ verticalKey, query })
53
      : executeSearch(searchActions);
54
  }
55
  return [executeQuery, autocompletePromiseRef];
179✔
56
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc