2020-04-24 21:17:03 -07:00
|
|
|
import React from "react";
|
|
|
|
import gql from "graphql-tag";
|
2022-10-14 19:19:56 -07:00
|
|
|
import { Box, Text, useColorModeValue, 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
|
|
|
|
2022-10-14 19:19:56 -07:00
|
|
|
import { 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";
|
2022-10-14 19:19:56 -07:00
|
|
|
import PaginationToolbar from "../components/PaginationToolbar";
|
|
|
|
|
|
|
|
const SEARCH_PER_PAGE = 30;
|
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
|
|
|
}) {
|
2022-10-14 19:33:45 -07:00
|
|
|
const scrollToTop = React.useCallback(() => {
|
2020-04-26 00:37:58 -07:00
|
|
|
if (scrollContainerRef.current) {
|
|
|
|
scrollContainerRef.current.scrollTop = 0;
|
|
|
|
}
|
2022-10-14 19:33:45 -07:00
|
|
|
}, [scrollContainerRef]);
|
2020-04-26 00:37:58 -07:00
|
|
|
|
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
|
2022-10-14 19:19:56 -07:00
|
|
|
// new instance. This resets both `currentPageNumber`, to take us back
|
|
|
|
// to page 1; and also `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!
|
2020-09-02 00:53:35 -07:00
|
|
|
key={serializeQuery(query)}
|
2020-04-24 21:17:03 -07:00
|
|
|
query={query}
|
|
|
|
outfitState={outfitState}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
2020-04-25 20:06:51 -07:00
|
|
|
firstSearchResultRef={firstSearchResultRef}
|
2022-10-14 19:33:45 -07:00
|
|
|
scrollToTop={scrollToTop}
|
2020-04-25 20:06:51 -07:00
|
|
|
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,
|
|
|
|
firstSearchResultRef,
|
2022-10-14 19:33:45 -07:00
|
|
|
scrollToTop,
|
2020-04-25 20:06:51 -07:00
|
|
|
onMoveFocusUpToQuery,
|
|
|
|
}) {
|
2022-10-14 19:19:56 -07:00
|
|
|
const [currentPageNumber, setCurrentPageNumber] = React.useState(1);
|
|
|
|
const { loading, error, items, numTotalPages } = useSearchResults(
|
2020-04-26 01:42:24 -07:00
|
|
|
query,
|
2022-10-14 19:19:56 -07:00
|
|
|
outfitState,
|
|
|
|
currentPageNumber
|
2020-04-26 01:42:24 -07:00
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
|
2022-10-14 19:33:45 -07:00
|
|
|
// Whenever the page number changes, scroll back to the top!
|
|
|
|
React.useEffect(() => scrollToTop(), [currentPageNumber, scrollToTop]);
|
|
|
|
|
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
|
|
|
|
2022-10-14 19:19:56 -07:00
|
|
|
const searchPanelBackground = useColorModeValue("white", "gray.900");
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// If the results aren't ready, we have some special case UI!
|
2022-10-14 19:19:56 -07:00
|
|
|
if (error) {
|
2020-04-26 01:42:24 -07:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 (
|
2022-10-14 19:19:56 -07:00
|
|
|
<Box>
|
|
|
|
<Box
|
|
|
|
position="sticky"
|
|
|
|
top="0"
|
|
|
|
background={searchPanelBackground}
|
|
|
|
zIndex="2"
|
|
|
|
paddingX="5"
|
|
|
|
paddingBottom="2"
|
|
|
|
paddingTop="1"
|
|
|
|
>
|
|
|
|
<PaginationToolbar
|
|
|
|
numTotalPages={numTotalPages}
|
|
|
|
currentPageNumber={currentPageNumber}
|
|
|
|
goToPageNumber={setCurrentPageNumber}
|
|
|
|
buildPageUrl={() => null}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
<ItemListContainer paddingX="4" paddingBottom="2">
|
2020-04-26 01:42:24 -07:00
|
|
|
{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>
|
2022-10-14 19:19:56 -07:00
|
|
|
{loading && (
|
|
|
|
<ItemListSkeleton
|
|
|
|
count={SEARCH_PER_PAGE}
|
|
|
|
paddingX="4"
|
|
|
|
paddingBottom="2"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{!loading && items.length === 0 && (
|
|
|
|
<Text paddingX="4">
|
|
|
|
We couldn't find any matching items{" "}
|
|
|
|
<span role="img" aria-label="(thinking emoji)">
|
|
|
|
🤔
|
|
|
|
</span>{" "}
|
|
|
|
Try again?
|
|
|
|
</Text>
|
|
|
|
)}
|
|
|
|
</Box>
|
2020-04-26 01:42:24 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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!
|
|
|
|
*/
|
2022-10-14 19:19:56 -07:00
|
|
|
function useSearchResults(
|
|
|
|
query,
|
|
|
|
outfitState,
|
|
|
|
currentPageNumber,
|
|
|
|
{ skip = false } = {}
|
|
|
|
) {
|
2020-04-25 00:43:01 -07:00
|
|
|
const { speciesId, colorId } = outfitState;
|
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-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);
|
|
|
|
|
2022-10-14 19:19:56 -07:00
|
|
|
const currentPageIndex = currentPageNumber - 1;
|
|
|
|
const offset = currentPageIndex * SEARCH_PER_PAGE;
|
|
|
|
|
2020-04-26 01:42:24 -07:00
|
|
|
// Here's the actual GQL query! At the bottom we have more config than usual!
|
2022-10-14 19:19:56 -07:00
|
|
|
const { loading: loadingGQL, error, data } = 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!
|
2022-10-14 19:19:56 -07:00
|
|
|
$perPage: Int!
|
2020-05-19 14:48:54 -07:00
|
|
|
) {
|
2021-06-21 10:37:54 -07:00
|
|
|
itemSearch: itemSearchV2(
|
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 00:43:01 -07:00
|
|
|
) {
|
2022-10-14 19:19:56 -07:00
|
|
|
numTotalItems
|
|
|
|
items(offset: $offset, limit: $perPage) {
|
2020-04-25 01:55:48 -07:00
|
|
|
# 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,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
2022-10-14 19:19:56 -07:00
|
|
|
offset,
|
|
|
|
perPage: SEARCH_PER_PAGE,
|
2020-09-01 20:06:54 -07:00
|
|
|
},
|
2021-01-21 14:57:21 -08:00
|
|
|
context: { sendAuth: true },
|
2020-11-08 15:04:17 -08:00
|
|
|
skip:
|
2022-10-14 19:19:56 -07:00
|
|
|
skip ||
|
|
|
|
(!debouncedQuery.value &&
|
|
|
|
!debouncedQuery.filterToItemKind &&
|
|
|
|
!debouncedQuery.filterToZoneLabel &&
|
|
|
|
!debouncedQuery.filterToCurrentUserOwnsOrWants),
|
2021-01-17 04:47:11 -08:00
|
|
|
onError: (e) => {
|
|
|
|
console.error("Error loading search results", e);
|
|
|
|
},
|
2022-10-14 19:19:56 -07:00
|
|
|
// Return `numTotalItems` from the GQL cache while waiting for next page!
|
|
|
|
returnPartialData: true,
|
2020-04-24 21:17:03 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-10-14 19:19:56 -07:00
|
|
|
const loading = debouncedQuery !== query || loadingGQL;
|
|
|
|
const items = data?.itemSearch?.items ?? [];
|
|
|
|
const numTotalItems = data?.itemSearch?.numTotalItems ?? null;
|
|
|
|
const numTotalPages = Math.ceil(numTotalItems / SEARCH_PER_PAGE);
|
2020-04-25 01:55:48 -07:00
|
|
|
|
2022-10-14 19:19:56 -07:00
|
|
|
return { loading, error, items, numTotalPages };
|
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;
|