2024-02-27 12:19:07 -08:00
|
|
|
import gql from "graphql-tag";
|
2023-11-11 08:15:10 -08:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
2024-02-27 12:19:07 -08:00
|
|
|
import apolloClient from "../apolloClient";
|
2024-02-25 14:46:27 -08:00
|
|
|
import { normalizeSwfAssetToLayer, normalizeZone } from "./shared-types";
|
2024-02-25 12:06:20 -08:00
|
|
|
|
2023-11-11 08:15:10 -08:00
|
|
|
export function useItemAppearances(id, options = {}) {
|
|
|
|
return useQuery({
|
|
|
|
...options,
|
|
|
|
queryKey: ["items", String(id)],
|
2023-11-11 08:41:31 -08:00
|
|
|
queryFn: () => loadItemAppearancesData(id),
|
2023-11-11 08:15:10 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:41:31 -08:00
|
|
|
async function loadItemAppearancesData(id) {
|
2024-02-25 12:06:20 -08:00
|
|
|
const res = await fetch(
|
|
|
|
`/items/${encodeURIComponent(id)}/appearances.json`,
|
|
|
|
);
|
2023-11-11 08:15:10 -08:00
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(
|
|
|
|
`loading item appearances failed: ${res.status} ${res.statusText}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:41:31 -08:00
|
|
|
return res.json().then(normalizeItemAppearancesData);
|
2023-11-11 08:15:10 -08:00
|
|
|
}
|
|
|
|
|
2024-02-25 12:06:20 -08:00
|
|
|
export function useItemSearch(searchOptions, queryOptions = {}) {
|
2024-02-25 14:46:27 -08:00
|
|
|
// Item searches are considered fresh for an hour, unless the search
|
|
|
|
// includes user-specific filters, in which case React Query will pretty
|
|
|
|
// aggressively reload it!
|
|
|
|
const includesUserSpecificFilters = searchOptions.filters.some(
|
|
|
|
(f) => f.key === "user_closet_hanger_ownership",
|
|
|
|
);
|
|
|
|
const staleTime = includesUserSpecificFilters ? 0 : 1000 * 60 * 5;
|
|
|
|
|
2024-02-25 12:06:20 -08:00
|
|
|
return useQuery({
|
|
|
|
...queryOptions,
|
|
|
|
queryKey: ["itemSearch", buildItemSearchParams(searchOptions)],
|
|
|
|
queryFn: () => loadItemSearch(searchOptions),
|
2024-02-25 14:46:27 -08:00
|
|
|
staleTime,
|
2024-02-25 12:06:20 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildItemSearchParams({
|
|
|
|
filters = [],
|
|
|
|
withAppearancesFor = null,
|
|
|
|
page = 1,
|
|
|
|
perPage = 30,
|
|
|
|
}) {
|
|
|
|
const params = new URLSearchParams();
|
2024-02-25 14:46:27 -08:00
|
|
|
for (const [i, { key, value, isPositive }] of filters.entries()) {
|
|
|
|
params.append(`q[${i}][key]`, key);
|
|
|
|
if (key === "fits") {
|
|
|
|
params.append(`q[${i}][value][species_id]`, value.speciesId);
|
|
|
|
params.append(`q[${i}][value][color_id]`, value.colorId);
|
|
|
|
if (value.altStyleId != null) {
|
|
|
|
params.append(`q[${i}][value][alt_style_id]`, value.altStyleId);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
params.append(`q[${i}][value]`, value);
|
|
|
|
}
|
|
|
|
if (isPositive == false) {
|
2024-02-25 12:06:20 -08:00
|
|
|
params.append(`q[${i}][is_positive]`, "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (withAppearancesFor != null) {
|
2024-02-25 14:46:27 -08:00
|
|
|
const { speciesId, colorId, altStyleId } = withAppearancesFor;
|
|
|
|
params.append(`with_appearances_for[species_id]`, speciesId);
|
|
|
|
params.append(`with_appearances_for[color_id]`, colorId);
|
|
|
|
if (altStyleId != null) {
|
|
|
|
params.append(`with_appearances_for[alt_style_id]`, altStyleId);
|
|
|
|
}
|
2024-02-25 12:06:20 -08:00
|
|
|
}
|
|
|
|
params.append("page", page);
|
|
|
|
params.append("per_page", perPage);
|
|
|
|
return params.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadItemSearch(searchOptions) {
|
|
|
|
const params = buildItemSearchParams(searchOptions);
|
|
|
|
|
|
|
|
const res = await fetch(`/items.json?${params}`);
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(
|
|
|
|
`loading item search failed: ${res.status} ${res.statusText}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-27 12:19:07 -08:00
|
|
|
const data = await res.json();
|
|
|
|
const result = normalizeItemSearchData(data, searchOptions);
|
|
|
|
|
|
|
|
for (const item of result.items) {
|
|
|
|
writeItemToApolloCache(item, searchOptions.withAppearancesFor);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-02-25 12:06:20 -08:00
|
|
|
}
|
|
|
|
|
2024-02-27 12:19:07 -08:00
|
|
|
/**
|
|
|
|
* writeItemToApolloCache is one last important bridge between our loaders and
|
|
|
|
* GQL! In `useOutfitState`, we consult the GraphQL cache to look up basic item
|
|
|
|
* info like zones, to decide when wearing an item would trigger a conflict
|
|
|
|
* with another.
|
|
|
|
*/
|
|
|
|
function writeItemToApolloCache(item, { speciesId, colorId, altStyleId }) {
|
|
|
|
apolloClient.writeQuery({
|
|
|
|
query: gql`
|
|
|
|
query WriteItemFromLoader(
|
|
|
|
$itemId: ID!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
$altStyleId: ID
|
|
|
|
) {
|
|
|
|
item(id: $itemId) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
|
|
|
isNc
|
|
|
|
isPb
|
|
|
|
currentUserOwnsThis
|
|
|
|
currentUserWantsThis
|
|
|
|
appearanceOn(
|
|
|
|
speciesId: $speciesId
|
|
|
|
colorId: $colorId
|
|
|
|
altStyleId: $altStyleId
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
layers {
|
|
|
|
id
|
|
|
|
remoteId
|
|
|
|
bodyId
|
|
|
|
knownGlitches
|
|
|
|
svgUrl
|
|
|
|
canvasMovieLibraryUrl
|
2024-02-27 12:43:28 -08:00
|
|
|
imageUrl: imageUrlV2(idealSize: SIZE_600)
|
2024-02-27 12:19:07 -08:00
|
|
|
swfUrl
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
itemId: item.id,
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
altStyleId,
|
|
|
|
},
|
|
|
|
data: { item },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:41:31 -08:00
|
|
|
function normalizeItemAppearancesData(data) {
|
|
|
|
return {
|
2023-11-11 08:50:32 -08:00
|
|
|
name: data.name,
|
2023-11-11 08:41:31 -08:00
|
|
|
appearances: data.appearances.map((appearance) => ({
|
|
|
|
body: normalizeBody(appearance.body),
|
2024-02-25 12:06:20 -08:00
|
|
|
swfAssets: appearance.swf_assets.map(normalizeSwfAssetToLayer),
|
2023-11-11 08:15:10 -08:00
|
|
|
})),
|
2023-11-11 08:41:31 -08:00
|
|
|
restrictedZones: data.restricted_zones.map((z) => normalizeZone(z)),
|
|
|
|
};
|
2023-11-11 08:15:10 -08:00
|
|
|
}
|
|
|
|
|
2024-02-25 12:06:20 -08:00
|
|
|
function normalizeItemSearchData(data, searchOptions) {
|
|
|
|
return {
|
2024-02-27 12:19:07 -08:00
|
|
|
__typename: "ItemSearchResultV2",
|
2024-02-25 12:06:20 -08:00
|
|
|
id: buildItemSearchParams(searchOptions),
|
2024-02-25 14:46:27 -08:00
|
|
|
numTotalPages: data.total_pages,
|
2024-02-25 12:06:20 -08:00
|
|
|
items: data.items.map((item) => ({
|
2024-02-27 12:19:07 -08:00
|
|
|
__typename: "Item",
|
2024-02-25 12:06:20 -08:00
|
|
|
id: String(item.id),
|
|
|
|
name: item.name,
|
|
|
|
thumbnailUrl: item.thumbnail_url,
|
|
|
|
isNc: item["nc?"],
|
|
|
|
isPb: item["pb?"],
|
|
|
|
currentUserOwnsThis: item["owned?"],
|
|
|
|
currentUserWantsThis: item["wanted?"],
|
|
|
|
appearanceOn: normalizeItemSearchAppearance(
|
|
|
|
data.appearances[item.id],
|
|
|
|
item,
|
|
|
|
),
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeItemSearchAppearance(data, item) {
|
|
|
|
if (data == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-02-27 12:19:07 -08:00
|
|
|
__typename: "ItemAppearance",
|
2024-02-25 12:06:20 -08:00
|
|
|
id: `item-${item.id}-body-${data.body.id}`,
|
|
|
|
layers: data.swf_assets.map(normalizeSwfAssetToLayer),
|
2024-09-08 18:15:27 -07:00
|
|
|
restrictedZones: [
|
|
|
|
...item.restricted_zones,
|
|
|
|
...data.swf_assets.map((a) => a.restricted_zones).flat(),
|
|
|
|
].map(normalizeZone),
|
2024-02-25 12:06:20 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:15:10 -08:00
|
|
|
function normalizeBody(body) {
|
|
|
|
if (String(body.id) === "0") {
|
|
|
|
return { id: "0" };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-02-27 12:19:07 -08:00
|
|
|
__typename: "Body",
|
2023-11-11 08:15:10 -08:00
|
|
|
id: String(body.id),
|
|
|
|
species: {
|
|
|
|
id: String(body.species.id),
|
|
|
|
name: body.species.name,
|
2023-11-11 11:57:43 -08:00
|
|
|
humanName: body.species.human_name,
|
2023-11-11 08:15:10 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|