2020-04-24 21:17:03 -07:00
|
|
|
import React from "react";
|
|
|
|
import gql from "graphql-tag";
|
2020-12-25 09:08:33 -08:00
|
|
|
import { Box, Text, VisuallyHidden } from "@chakra-ui/react";
|
2020-07-31 23:10:34 -07:00
|
|
|
import { useQuery } from "@apollo/client";
|
2020-04-24 21:17:03 -07:00
|
|
|
|
2020-09-01 19:40:53 -07:00
|
|
|
import { Delay, useDebounce } from "../util";
|
2021-01-18 15:56:24 -08:00
|
|
|
import { emptySearchQuery } from "./SearchToolbar";
|
2020-08-05 00:25:25 -07:00
|
|
|
import Item, { ItemListContainer, ItemListSkeleton } from "./Item";
|
2020-07-22 21:29:57 -07:00
|
|
|
import { itemAppearanceFragment } from "../components/useOutfitAppearance";
|
2020-04-24 21:17:03 -07:00
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
/**
|
|
|
|
* SearchPanel shows item search results to the user, so they can preview them
|
|
|
|
* and add them to their outfit!
|
|
|
|
*
|
|
|
|
* It's tightly coordinated with SearchToolbar, using refs to control special
|
|
|
|
* keyboard and focus interactions.
|
|
|
|
*/
|
2020-04-25 01:55:48 -07:00
|
|
|
function SearchPanel({
|
|
|
|
query,
|
|
|
|
outfitState,
|
|
|
|
dispatchToOutfit,
|
2020-04-26 00:37:58 -07:00
|
|
|
scrollContainerRef,
|
|
|
|
searchQueryRef,
|
2020-04-25 20:06:51 -07:00
|
|
|
firstSearchResultRef,
|
2020-04-25 01:55:48 -07:00
|
|
|
}) {
|
2020-04-26 00:37:58 -07:00
|
|
|
// Whenever the search query changes, scroll back up to the top!
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (scrollContainerRef.current) {
|
|
|
|
scrollContainerRef.current.scrollTop = 0;
|
|
|
|
}
|
|
|
|
}, [query, scrollContainerRef]);
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// Sometimes we want to give focus back to the search field!
|
2020-04-26 00:37:58 -07:00
|
|
|
const onMoveFocusUpToQuery = (e) => {
|
|
|
|
if (searchQueryRef.current) {
|
|
|
|
searchQueryRef.current.focus();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-24 21:17:03 -07:00
|
|
|
return (
|
2020-04-25 20:27:04 -07:00
|
|
|
<Box
|
|
|
|
onKeyDown={(e) => {
|
2020-04-26 01:42:24 -07:00
|
|
|
// This will catch any Escape presses when the user's focus is inside
|
|
|
|
// the SearchPanel.
|
2020-04-25 20:27:04 -07:00
|
|
|
if (e.key === "Escape") {
|
|
|
|
onMoveFocusUpToQuery(e);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2020-04-24 21:17:03 -07:00
|
|
|
<SearchResults
|
2020-09-02 00:53:35 -07:00
|
|
|
// When the query changes, replace the SearchResults component with a
|
|
|
|
// new instance, to reset `itemIdsToReconsider`. That way, if you find
|
|
|
|
// an item you like in one search, then immediately do a second search
|
|
|
|
// and try a conflicting item, we'll restore the item you liked from
|
|
|
|
// your first search!
|
|
|
|
//
|
|
|
|
// NOTE: I wonder how this affects things like state. This component
|
|
|
|
// also tries to gracefully handle changes in the query, but tbh
|
|
|
|
// I wonder whether that's still necessary...
|
|
|
|
key={serializeQuery(query)}
|
2020-04-24 21:17:03 -07:00
|
|
|
query={query}
|
|
|
|
outfitState={outfitState}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
2020-04-26 00:37:58 -07:00
|
|
|
scrollContainerRef={scrollContainerRef}
|
2020-04-25 20:06:51 -07:00
|
|
|
firstSearchResultRef={firstSearchResultRef}
|
|
|
|
onMoveFocusUpToQuery={onMoveFocusUpToQuery}
|
2020-04-24 21:17:03 -07:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
/**
|
|
|
|
* SearchResults loads the search results from the user's query, renders them,
|
|
|
|
* and tracks the scroll container for infinite scrolling.
|
|
|
|
*
|
|
|
|
* For each item, we render a <label> with a visually-hidden checkbox and the
|
|
|
|
* Item component (which will visually reflect the radio's state). This makes
|
|
|
|
* the list screen-reader- and keyboard-accessible!
|
|
|
|
*/
|
2020-04-25 20:06:51 -07:00
|
|
|
function SearchResults({
|
|
|
|
query,
|
|
|
|
outfitState,
|
|
|
|
dispatchToOutfit,
|
2020-04-26 00:37:58 -07:00
|
|
|
scrollContainerRef,
|
2020-04-25 20:06:51 -07:00
|
|
|
firstSearchResultRef,
|
|
|
|
onMoveFocusUpToQuery,
|
|
|
|
}) {
|
2020-04-26 01:42:24 -07:00
|
|
|
const { loading, loadingMore, error, items, fetchMore } = useSearchResults(
|
|
|
|
query,
|
|
|
|
outfitState
|
|
|
|
);
|
|
|
|
useScrollTracker(scrollContainerRef, 300, fetchMore);
|
|
|
|
|
2020-09-02 00:53:35 -07:00
|
|
|
// This will save the `wornItemIds` when the SearchResults first mounts, and
|
|
|
|
// keep it saved even after the outfit changes. We use this to try to restore
|
|
|
|
// these items after the user makes changes, e.g., after they try on another
|
|
|
|
// Background we want to restore the previous one!
|
|
|
|
const [itemIdsToReconsider] = React.useState(outfitState.wornItemIds);
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// You can use UpArrow/DownArrow to navigate between items, and even back up
|
|
|
|
// to the search field!
|
2021-04-26 07:14:29 -07:00
|
|
|
const goToPrevItem = React.useCallback(
|
|
|
|
(e) => {
|
|
|
|
const prevLabel = e.target.closest("label").previousSibling;
|
|
|
|
if (prevLabel) {
|
|
|
|
prevLabel.querySelector("input[type=checkbox]").focus();
|
|
|
|
prevLabel.scrollIntoView({ block: "center" });
|
|
|
|
e.preventDefault();
|
|
|
|
} else {
|
|
|
|
// If we're at the top of the list, move back up to the search box!
|
|
|
|
onMoveFocusUpToQuery(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[onMoveFocusUpToQuery]
|
|
|
|
);
|
|
|
|
const goToNextItem = React.useCallback((e) => {
|
2020-04-26 01:42:24 -07:00
|
|
|
const nextLabel = e.target.closest("label").nextSibling;
|
|
|
|
if (nextLabel) {
|
|
|
|
nextLabel.querySelector("input[type=checkbox]").focus();
|
|
|
|
nextLabel.scrollIntoView({ block: "center" });
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2021-04-26 07:14:29 -07:00
|
|
|
}, []);
|
2020-04-26 01:42:24 -07:00
|
|
|
|
|
|
|
// If the results aren't ready, we have some special case UI!
|
|
|
|
if (loading) {
|
|
|
|
return (
|
|
|
|
<Delay ms={500}>
|
|
|
|
<ItemListSkeleton count={8} />
|
|
|
|
</Delay>
|
|
|
|
);
|
|
|
|
} else if (error) {
|
|
|
|
return (
|
2020-08-12 00:37:31 -07:00
|
|
|
<Text>
|
2020-04-26 01:42:24 -07:00
|
|
|
We hit an error trying to load your search results{" "}
|
|
|
|
<span role="img" aria-label="(sweat emoji)">
|
|
|
|
😓
|
|
|
|
</span>{" "}
|
|
|
|
Try again?
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
} else if (items.length === 0) {
|
|
|
|
return (
|
2020-08-12 00:37:31 -07:00
|
|
|
<Text>
|
2020-04-26 01:42:24 -07:00
|
|
|
We couldn't find any matching items{" "}
|
|
|
|
<span role="img" aria-label="(thinking emoji)">
|
|
|
|
🤔
|
|
|
|
</span>{" "}
|
|
|
|
Try again?
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, render the item list, with checkboxes and Item components!
|
|
|
|
// We also render some extra skeleton items at the bottom during infinite
|
|
|
|
// scroll loading.
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ItemListContainer>
|
|
|
|
{items.map((item, index) => (
|
2021-04-26 07:14:29 -07:00
|
|
|
<SearchResultItem
|
|
|
|
key={item.id}
|
|
|
|
item={item}
|
|
|
|
itemIdsToReconsider={itemIdsToReconsider}
|
|
|
|
isWorn={outfitState.wornItemIds.includes(item.id)}
|
|
|
|
isInOutfit={outfitState.allItemIds.includes(item.id)}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
|
|
|
checkboxRef={index === 0 ? firstSearchResultRef : null}
|
|
|
|
goToPrevItem={goToPrevItem}
|
|
|
|
goToNextItem={goToNextItem}
|
|
|
|
/>
|
2020-04-26 01:42:24 -07:00
|
|
|
))}
|
|
|
|
</ItemListContainer>
|
|
|
|
{loadingMore && <ItemListSkeleton count={8} />}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-26 07:14:29 -07:00
|
|
|
function SearchResultItem({
|
|
|
|
item,
|
|
|
|
itemIdsToReconsider,
|
|
|
|
isWorn,
|
|
|
|
isInOutfit,
|
|
|
|
dispatchToOutfit,
|
|
|
|
checkboxRef,
|
|
|
|
goToPrevItem,
|
|
|
|
goToNextItem,
|
|
|
|
}) {
|
|
|
|
// It's important to use `useCallback` for `onRemove`, to avoid re-rendering
|
|
|
|
// the whole list of <Item>s!
|
|
|
|
const onRemove = React.useCallback(
|
|
|
|
() =>
|
|
|
|
dispatchToOutfit({
|
|
|
|
type: "removeItem",
|
|
|
|
itemId: item.id,
|
|
|
|
itemIdsToReconsider,
|
|
|
|
}),
|
|
|
|
[item.id, itemIdsToReconsider, dispatchToOutfit]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<label>
|
|
|
|
<VisuallyHidden
|
|
|
|
as="input"
|
|
|
|
type="checkbox"
|
|
|
|
aria-label={`Wear "${item.name}"`}
|
|
|
|
value={item.id}
|
|
|
|
checked={isWorn}
|
|
|
|
ref={checkboxRef}
|
|
|
|
onChange={(e) => {
|
|
|
|
const itemId = e.target.value;
|
|
|
|
const willBeWorn = e.target.checked;
|
|
|
|
if (willBeWorn) {
|
|
|
|
dispatchToOutfit({ type: "wearItem", itemId, itemIdsToReconsider });
|
|
|
|
} else {
|
|
|
|
dispatchToOutfit({
|
|
|
|
type: "unwearItem",
|
|
|
|
itemId,
|
|
|
|
itemIdsToReconsider,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
e.target.click();
|
|
|
|
} else if (e.key === "ArrowUp") {
|
|
|
|
goToPrevItem(e);
|
|
|
|
} else if (e.key === "ArrowDown") {
|
|
|
|
goToNextItem(e);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Item
|
|
|
|
item={item}
|
|
|
|
isWorn={isWorn}
|
|
|
|
isInOutfit={isInOutfit}
|
|
|
|
onRemove={onRemove}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
/**
|
|
|
|
* useSearchResults manages the actual querying and state management of search!
|
|
|
|
* It's hefty, infinite-scroll pagination is a bit of a thing!
|
|
|
|
*/
|
|
|
|
function useSearchResults(query, outfitState) {
|
2020-04-25 00:43:01 -07:00
|
|
|
const { speciesId, colorId } = outfitState;
|
2020-04-26 01:42:24 -07:00
|
|
|
const [isEndOfResults, setIsEndOfResults] = React.useState(false);
|
2020-04-24 21:17:03 -07:00
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// We debounce the search query, so that we don't resend a new query whenever
|
|
|
|
// the user types anything.
|
2020-09-01 19:53:38 -07:00
|
|
|
const debouncedQuery = useDebounce(query, 300, {
|
|
|
|
waitForFirstPause: true,
|
2021-01-18 15:56:24 -08:00
|
|
|
initialValue: emptySearchQuery,
|
2020-09-01 19:53:38 -07:00
|
|
|
});
|
2020-04-24 21:17:03 -07:00
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// When the query changes, we should update our impression of whether we've
|
|
|
|
// reached the end!
|
2020-04-25 02:18:03 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
setIsEndOfResults(false);
|
|
|
|
}, [query]);
|
|
|
|
|
2020-09-01 20:06:54 -07:00
|
|
|
// NOTE: This query should always load ~instantly, from the client cache.
|
|
|
|
const { data: zoneData } = useQuery(gql`
|
|
|
|
query SearchPanelZones {
|
|
|
|
allZones {
|
|
|
|
id
|
|
|
|
label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
const allZones = zoneData?.allZones || [];
|
|
|
|
const filterToZones = query.filterToZoneLabel
|
|
|
|
? allZones.filter((z) => z.label === query.filterToZoneLabel)
|
|
|
|
: [];
|
|
|
|
const filterToZoneIds = filterToZones.map((z) => z.id);
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// Here's the actual GQL query! At the bottom we have more config than usual!
|
|
|
|
const {
|
|
|
|
loading: loadingGQL,
|
|
|
|
error,
|
|
|
|
data,
|
|
|
|
fetchMore: fetchMoreGQL,
|
|
|
|
} = useQuery(
|
2020-04-24 21:17:03 -07:00
|
|
|
gql`
|
2020-05-19 14:48:54 -07:00
|
|
|
query SearchPanel(
|
|
|
|
$query: String!
|
2021-02-04 23:34:43 -08:00
|
|
|
$fitsPet: FitsPetSearchFilter
|
2020-11-08 15:04:17 -08:00
|
|
|
$itemKind: ItemKindSearchFilter
|
2021-01-21 15:58:24 -08:00
|
|
|
$currentUserOwnsOrWants: OwnsOrWants
|
2020-09-01 20:06:54 -07:00
|
|
|
$zoneIds: [ID!]!
|
2020-11-08 15:04:17 -08:00
|
|
|
$speciesId: ID!
|
2020-05-19 14:48:54 -07:00
|
|
|
$colorId: ID!
|
|
|
|
$offset: Int!
|
|
|
|
) {
|
2021-02-04 23:34:43 -08:00
|
|
|
itemSearch(
|
2020-04-25 00:43:01 -07:00
|
|
|
query: $query
|
2021-02-04 23:34:43 -08:00
|
|
|
fitsPet: $fitsPet
|
2020-11-08 15:04:17 -08:00
|
|
|
itemKind: $itemKind
|
2021-01-21 15:58:24 -08:00
|
|
|
currentUserOwnsOrWants: $currentUserOwnsOrWants
|
2020-09-01 20:06:54 -07:00
|
|
|
zoneIds: $zoneIds
|
2020-04-25 01:55:48 -07:00
|
|
|
offset: $offset
|
|
|
|
limit: 50
|
2020-04-25 00:43:01 -07:00
|
|
|
) {
|
2020-04-25 01:55:48 -07:00
|
|
|
query
|
2020-09-01 20:30:18 -07:00
|
|
|
zones {
|
|
|
|
id
|
|
|
|
}
|
2020-04-25 01:55:48 -07:00
|
|
|
items {
|
|
|
|
# TODO: De-dupe this from useOutfitState?
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
2020-09-01 01:18:24 -07:00
|
|
|
isNc
|
2020-11-08 15:13:30 -08:00
|
|
|
isPb
|
2020-09-12 20:02:56 -07:00
|
|
|
currentUserOwnsThis
|
|
|
|
currentUserWantsThis
|
2020-04-25 01:55:48 -07:00
|
|
|
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
|
|
|
# This enables us to quickly show the item when the user clicks it!
|
2020-05-02 17:22:46 -07:00
|
|
|
...ItemAppearanceForOutfitPreview
|
2020-04-25 01:55:48 -07:00
|
|
|
|
|
|
|
# This is used to group items by zone, and to detect conflicts when
|
|
|
|
# wearing a new item.
|
|
|
|
layers {
|
|
|
|
zone {
|
|
|
|
id
|
2020-08-19 17:50:05 -07:00
|
|
|
label @client
|
2020-04-25 01:55:48 -07:00
|
|
|
}
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
2020-09-01 01:18:24 -07:00
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
label @client
|
|
|
|
isCommonlyUsedByItems @client
|
|
|
|
}
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${itemAppearanceFragment}
|
|
|
|
`,
|
|
|
|
{
|
2020-09-01 20:06:54 -07:00
|
|
|
variables: {
|
|
|
|
query: debouncedQuery.value,
|
2021-02-04 23:34:43 -08:00
|
|
|
fitsPet: { speciesId, colorId },
|
2020-11-08 15:04:17 -08:00
|
|
|
itemKind: debouncedQuery.filterToItemKind,
|
2021-01-21 15:58:24 -08:00
|
|
|
currentUserOwnsOrWants: debouncedQuery.filterToCurrentUserOwnsOrWants,
|
2020-09-01 20:06:54 -07:00
|
|
|
zoneIds: filterToZoneIds,
|
2021-02-04 23:34:43 -08:00
|
|
|
offset: 0,
|
2020-09-01 20:06:54 -07:00
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
},
|
2021-01-21 14:57:21 -08:00
|
|
|
context: { sendAuth: true },
|
2020-11-08 15:04:17 -08:00
|
|
|
skip:
|
|
|
|
!debouncedQuery.value &&
|
|
|
|
!debouncedQuery.filterToItemKind &&
|
2021-01-21 15:58:24 -08:00
|
|
|
!debouncedQuery.filterToZoneLabel &&
|
|
|
|
!debouncedQuery.filterToCurrentUserOwnsOrWants,
|
2020-04-25 01:55:48 -07:00
|
|
|
notifyOnNetworkStatusChange: true,
|
2020-04-25 02:18:03 -07:00
|
|
|
onCompleted: (d) => {
|
2020-04-26 01:42:24 -07:00
|
|
|
// This is called each time the query completes, including on
|
|
|
|
// `fetchMore`, with the extended results. But, on the first time, this
|
|
|
|
// logic can tell us whether we're at the end of the list, by counting
|
|
|
|
// whether there was <30. We also have to check in `fetchMore`!
|
2021-02-04 23:34:43 -08:00
|
|
|
const items = d && d.itemSearch && d.itemSearch.items;
|
2020-04-25 02:18:03 -07:00
|
|
|
if (items && items.length < 30) {
|
|
|
|
setIsEndOfResults(true);
|
|
|
|
}
|
|
|
|
},
|
2021-01-17 04:47:11 -08:00
|
|
|
onError: (e) => {
|
|
|
|
console.error("Error loading search results", e);
|
|
|
|
},
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// Smooth over the data a bit, so that we can use key fields with confidence!
|
2021-02-04 23:34:43 -08:00
|
|
|
const result = data?.itemSearch;
|
2020-09-01 20:30:18 -07:00
|
|
|
const resultValue = result?.query;
|
|
|
|
const zoneStr = filterToZoneIds.sort().join(",");
|
|
|
|
const resultZoneStr = (result?.zones || [])
|
|
|
|
.map((z) => z.id)
|
|
|
|
.sort()
|
|
|
|
.join(",");
|
|
|
|
const queriesMatch = resultValue === query.value && resultZoneStr === zoneStr;
|
|
|
|
const items = result?.items || [];
|
2020-04-25 01:55:48 -07:00
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// Okay, what kind of loading state is this?
|
|
|
|
let loading;
|
|
|
|
let loadingMore;
|
2020-09-01 20:30:18 -07:00
|
|
|
if (loadingGQL && items.length > 0 && queriesMatch) {
|
2020-08-19 18:11:40 -07:00
|
|
|
// If we already have items for this query, but we're also loading GQL,
|
|
|
|
// then we're `loadingMore`.
|
2020-04-26 01:42:24 -07:00
|
|
|
loading = false;
|
|
|
|
loadingMore = true;
|
2020-08-19 18:11:40 -07:00
|
|
|
} else if (loadingGQL || query !== debouncedQuery) {
|
|
|
|
// Otherwise, if we're loading GQL or the user has changed the query, we're
|
|
|
|
// just `loading`.
|
|
|
|
loading = true;
|
|
|
|
loadingMore = false;
|
2020-04-26 01:42:24 -07:00
|
|
|
} else {
|
|
|
|
// Otherwise, we're not loading at all!
|
|
|
|
loading = false;
|
|
|
|
loadingMore = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When SearchResults calls this, we'll resend the query, with the `offset`
|
|
|
|
// increased. We'll append the results to the original query!
|
|
|
|
const fetchMore = React.useCallback(() => {
|
2021-02-01 19:06:19 -08:00
|
|
|
if (!loadingGQL && !error && !isEndOfResults) {
|
2020-04-26 01:42:24 -07:00
|
|
|
fetchMoreGQL({
|
2020-04-25 01:55:48 -07:00
|
|
|
variables: {
|
|
|
|
offset: items.length,
|
|
|
|
},
|
|
|
|
updateQuery: (prev, { fetchMoreResult }) => {
|
2020-04-25 02:18:03 -07:00
|
|
|
// Note: This is a bit awkward because, if the results count ends on
|
|
|
|
// a multiple of 30, the user will see a flash of loading before
|
|
|
|
// getting told it's actually the end. Ah well :/
|
|
|
|
//
|
|
|
|
// We could maybe make this more rigorous later with
|
|
|
|
// react-virtualized to have a better scrollbar anyway, and then
|
|
|
|
// we'd need to return the total result count... a bit annoying to
|
|
|
|
// potentially double the query runtime? We'd need to see how slow it
|
|
|
|
// actually makes things.
|
2021-02-04 23:34:43 -08:00
|
|
|
if (fetchMoreResult.itemSearch.items.length < 30) {
|
2020-04-25 02:18:03 -07:00
|
|
|
setIsEndOfResults(true);
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:55:48 -07:00
|
|
|
return {
|
|
|
|
...prev,
|
2021-02-04 23:34:43 -08:00
|
|
|
itemSearch: {
|
|
|
|
...(prev?.itemSearch || {}),
|
2020-04-25 01:55:48 -07:00
|
|
|
items: [
|
2021-02-04 23:34:43 -08:00
|
|
|
...(prev?.itemSearch?.items || []),
|
|
|
|
...(fetchMoreResult?.itemSearch?.items || []),
|
2020-04-25 01:55:48 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2021-02-01 19:06:19 -08:00
|
|
|
}).catch((e) => {
|
|
|
|
console.error("Error loading more search results pages", e);
|
2020-04-25 01:55:48 -07:00
|
|
|
});
|
|
|
|
}
|
2021-02-01 19:06:19 -08:00
|
|
|
}, [loadingGQL, error, isEndOfResults, fetchMoreGQL, items.length]);
|
2020-04-25 01:55:48 -07:00
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
return { loading, loadingMore, error, items, fetchMore };
|
2020-04-25 01:55:48 -07:00
|
|
|
}
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
/**
|
|
|
|
* useScrollTracker watches for the given scroll container to scroll near the
|
|
|
|
* bottom, then fires a callback. We use this to fetch more search results!
|
|
|
|
*/
|
|
|
|
function useScrollTracker(scrollContainerRef, threshold, onScrolledToBottom) {
|
2020-04-25 01:55:48 -07:00
|
|
|
const onScroll = React.useCallback(
|
|
|
|
(e) => {
|
|
|
|
const topEdgeScrollPosition = e.target.scrollTop;
|
|
|
|
const bottomEdgeScrollPosition =
|
|
|
|
topEdgeScrollPosition + e.target.clientHeight;
|
|
|
|
const remainingScrollDistance =
|
|
|
|
e.target.scrollHeight - bottomEdgeScrollPosition;
|
|
|
|
if (remainingScrollDistance < threshold) {
|
|
|
|
onScrolledToBottom();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[onScrolledToBottom, threshold]
|
2020-04-24 21:17:03 -07:00
|
|
|
);
|
2020-04-25 01:55:48 -07:00
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
2020-04-26 00:37:58 -07:00
|
|
|
const scrollContainer = scrollContainerRef.current;
|
|
|
|
|
|
|
|
if (!scrollContainer) {
|
2020-04-25 01:55:48 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-26 00:37:58 -07:00
|
|
|
scrollContainer.addEventListener("scroll", onScroll);
|
2020-04-25 01:55:48 -07:00
|
|
|
|
|
|
|
return () => {
|
2020-04-26 00:37:58 -07:00
|
|
|
if (scrollContainer) {
|
|
|
|
scrollContainer.removeEventListener("scroll", onScroll);
|
2020-04-25 01:55:48 -07:00
|
|
|
}
|
|
|
|
};
|
2020-04-26 00:37:58 -07:00
|
|
|
}, [onScroll, scrollContainerRef]);
|
2020-04-25 20:44:59 -07:00
|
|
|
}
|
|
|
|
|
2020-09-02 00:53:35 -07:00
|
|
|
/**
|
|
|
|
* serializeQuery stably converts a search query object to a string, for easier
|
|
|
|
* JS comparison.
|
|
|
|
*/
|
|
|
|
function serializeQuery(query) {
|
2020-11-08 15:04:17 -08:00
|
|
|
return `${JSON.stringify([
|
|
|
|
query.value,
|
|
|
|
query.filterToItemKind,
|
|
|
|
query.filterToZoneLabel,
|
2021-01-21 15:58:24 -08:00
|
|
|
query.filterToCurrentUserOwnsOrWants,
|
2020-11-08 15:04:17 -08:00
|
|
|
])}`;
|
2020-09-02 00:53:35 -07:00
|
|
|
}
|
|
|
|
|
2020-04-24 21:17:03 -07:00
|
|
|
export default SearchPanel;
|