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";
|
2021-05-13 17:33:54 -07:00
|
|
|
import getVisibleLayers, {
|
|
|
|
itemAppearanceFragmentForGetVisibleLayers,
|
|
|
|
petAppearanceFragmentForGetVisibleLayers,
|
2021-05-13 17:35:50 -07:00
|
|
|
} from "../../shared/getVisibleLayers";
|
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
|
|
|
},
|
2021-03-14 07:16:01 -07:00
|
|
|
skip:
|
|
|
|
speciesId == null ||
|
|
|
|
colorId == null ||
|
|
|
|
(pose == null && appearanceId == null),
|
2020-07-22 23:08:28 -07:00
|
|
|
}
|
|
|
|
);
|
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
|
2021-02-10 13:47:02 -08:00
|
|
|
name # HACK: This is for HTML5 detection UI in OutfitControls!
|
2021-02-09 20:28:03 -08:00
|
|
|
appearance: 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
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-02-09 20:28:03 -08:00
|
|
|
const petAppearance = data1?.petAppearance;
|
2021-02-10 13:51:59 -08:00
|
|
|
const items = data2?.items;
|
2020-06-05 23:56:42 -07:00
|
|
|
const itemAppearances = React.useMemo(
|
2021-02-09 20:28:03 -08:00
|
|
|
() => (items || []).map((i) => i.appearance),
|
|
|
|
[items]
|
2020-06-05 23:56:42 -07:00
|
|
|
);
|
|
|
|
const visibleLayers = React.useMemo(
|
2021-02-09 20:28:03 -08:00
|
|
|
() => getVisibleLayers(petAppearance, itemAppearances),
|
|
|
|
[petAppearance, itemAppearances]
|
2020-06-05 23:56:42 -07:00
|
|
|
);
|
2020-05-02 13:40:37 -07:00
|
|
|
|
2021-02-09 20:28:03 -08:00
|
|
|
const bodyId = petAppearance?.bodyId;
|
2020-08-01 14:30:10 -07:00
|
|
|
|
2020-07-22 23:08:28 -07:00
|
|
|
return {
|
|
|
|
loading: loading1 || loading2,
|
|
|
|
error: error1 || error2,
|
2021-02-09 20:28:03 -08:00
|
|
|
petAppearance,
|
2021-02-10 13:51:59 -08:00
|
|
|
items: items || [],
|
2021-02-09 20:28:03 -08:00
|
|
|
itemAppearances,
|
2020-07-22 23:08:28 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-15 07:50:13 -07:00
|
|
|
export const appearanceLayerFragment = gql`
|
|
|
|
fragment AppearanceLayerForOutfitPreview on AppearanceLayer {
|
|
|
|
id
|
|
|
|
svgUrl
|
|
|
|
canvasMovieLibraryUrl
|
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
bodyId
|
2021-03-18 08:31:12 -07:00
|
|
|
knownGlitches # For HTML5 & Known Glitches UI
|
2021-03-15 07:50:13 -07:00
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth @client
|
2021-03-17 05:54:34 -07:00
|
|
|
label @client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2021-04-26 06:07:52 -07:00
|
|
|
export const appearanceLayerFragmentForSupport = gql`
|
2021-03-17 05:54:34 -07:00
|
|
|
fragment AppearanceLayerForSupport on AppearanceLayer {
|
|
|
|
id
|
|
|
|
remoteId # HACK: This is for Support tools, but other views don't need it
|
|
|
|
swfUrl # HACK: This is for Support tools, but other views don't need it
|
|
|
|
zone {
|
|
|
|
id
|
2021-03-15 07:50:13 -07:00
|
|
|
label @client # HACK: This is for Support tools, but other views don't need it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-05-02 13:40:37 -07:00
|
|
|
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 {
|
2021-03-17 05:54:34 -07:00
|
|
|
id
|
2021-03-15 07:50:13 -07:00
|
|
|
...AppearanceLayerForOutfitPreview
|
2021-03-17 05:54:34 -07:00
|
|
|
...AppearanceLayerForSupport # HACK: Most users don't need this!
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
2021-01-04 00:26:05 -08:00
|
|
|
...ItemAppearanceForGetVisibleLayers
|
2020-05-02 13:40:37 -07:00
|
|
|
}
|
2021-01-04 00:26:05 -08:00
|
|
|
|
2021-03-15 07:50:13 -07:00
|
|
|
${appearanceLayerFragment}
|
2021-03-17 05:54:34 -07:00
|
|
|
${appearanceLayerFragmentForSupport}
|
2021-01-04 00:26:05 -08:00
|
|
|
${itemAppearanceFragmentForGetVisibleLayers}
|
2020-05-02 13:40:37 -07:00
|
|
|
`;
|
2020-05-02 17:22:46 -07:00
|
|
|
|
2021-01-04 00:10:35 -08:00
|
|
|
export const petAppearanceFragment = gql`
|
|
|
|
fragment PetAppearanceForOutfitPreview on PetAppearance {
|
|
|
|
id
|
|
|
|
bodyId
|
2021-05-03 14:52:50 -07:00
|
|
|
pose # For Known Glitches UI
|
2021-03-18 08:31:12 -07:00
|
|
|
isGlitched # For Known Glitches UI
|
2021-05-03 14:52:50 -07:00
|
|
|
species {
|
|
|
|
id # For Known Glitches UI
|
|
|
|
}
|
2021-03-18 09:02:40 -07:00
|
|
|
color {
|
|
|
|
id # For Known Glitches UI
|
|
|
|
}
|
2021-01-04 00:10:35 -08:00
|
|
|
layers {
|
|
|
|
id
|
2021-03-17 05:54:34 -07:00
|
|
|
...AppearanceLayerForOutfitPreview
|
2021-01-04 00:10:35 -08:00
|
|
|
}
|
|
|
|
...PetAppearanceForGetVisibleLayers
|
|
|
|
}
|
|
|
|
|
2021-03-17 05:54:34 -07:00
|
|
|
${appearanceLayerFragment}
|
2021-01-04 00:10:35 -08:00
|
|
|
${petAppearanceFragmentForGetVisibleLayers}
|
|
|
|
`;
|