2023-11-11 08:15:10 -08:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
|
|
|
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) {
|
2023-11-11 08:15:10 -08:00
|
|
|
const res = await fetch(`/items/${encodeURIComponent(id)}/appearances.json`);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
swfAssets: appearance.swf_assets.map((asset) => ({
|
|
|
|
id: String(asset.id),
|
|
|
|
knownGlitches: asset.known_glitches,
|
|
|
|
zone: normalizeZone(asset.zone),
|
|
|
|
restrictedZones: asset.restricted_zones.map((z) => normalizeZone(z)),
|
|
|
|
urls: {
|
|
|
|
swf: asset.urls.swf,
|
|
|
|
png: asset.urls.png,
|
|
|
|
manifest: asset.urls.manifest,
|
|
|
|
},
|
|
|
|
})),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
humanName: body.species.humanName,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeZone(zone) {
|
|
|
|
return { id: String(zone.id), label: zone.label, depth: zone.depth };
|
|
|
|
}
|