1 | import { useRef, useState } from 'react';
|
2 | import Supercluster from 'supercluster';
|
3 | import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect';
|
4 | import { dequal } from 'dequal';
|
5 |
|
6 | var useSupercluster = function useSupercluster(_ref) {
|
7 | var points = _ref.points,
|
8 | bounds = _ref.bounds,
|
9 | zoom = _ref.zoom,
|
10 | options = _ref.options;
|
11 | var superclusterRef = useRef();
|
12 | var pointsRef = useRef();
|
13 |
|
14 | var _useState = useState([]),
|
15 | clusters = _useState[0],
|
16 | setClusters = _useState[1];
|
17 |
|
18 | var zoomInt = Math.round(zoom);
|
19 | useDeepCompareEffectNoCheck(function () {
|
20 | if (!superclusterRef.current || !dequal(pointsRef.current, points)) {
|
21 | superclusterRef.current = new Supercluster(options);
|
22 | superclusterRef.current.load(points);
|
23 | }
|
24 |
|
25 | if (bounds) {
|
26 | setClusters(superclusterRef.current.getClusters(bounds, zoomInt));
|
27 | }
|
28 |
|
29 | pointsRef.current = points;
|
30 | }, [points, bounds, zoomInt]);
|
31 | return {
|
32 | clusters: clusters,
|
33 | supercluster: superclusterRef.current
|
34 | };
|
35 | };
|
36 |
|
37 | export default useSupercluster;
|
38 |
|