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-08-28 22:58:39 -07:00
|
|
|
const { wornItemIds, speciesId, colorId, pose, appearanceId } = 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-08-28 22:58:39 -07:00
|
|
|
appearanceId == null
|
|
|
|
? gql`
|
|
|
|
query OutfitPetAppearance(
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
$pose: Pose!
|
|
|
|
) {
|
|
|
|
petAppearance(
|
|
|
|
speciesId: $speciesId
|
|
|
|
colorId: $colorId
|
|
|
|
pose: $pose
|
|
|
|
) {
|
|
|
|
...PetAppearanceForOutfitPreview
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${petAppearanceFragment}
|
|
|
|
`
|
|
|
|
: gql`
|
|
|
|
query OutfitPetAppearanceById($appearanceId: ID!) {
|
|
|
|
petAppearance: petAppearanceById(id: $appearanceId) {
|
|
|
|
...PetAppearanceForOutfitPreview
|
|
|
|
}
|
|
|
|
}
|
|
|
|
${petAppearanceFragment}
|
|
|
|
`,
|
2020-07-22 23:08:28 -07:00
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
pose,
|
2020-08-28 22:58:39 -07:00
|
|
|
appearanceId,
|
2020-07-22 23:08:28 -07:00
|
|
|
},
|
|
|
|
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-08-01 14:30:10 -07:00
|
|
|
const bodyId = data1?.petAppearance?.bodyId;
|
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
return {
|
|
|
|
loading: loading1 || loading2,
|
|
|
|
error: error1 || error2,
|
|
|
|
visibleLayers,
|
2020-08-01 14:30:10 -07:00
|
|
|
bodyId,
|
2020-07-22 23:08:28 -07:00
|
|
|
};
|
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-08-31 19:23:56 -07:00
|
|
|
const itemRestrictedZoneIds = validItemAppearances
|
|
|
|
.map((a) => a.restrictedZones)
|
|
|
|
.flat()
|
|
|
|
.map((z) => z.id);
|
2020-08-31 23:18:30 -07:00
|
|
|
const petRestrictedZoneIds = petAppearance.restrictedZones.map((z) => z.id);
|
2020-08-31 19:23:56 -07:00
|
|
|
const allRestrictedZoneIds = new Set([
|
|
|
|
...itemRestrictedZoneIds,
|
|
|
|
...petRestrictedZoneIds,
|
|
|
|
]);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
|
|
|
const visibleLayers = allLayers.filter(
|
2020-08-31 19:23:56 -07:00
|
|
|
(l) => !allRestrictedZoneIds.has(l.zone.id)
|
2020-05-02 13:40:37 -07:00
|
|
|
);
|
|
|
|
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-08-28 00:10:00 -07:00
|
|
|
id
|
2020-05-02 13:40:37 -07:00
|
|
|
layers {
|
|
|
|
id
|
2020-08-14 22:09:52 -07:00
|
|
|
remoteId # HACK: This is for Support tools, but other views don't need it
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-09-22 03:53:48 -07:00
|
|
|
canvasMovieLibraryUrl
|
2020-05-02 13:40:37 -07:00
|
|
|
imageUrl(size: SIZE_600)
|
2020-08-01 22:54:30 -07:00
|
|
|
swfUrl # HACK: This is for Support tools, but other views don't need it
|
2020-08-01 14:12:57 -07:00
|
|
|
bodyId
|
2020-05-02 13:40:37 -07:00
|
|
|
zone {
|
|
|
|
id
|
2020-08-19 17:50:05 -07:00
|
|
|
depth @client
|
|
|
|
label @client # 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-08-01 14:30:10 -07:00
|
|
|
bodyId
|
2020-05-02 17:22:46 -07:00
|
|
|
layers {
|
|
|
|
id
|
2020-05-11 21:19:34 -07:00
|
|
|
svgUrl
|
2020-09-22 03:53:48 -07:00
|
|
|
canvasMovieLibraryUrl
|
2020-05-02 17:22:46 -07:00
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
|
|
|
id
|
2020-08-19 17:50:05 -07:00
|
|
|
depth @client
|
2020-05-02 17:22:46 -07:00
|
|
|
}
|
2020-08-31 23:18:30 -07:00
|
|
|
}
|
|
|
|
restrictedZones {
|
|
|
|
id
|
2020-05-02 17:22:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|