2020-06-05 23:56:42 -07:00
|
|
|
import React from "react";
|
2020-05-02 13:40:37 -07:00
|
|
|
import gql from "graphql-tag";
|
2020-07-31 23:10:34 -07:00
|
|
|
import { useQuery } from "@apollo/client";
|
2020-05-02 13:40:37 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
// We split this query out from the other one, so that we can HTTP cache it.
|
|
|
|
//
|
|
|
|
// While Apollo gives us fine-grained caching during the page session, we can
|
|
|
|
// only HTTP a full query at a time.
|
|
|
|
//
|
|
|
|
// This is a minor optimization with respect to keeping the user's cache
|
|
|
|
// populated with their favorite species/color combinations. Once we start
|
|
|
|
// caching the items by body instead of species/color, this could make color
|
|
|
|
// changes really snappy!
|
|
|
|
//
|
|
|
|
// The larger optimization is that this enables the CDN to edge-cache the
|
|
|
|
// most popular species/color combinations, for very fast previews on the
|
|
|
|
// HomePage. At time of writing, Vercel isn't actually edge-caching these, I
|
|
|
|
// assume because our traffic isn't enough - so let's keep an eye on this!
|
|
|
|
const { loading: loading1, error: error1, data: data1 } = useQuery(
|
2020-05-02 13:40:37 -07:00
|
|
|
gql`
|
2020-07-22 23:08:28 -07:00
|
|
|
query OutfitPetAppearance($speciesId: ID!, $colorId: ID!, $pose: Pose!) {
|
2020-05-23 12:47:06 -07:00
|
|
|
petAppearance(speciesId: $speciesId, colorId: $colorId, pose: $pose) {
|
2020-05-02 17:22:46 -07:00
|
|
|
...PetAppearanceForOutfitPreview
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
2020-07-22 23:08:28 -07:00
|
|
|
}
|
|
|
|
${petAppearanceFragment}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
pose,
|
|
|
|
},
|
|
|
|
skip: speciesId == null || colorId == null || pose == null,
|
|
|
|
}
|
|
|
|
);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
const { loading: loading2, error: error2, data: data2 } = useQuery(
|
|
|
|
gql`
|
|
|
|
query OutfitItemsAppearance(
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
$wornItemIds: [ID!]!
|
|
|
|
) {
|
2020-05-02 13:40:37 -07:00
|
|
|
items(ids: $wornItemIds) {
|
|
|
|
id
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
2020-05-02 17:22:46 -07:00
|
|
|
...ItemAppearanceForOutfitPreview
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${itemAppearanceFragment}
|
|
|
|
`,
|
|
|
|
{
|
2020-05-02 22:32:08 -07:00
|
|
|
variables: {
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
2020-07-22 23:08:28 -07:00
|
|
|
wornItemIds,
|
2020-05-02 22:32:08 -07:00
|
|
|
},
|
2020-07-22 23:08:28 -07:00
|
|
|
skip: speciesId == null || colorId == null || wornItemIds.length === 0,
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-05 23:56:42 -07:00
|
|
|
const itemAppearances = React.useMemo(
|
2020-07-22 23:08:28 -07:00
|
|
|
() => (data2?.items || []).map((i) => i.appearanceOn),
|
|
|
|
[data2]
|
2020-06-05 23:56:42 -07:00
|
|
|
);
|
|
|
|
const visibleLayers = React.useMemo(
|
2020-07-22 23:08:28 -07:00
|
|
|
() => getVisibleLayers(data1?.petAppearance, itemAppearances),
|
|
|
|
[data1, itemAppearances]
|
2020-06-05 23:56:42 -07:00
|
|
|
);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
return {
|
|
|
|
loading: loading1 || loading2,
|
|
|
|
error: error1 || error2,
|
|
|
|
visibleLayers,
|
|
|
|
};
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
|
2020-05-02 21:04:54 -07:00
|
|
|
export function getVisibleLayers(petAppearance, itemAppearances) {
|
|
|
|
if (!petAppearance) {
|
2020-05-02 13:40:37 -07:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:32:08 -07:00
|
|
|
const validItemAppearances = itemAppearances.filter((a) => a);
|
|
|
|
|
2020-08-01 01:35:27 -07:00
|
|
|
const petLayers = petAppearance.layers.map((l) => ({ ...l, source: "pet" }));
|
|
|
|
const itemLayers = validItemAppearances
|
|
|
|
.map((a) => a.layers)
|
|
|
|
.flat()
|
|
|
|
.map((l) => ({ ...l, source: "item" }));
|
|
|
|
let allLayers = [...petLayers, ...itemLayers];
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2020-05-02 22:32:08 -07:00
|
|
|
const allRestrictedZoneIds = validItemAppearances
|
2020-05-02 13:40:37 -07:00
|
|
|
.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`
|
2020-05-02 17:22:46 -07:00
|
|
|
fragment ItemAppearanceForOutfitPreview on ItemAppearance {
|
2020-05-02 13:40:37 -07:00
|
|
|
layers {
|
|
|
|
id
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-05-02 13:40:37 -07:00
|
|
|
imageUrl(size: SIZE_600)
|
2020-08-01 14:12:57 -07:00
|
|
|
bodyId
|
2020-05-02 13:40:37 -07:00
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth
|
2020-08-01 01:35:27 -07:00
|
|
|
label # HACK: This is for Support tools, but other views don't need it
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
2020-05-02 17:22:46 -07:00
|
|
|
|
|
|
|
export const petAppearanceFragment = gql`
|
|
|
|
fragment PetAppearanceForOutfitPreview on PetAppearance {
|
2020-05-02 22:41:01 -07:00
|
|
|
id
|
2020-05-02 17:22:46 -07:00
|
|
|
layers {
|
|
|
|
id
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-05-02 17:22:46 -07:00
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|