File size: 2,711 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React, { useState, useEffect, useMemo, useContext } from 'react';
import { instantSearchContext } from 'react-instantsearch-core';
import { createConcurrentSafePromise } from '../lib/createConcurrentSafePromise';
import { debounce } from '../lib/debounce';

function hasReactHooks() {
  // >= 16.8.0
  const [major, minor] = React.version.split('.').map(Number);
  return major >= 17 || (major === 16 && minor >= 8);
}

export default function useAnswers({
  searchClient,
  queryLanguages,
  attributesForPrediction,
  nbHits,
  renderDebounceTime = 100,
  searchDebounceTime = 100,
  ...extraParameters
}) {
  if (!hasReactHooks()) {
    throw new Error(
      `\`Answers\` component and \`useAnswers\` hook require all React packages to be 16.8.0 or higher.`
    );
  }
  const context = useContext(instantSearchContext);
  const [query, setQuery] = useState(context.store.getState().widgets.query);
  const [index, setIndex] = useState(context.mainTargetedIndex);
  const [isLoading, setIsLoading] = useState(false);
  const [hits, setHits] = useState([]);
  const runConcurrentSafePromise = useMemo(
    () => createConcurrentSafePromise(),
    []
  );
  const searchIndex = useMemo(
    () => searchClient.initIndex(index),
    [searchClient, index]
  );
  if (!searchIndex.findAnswers) {
    throw new Error(
      '`Answers` component and `useAnswers` hook require `algoliasearch` to be 4.8.0 or higher.'
    );
  }

  const debouncedSearch = useMemo(() => {
    return debounce(searchIndex.findAnswers, searchDebounceTime);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [searchIndex]);

  useEffect(() => {
    setIndex(context.mainTargetedIndex);

    return context.store.subscribe(() => {
      const { widgets } = context.store.getState();
      setQuery(widgets.query);
    });
  }, [context]);

  const setDebouncedResult = useMemo(
    () =>
      debounce((result) => {
        setIsLoading(false);
        setHits(result.hits);
      }, renderDebounceTime),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [setIsLoading, setHits]
  );

  const fetchAnswers = () => {
    if (!query) {
      setIsLoading(false);
      setHits([]);
      return;
    }
    setIsLoading(true);
    runConcurrentSafePromise(
      debouncedSearch(query, queryLanguages, {
        ...extraParameters,
        nbHits,
        attributesForPrediction,
      })
    ).then((result) => {
      if (!result) {
        // It's undefined when it's debounced.
        return;
      }
      setDebouncedResult(result);
    });
  };

  useEffect(() => {
    fetchAnswers();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [query]);

  return { hits, isLoading };
}