Skip to content

Commit d270310

Browse files
authored
Merge pull request #448 from performant-software/feature/cdp68_bounding_box
CDP #68 - Bounding box
2 parents ca5dee2 + aa0078a commit d270310

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

packages/core-data/src/components/PersistentSearchStateContextProvider.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ type Props = {
1313

1414
const PersistentSearchStateContextProvider = (props: Props) => {
1515
const { geoSearch, infiniteHits, searchBox } = props;
16-
const { cachedHits, observe, unobserve } = useProgressiveSearch(infiniteHits);
16+
const {
17+
cachedHits,
18+
observe,
19+
unobserve,
20+
searching
21+
} = useProgressiveSearch(infiniteHits);
1722

1823
/**
1924
* Memoizes the context value.
@@ -31,8 +36,9 @@ const PersistentSearchStateContextProvider = (props: Props) => {
3136
geoSearch,
3237
searchBox,
3338
observe,
34-
unobserve
35-
}), [cachedHits, geoSearch, searchBox, observe, unobserve]);
39+
unobserve,
40+
searching
41+
}), [cachedHits, geoSearch, searchBox, observe, unobserve, searching]);
3642

3743
return (
3844
<PersistentSearchStateContext.Provider

packages/core-data/src/components/SearchResultsLayer.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useMap } from '@peripleo/maplibre';
55
import React, { useEffect, useMemo, useState } from 'react';
66
import _ from 'underscore';
77
import TypesenseUtils from '../utils/Typesense';
8-
import { useCachedHits, useSearchCompleted } from '../hooks/Typesense';
8+
import { useCachedHits, useSearching } from '../hooks/Typesense';
99

1010
type Props = {
1111
/**
@@ -32,17 +32,17 @@ type Props = {
3232
* Path to the geometry attribute for each result.
3333
*/
3434
geometry: string,
35-
}
35+
};
3636

3737
/**
3838
* This component renders a map layer for the search results from a Typesense search index.
3939
*/
4040
const SearchResultsLayer = (props: Props) => {
4141
const [mapLoaded, setMapLoaded] = useState(false);
42-
const [searchCompleted, setSearchCompleted] = useState(false);
4342

4443
const hits = useCachedHits();
4544
const map = useMap();
45+
const isSearching = useSearching();
4646

4747
/**
4848
* Memo-ize the Typesense hits as a feature collection.
@@ -60,24 +60,21 @@ const SearchResultsLayer = (props: Props) => {
6060
const boundingBoxDependencies = [
6161
data,
6262
mapLoaded,
63-
searchCompleted,
63+
isSearching,
6464
props.boundingBoxData,
6565
props.boundingBoxOptions,
6666
props.buffer,
6767
props.fitBoundingBox
6868
];
6969

7070
useEffect(() => {
71-
if (props.fitBoundingBox && data && mapLoaded && searchCompleted) {
71+
if (props.fitBoundingBox && data && mapLoaded && !isSearching) {
7272
// Set the bounding box on the map
7373
const bbox = MapUtils.getBoundingBox(data, props.buffer);
7474

7575
if (bbox) {
7676
map.fitBounds(bbox, props.boundingBoxOptions, props.boundingBoxData);
7777
}
78-
79-
// Reset search completed
80-
setSearchCompleted(false);
8178
}
8279
}, boundingBoxDependencies);
8380

@@ -97,11 +94,6 @@ const SearchResultsLayer = (props: Props) => {
9794
};
9895
}, [map]);
9996

100-
/**
101-
* Sets the search completed state to true.
102-
*/
103-
useSearchCompleted(() => setSearchCompleted(true), []);
104-
10597
if (!data) {
10698
return null;
10799
}

packages/core-data/src/hooks/ProgressiveSearch.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type OnCompleteCallback = (results: Array<SearchResult>) => void;
1414

1515
const useProgressiveSearch = (infiniteHits, transformResults = null) => {
1616
const [cachedHits, setCachedHits] = useState(TypesenseUtils.createCachedHits([]));
17+
const [searching, setSearching] = useState(false);
1718

1819
const lastSearchState = useRef<any>();
1920
const callbacks = useRef<Array<OnCompleteCallback>>([]);
@@ -77,11 +78,13 @@ const useProgressiveSearch = (infiniteHits, transformResults = null) => {
7778
};
7879

7980
useEffect(() => {
80-
const { results } = infiniteHits;
81-
82-
const isFirstPage = results.page === 0;
81+
const { isFirstPage, results } = infiniteHits;
8382
const hits = getHits(results);
8483

84+
if (isFirstPage) {
85+
setSearching(true);
86+
}
87+
8588
// Add to cache and load next page
8689
if (isFirstPage && hasStateChanged(results._state, lastSearchState.current, true)) {
8790
setCachedHits(() => TypesenseUtils.createCachedHits(hits));
@@ -91,11 +94,9 @@ const useProgressiveSearch = (infiniteHits, transformResults = null) => {
9194
}, [infiniteHits.results]);
9295

9396
useEffect(() => {
94-
const { results } = infiniteHits;
97+
const { isLastPage, results } = infiniteHits;
9598
const hits = getHits(results);
9699

97-
const isLastPage = results.page + 1 >= results.nbPages;
98-
99100
if (!isLastPage && infiniteHits.showMore) {
100101
setTimeout(() => infiniteHits.showMore(), 25);
101102
} else if (hasStateChanged(results._state, lastSearchState.current)) {
@@ -105,10 +106,19 @@ const useProgressiveSearch = (infiniteHits, transformResults = null) => {
105106
});
106107
}
107108

109+
if (isLastPage) {
110+
setSearching(false);
111+
}
112+
108113
lastSearchState.current = results._state;
109114
}, [cachedHits]);
110115

111-
return { cachedHits: cachedHits.hits, observe, unobserve };
116+
return {
117+
cachedHits: cachedHits.hits,
118+
observe,
119+
unobserve,
120+
searching
121+
};
112122
};
113123

114124
export default useProgressiveSearch;

packages/core-data/src/hooks/Typesense.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export const useSearchCompleted = (callback: ((hits: TypesenseSearchResult[]) =>
3636
}, deps);
3737
};
3838

39+
export const useSearching = () => {
40+
const { searching } = useContext(PersistentSearchStateContext);
41+
return searching;
42+
};
43+
3944
export const useGeoSearchToggle = () => {
4045
const { clearMapRefinement, isRefinedWithMap, refine } = useGeoSearch();
4146

packages/core-data/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export {
7777
useGeoSearch,
7878
useGeoSearchToggle,
7979
useSearchBox,
80-
useSearchCompleted
80+
useSearchCompleted,
81+
useSearching
8182
} from './hooks/Typesense';
8283

8384
// Services

0 commit comments

Comments
 (0)