2024-02-25 14:46:27 -08:00
|
|
|
import React from "react";
|
2023-08-10 15:56:36 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/client";
|
2024-02-25 14:46:27 -08:00
|
|
|
import { useDebounce, useLocalStorage } from "../util";
|
|
|
|
import { useItemSearch } from "../loaders/items";
|
|
|
|
import { emptySearchQuery, searchQueryIsEmpty } from "./SearchToolbar";
|
2023-08-10 15:56:36 -07:00
|
|
|
import { itemAppearanceFragment } from "../components/useOutfitAppearance";
|
|
|
|
import { SEARCH_PER_PAGE } from "./SearchPanel";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useSearchResults manages the actual querying and state management of search!
|
|
|
|
*/
|
|
|
|
export function useSearchResults(
|
|
|
|
query,
|
|
|
|
outfitState,
|
|
|
|
currentPageNumber,
|
2023-10-24 16:45:49 -07:00
|
|
|
{ skip = false } = {},
|
2023-08-10 15:56:36 -07:00
|
|
|
) {
|
2024-02-01 04:57:20 -08:00
|
|
|
const { speciesId, colorId, altStyleId } = outfitState;
|
2023-08-10 15:56:36 -07:00
|
|
|
|
|
|
|
// We debounce the search query, so that we don't resend a new query whenever
|
|
|
|
// the user types anything.
|
|
|
|
const debouncedQuery = useDebounce(query, 300, {
|
|
|
|
waitForFirstPause: true,
|
|
|
|
initialValue: emptySearchQuery,
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
const currentPageIndex = currentPageNumber - 1;
|
|
|
|
const offset = currentPageIndex * SEARCH_PER_PAGE;
|
|
|
|
|
2024-02-25 14:46:27 -08:00
|
|
|
// Until the new item search is ready, we can toggle between them! Use
|
|
|
|
// `setItemSearchQueryMode` in the JS console to choose "gql" or "new".
|
|
|
|
const [queryMode, setQueryMode] = useLocalStorage(
|
|
|
|
"DTIItemSearchQueryMode",
|
|
|
|
"gql",
|
|
|
|
);
|
|
|
|
React.useEffect(() => {
|
|
|
|
window.setItemSearchQueryMode = setQueryMode;
|
|
|
|
}, [setQueryMode]);
|
2024-02-25 12:06:20 -08:00
|
|
|
|
2023-08-10 15:56:36 -07:00
|
|
|
const {
|
|
|
|
loading: loadingGQL,
|
2024-02-25 14:46:27 -08:00
|
|
|
error: errorGQL,
|
|
|
|
data: dataGQL,
|
2023-08-10 15:56:36 -07:00
|
|
|
} = useQuery(
|
|
|
|
gql`
|
|
|
|
query SearchPanel(
|
|
|
|
$query: String!
|
|
|
|
$fitsPet: FitsPetSearchFilter
|
|
|
|
$itemKind: ItemKindSearchFilter
|
|
|
|
$currentUserOwnsOrWants: OwnsOrWants
|
|
|
|
$zoneIds: [ID!]!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
2024-02-01 04:57:20 -08:00
|
|
|
$altStyleId: ID
|
2023-08-10 15:56:36 -07:00
|
|
|
$offset: Int!
|
|
|
|
$perPage: Int!
|
|
|
|
) {
|
|
|
|
itemSearch: itemSearchV2(
|
|
|
|
query: $query
|
|
|
|
fitsPet: $fitsPet
|
|
|
|
itemKind: $itemKind
|
|
|
|
currentUserOwnsOrWants: $currentUserOwnsOrWants
|
|
|
|
zoneIds: $zoneIds
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
numTotalItems
|
|
|
|
items(offset: $offset, limit: $perPage) {
|
|
|
|
# TODO: De-dupe this from useOutfitState?
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
|
|
|
isNc
|
|
|
|
isPb
|
|
|
|
currentUserOwnsThis
|
|
|
|
currentUserWantsThis
|
|
|
|
|
2024-02-01 04:57:20 -08:00
|
|
|
appearanceOn(
|
|
|
|
speciesId: $speciesId
|
|
|
|
colorId: $colorId
|
|
|
|
altStyleId: $altStyleId
|
|
|
|
) {
|
2023-08-10 15:56:36 -07:00
|
|
|
# This enables us to quickly show the item when the user clicks it!
|
|
|
|
...ItemAppearanceForOutfitPreview
|
|
|
|
|
|
|
|
# This is used to group items by zone, and to detect conflicts when
|
|
|
|
# wearing a new item.
|
|
|
|
layers {
|
|
|
|
zone {
|
|
|
|
id
|
2023-08-10 18:09:10 -07:00
|
|
|
label
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
restrictedZones {
|
|
|
|
id
|
2023-08-10 18:09:10 -07:00
|
|
|
label
|
|
|
|
isCommonlyUsedByItems
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${itemAppearanceFragment}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
query: debouncedQuery.value,
|
2024-02-01 04:57:20 -08:00
|
|
|
fitsPet: { speciesId, colorId, altStyleId },
|
2023-08-10 15:56:36 -07:00
|
|
|
itemKind: debouncedQuery.filterToItemKind,
|
|
|
|
currentUserOwnsOrWants: debouncedQuery.filterToCurrentUserOwnsOrWants,
|
|
|
|
zoneIds: filterToZoneIds,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
2024-02-01 04:57:20 -08:00
|
|
|
altStyleId,
|
2023-08-10 15:56:36 -07:00
|
|
|
offset,
|
|
|
|
perPage: SEARCH_PER_PAGE,
|
|
|
|
},
|
|
|
|
context: { sendAuth: true },
|
|
|
|
skip:
|
|
|
|
skip ||
|
2024-02-25 14:46:27 -08:00
|
|
|
queryMode !== "gql" ||
|
2023-08-10 15:56:36 -07:00
|
|
|
(!debouncedQuery.value &&
|
|
|
|
!debouncedQuery.filterToItemKind &&
|
|
|
|
!debouncedQuery.filterToZoneLabel &&
|
|
|
|
!debouncedQuery.filterToCurrentUserOwnsOrWants),
|
|
|
|
onError: (e) => {
|
|
|
|
console.error("Error loading search results", e);
|
|
|
|
},
|
|
|
|
// Return `numTotalItems` from the GQL cache while waiting for next page!
|
|
|
|
returnPartialData: true,
|
2023-10-24 16:45:49 -07:00
|
|
|
},
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
2024-02-25 14:46:27 -08:00
|
|
|
const {
|
|
|
|
isLoading: loadingQuery,
|
|
|
|
error: errorQuery,
|
|
|
|
data: dataQuery,
|
|
|
|
} = useItemSearch(
|
|
|
|
{
|
|
|
|
filters: buildSearchFilters(debouncedQuery, outfitState),
|
|
|
|
withAppearancesFor: { speciesId, colorId, altStyleId },
|
|
|
|
page: currentPageIndex + 1,
|
|
|
|
perPage: SEARCH_PER_PAGE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enabled:
|
|
|
|
!skip && queryMode === "new" && !searchQueryIsEmpty(debouncedQuery),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const loading =
|
|
|
|
debouncedQuery !== query ||
|
|
|
|
(queryMode === "gql" ? loadingGQL : loadingQuery);
|
|
|
|
const error = queryMode === "gql" ? errorGQL : errorQuery;
|
|
|
|
const items =
|
|
|
|
(queryMode === "gql" ? dataGQL?.itemSearch?.items : dataQuery?.items) ?? [];
|
|
|
|
const numTotalPages =
|
|
|
|
(queryMode === "gql"
|
|
|
|
? Math.ceil((dataGQL?.itemSearch?.numTotalItems ?? 0) / SEARCH_PER_PAGE)
|
|
|
|
: dataQuery?.numTotalPages) ?? 0;
|
2023-08-10 15:56:36 -07:00
|
|
|
|
|
|
|
return { loading, error, items, numTotalPages };
|
|
|
|
}
|
2024-02-25 14:46:27 -08:00
|
|
|
|
|
|
|
function buildSearchFilters(query, { speciesId, colorId, altStyleId }) {
|
|
|
|
const filters = [];
|
|
|
|
|
|
|
|
if (query.value) {
|
|
|
|
filters.push({ key: "name", value: query.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.filterToItemKind === "NC") {
|
|
|
|
filters.push({ key: "is_nc" });
|
|
|
|
} else if (query.filterToItemKind === "PB") {
|
|
|
|
filters.push({ key: "is_pb" });
|
|
|
|
} else if (query.filterToItemKind === "NP") {
|
|
|
|
filters.push({ key: "is_np" });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.filterToZoneLabel != null) {
|
|
|
|
filters.push({
|
|
|
|
key: "occupied_zone_set_name",
|
|
|
|
value: query.filterToZoneLabel,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.filterToCurrentUserOwnsOrWants === "OWNS") {
|
|
|
|
filters.push({ key: "user_closet_hanger_ownership", value: "true" });
|
|
|
|
} else if (query.filterToCurrentUserOwnsOrWants === "WANTS") {
|
|
|
|
filters.push({ key: "user_closet_hanger_ownership", value: "false" });
|
|
|
|
}
|
|
|
|
|
|
|
|
filters.push({
|
|
|
|
key: "fits",
|
|
|
|
value: { speciesId, colorId, altStyleId },
|
|
|
|
});
|
|
|
|
|
|
|
|
return filters;
|
|
|
|
}
|