impress-2020/src/app/components/useOutfitAppearance.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

import React from "react";
import gql from "graphql-tag";
import { useQuery } from "@apollo/react-hooks";
/**
* useOutfitAppearance downloads the outfit's appearance data, and returns
* visibleLayers for rendering.
*/
export default function useOutfitAppearance(outfitState) {
2020-05-23 12:47:06 -07:00
const { wornItemIds, speciesId, colorId, pose } = outfitState;
const { loading, error, data } = useQuery(
gql`
2020-05-19 14:48:54 -07:00
query OutfitAppearance(
$wornItemIds: [ID!]!
$speciesId: ID!
$colorId: ID!
2020-05-23 12:47:06 -07:00
$pose: Pose!
) {
2020-05-23 12:47:06 -07:00
petAppearance(speciesId: $speciesId, colorId: $colorId, pose: $pose) {
...PetAppearanceForOutfitPreview
}
items(ids: $wornItemIds) {
id
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
...ItemAppearanceForOutfitPreview
}
}
}
${itemAppearanceFragment}
${petAppearanceFragment}
`,
{
variables: {
wornItemIds,
speciesId,
colorId,
2020-05-23 12:47:06 -07:00
pose,
},
}
);
const itemAppearances = React.useMemo(
() => (data?.items || []).map((i) => i.appearanceOn),
[data]
);
const visibleLayers = React.useMemo(
() => getVisibleLayers(data?.petAppearance, itemAppearances),
[data, itemAppearances]
);
return { loading, error, visibleLayers };
}
2020-05-02 21:04:54 -07:00
export function getVisibleLayers(petAppearance, itemAppearances) {
if (!petAppearance) {
return [];
}
const validItemAppearances = itemAppearances.filter((a) => a);
const allAppearances = [petAppearance, ...validItemAppearances];
let allLayers = allAppearances.map((a) => a.layers).flat();
// Clean up our data a bit, by ensuring only one layer per zone. This
// shouldn't happen in theory, but sometimes our database doesn't clean up
// after itself correctly :(
allLayers = allLayers.filter((l, i) => {
return allLayers.findIndex((l2) => l2.zone.id === l.zone.id) === i;
});
const allRestrictedZoneIds = validItemAppearances
.map((l) => l.restrictedZones)
.flat()
.map((z) => z.id);
const visibleLayers = allLayers.filter(
(l) => !allRestrictedZoneIds.includes(l.zone.id)
);
visibleLayers.sort((a, b) => a.zone.depth - b.zone.depth);
return visibleLayers;
}
export const itemAppearanceFragment = gql`
fragment ItemAppearanceForOutfitPreview on ItemAppearance {
layers {
id
2020-05-11 21:19:34 -07:00
svgUrl
imageUrl(size: SIZE_600)
zone {
id
depth
}
}
restrictedZones {
id
}
}
`;
export const petAppearanceFragment = gql`
fragment PetAppearanceForOutfitPreview on PetAppearance {
id
layers {
id
2020-05-11 21:19:34 -07:00
svgUrl
imageUrl(size: SIZE_600)
zone {
id
depth
}
}
}
`;