2024-01-29 03:20:48 -08:00
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
2024-02-25 12:06:20 -08:00
|
|
|
import { normalizeSwfAssetToLayer } from "./shared-types";
|
|
|
|
|
2024-02-08 10:48:45 -08:00
|
|
|
export function useAltStylesForSpecies(speciesId, options = {}) {
|
2024-01-29 03:20:48 -08:00
|
|
|
return useQuery({
|
|
|
|
...options,
|
|
|
|
queryKey: ["altStylesForSpecies", String(speciesId)],
|
|
|
|
queryFn: () => loadAltStylesForSpecies(speciesId),
|
2024-02-08 10:48:45 -08:00
|
|
|
enabled: (options.enabled ?? true) && speciesId != null,
|
2024-01-29 03:20:48 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-30 07:01:03 -08:00
|
|
|
// NOTE: This is actually just a wrapper for `useAltStylesForSpecies`, to share
|
|
|
|
// the same cache key!
|
|
|
|
export function useAltStyle(id, speciesId, options = {}) {
|
2024-02-24 17:23:57 -08:00
|
|
|
const query = useAltStylesForSpecies(speciesId, {
|
|
|
|
...options,
|
|
|
|
enabled: (options.enabled ?? true) && id != null,
|
|
|
|
});
|
2024-01-30 07:01:03 -08:00
|
|
|
|
|
|
|
return {
|
|
|
|
...query,
|
|
|
|
data: query.data?.find((s) => s.id === id) ?? null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-29 03:20:48 -08:00
|
|
|
async function loadAltStylesForSpecies(speciesId) {
|
|
|
|
const res = await fetch(
|
|
|
|
`/species/${encodeURIComponent(speciesId)}/alt-styles.json`,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(
|
|
|
|
`loading alt styles failed: ${res.status} ${res.statusText}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json().then(normalizeAltStyles);
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeAltStyles(altStylesData) {
|
|
|
|
return altStylesData.map(normalizeAltStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeAltStyle(altStyleData) {
|
|
|
|
return {
|
2024-01-30 06:21:32 -08:00
|
|
|
id: String(altStyleData.id),
|
|
|
|
speciesId: String(altStyleData.species_id),
|
|
|
|
colorId: String(altStyleData.color_id),
|
|
|
|
bodyId: String(altStyleData.body_id),
|
2024-01-30 05:54:20 -08:00
|
|
|
seriesName: altStyleData.series_name,
|
2024-01-29 03:20:48 -08:00
|
|
|
adjectiveName: altStyleData.adjective_name,
|
|
|
|
thumbnailUrl: altStyleData.thumbnail_url,
|
2024-01-30 07:01:03 -08:00
|
|
|
|
|
|
|
// This matches the PetAppearanceForOutfitPreview GQL fragment!
|
|
|
|
appearance: {
|
|
|
|
bodyId: String(altStyleData.body_id),
|
|
|
|
pose: "UNKNOWN",
|
|
|
|
isGlitched: false,
|
|
|
|
species: { id: String(altStyleData.species_id) },
|
|
|
|
color: { id: String(altStyleData.species_id) },
|
|
|
|
layers: altStyleData.swf_assets.map(normalizeSwfAssetToLayer),
|
|
|
|
restrictedZones: [],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|