2023-11-11 08:15:10 -08:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
2024-02-25 12:06:20 -08:00
|
|
|
import { normalizeSwfAssetToLayer } from "./shared-types";
|
|
|
|
|
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 = {}) {
|
|
|
|
return useQuery({
|
|
|
|
...queryOptions,
|
|
|
|
queryKey: ["itemSearch", buildItemSearchParams(searchOptions)],
|
|
|
|
queryFn: () => loadItemSearch(searchOptions),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildItemSearchParams({
|
|
|
|
filters = [],
|
|
|
|
withAppearancesFor = null,
|
|
|
|
page = 1,
|
|
|
|
perPage = 30,
|
|
|
|
}) {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
for (const [i, filter] of filters.entries()) {
|
|
|
|
params.append(`q[${i}][key]`, filter.key);
|
|
|
|
params.append(`q[${i}][value]`, filter.value);
|
|
|
|
if (params.isPositive == false) {
|
|
|
|
params.append(`q[${i}][is_positive]`, "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (withAppearancesFor != null) {
|
|
|
|
params.append("with_appearances_for", withAppearancesFor);
|
|
|
|
}
|
|
|
|
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}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
.json()
|
|
|
|
.then((data) => normalizeItemSearchData(data, searchOptions));
|
|
|
|
}
|
|
|
|
window.loadItemSearch = loadItemSearch;
|
|
|
|
|
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 {
|
|
|
|
id: buildItemSearchParams(searchOptions),
|
|
|
|
numTotalItems: data.total_count,
|
|
|
|
items: data.items.map((item) => ({
|
|
|
|
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 {
|
|
|
|
id: `item-${item.id}-body-${data.body.id}`,
|
|
|
|
layers: data.swf_assets.map(normalizeSwfAssetToLayer),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-11 08:15:10 -08:00
|
|
|
function normalizeBody(body) {
|
|
|
|
if (String(body.id) === "0") {
|
|
|
|
return { id: "0" };
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
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
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeZone(zone) {
|
|
|
|
return { id: String(zone.id), label: zone.label, depth: zone.depth };
|
|
|
|
}
|