File size: 3,122 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
import { useEffect, useRef } from 'react';

import { dequal } from './dequal';
import { useInstantSearchContext } from './useInstantSearchContext';
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';

import type { Widget } from 'instantsearch.js';
import type { IndexWidget } from 'instantsearch.js/es/widgets/index/index';

export function useWidget<TWidget extends Widget | IndexWidget, TProps>({
  widget,
  parentIndex,
  props,
  shouldSsr,
}: {
  widget: TWidget;
  parentIndex: IndexWidget;
  props: TProps;
  shouldSsr: boolean;
}) {
  const prevPropsRef = useRef<TProps>(props);
  useEffect(() => {
    prevPropsRef.current = props;
  }, [props]);

  const prevWidgetRef = useRef<TWidget>(widget);
  useEffect(() => {
    prevWidgetRef.current = widget;
  }, [widget]);

  const cleanupTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  const shouldAddWidgetEarly =
    shouldSsr && !parentIndex.getWidgets().includes(widget);

  const search = useInstantSearchContext();

  // This effect is responsible for adding, removing, and updating the widget.
  // We need to support scenarios where the widget is remounted quickly, like in
  // Strict Mode, so that we don't lose its state, and therefore that we don't
  // break routing.
  useIsomorphicLayoutEffect(() => {
    const previousWidget = prevWidgetRef.current;
    function cleanup() {
      if (search._preventWidgetCleanup) return;
      parentIndex.removeWidgets([previousWidget]);
    }

    // Scenario 1: the widget is added for the first time.
    if (cleanupTimerRef.current === null) {
      if (!shouldAddWidgetEarly) {
        parentIndex.addWidgets([widget]);
      }
    }
    // Scenario 2: the widget is rerendered or updated.
    else {
      // We cancel the original effect cleanup because it may not be necessary if
      // props haven't changed. (We manually call it if it is below.)
      clearTimeout(cleanupTimerRef.current);

      // Warning: if an unstable function prop is provided, `dequal` is not able
      // to keep its reference and therefore will consider that props did change.
      // This could unsollicitely remove/add the widget, therefore forget its state,
      // and could be a source of confusion.
      // If users face this issue, we should advise them to provide stable function
      // references.
      const arePropsEqual = dequal(props, prevPropsRef.current);

      // If props did change, then we execute the cleanup function instantly
      // and then add the widget back. This lets us add the widget without
      // waiting for the scheduled cleanup function to finish (that we canceled
      // above).
      if (!arePropsEqual) {
        cleanup();
        parentIndex.addWidgets([widget]);
      }
    }

    return () => {
      // We don't remove the widget right away, but rather schedule it so that
      // we're able to cancel it in the next effect.
      cleanupTimerRef.current = setTimeout(cleanup);
    };
  }, [parentIndex, widget, shouldAddWidgetEarly, search, props]);

  if (shouldAddWidgetEarly) {
    parentIndex.addWidgets([widget]);
  }
}