2020-04-23 13:31:39 -07:00
|
|
|
import React from "react";
|
2020-04-24 00:28:00 -07:00
|
|
|
import { CSSTransition, TransitionGroup } from "react-transition-group";
|
2020-04-23 13:31:39 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/react-hooks";
|
|
|
|
import { Flex, Image, Spinner, Text, Icon, Box } from "@chakra-ui/core";
|
|
|
|
|
|
|
|
import { Delay } from "./util";
|
|
|
|
|
2020-04-24 00:28:00 -07:00
|
|
|
import "./OutfitPreview.css";
|
|
|
|
|
2020-04-24 19:16:24 -07:00
|
|
|
function OutfitPreview({ outfitState }) {
|
|
|
|
const { wornItemIds, speciesId, colorId } = outfitState;
|
|
|
|
|
2020-04-23 13:31:39 -07:00
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
2020-04-24 19:16:24 -07:00
|
|
|
query($wornItemIds: [ID!]!, $speciesId: ID!, $colorId: ID!) {
|
2020-04-23 14:23:46 -07:00
|
|
|
petAppearance(speciesId: $speciesId, colorId: $colorId) {
|
|
|
|
layers {
|
|
|
|
id
|
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
2020-04-23 14:44:06 -07:00
|
|
|
id
|
2020-04-23 14:23:46 -07:00
|
|
|
depth
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 14:44:06 -07:00
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
2020-04-23 14:23:46 -07:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:16:24 -07:00
|
|
|
items(ids: $wornItemIds) {
|
2020-04-23 13:31:39 -07:00
|
|
|
id
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
|
|
|
layers {
|
|
|
|
id
|
|
|
|
imageUrl(size: SIZE_600)
|
2020-04-23 14:23:46 -07:00
|
|
|
zone {
|
2020-04-23 14:44:06 -07:00
|
|
|
id
|
2020-04-23 14:23:46 -07:00
|
|
|
depth
|
|
|
|
}
|
2020-04-23 13:31:39 -07:00
|
|
|
}
|
2020-04-23 14:44:06 -07:00
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
2020-04-23 13:31:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2020-04-23 23:43:39 -07:00
|
|
|
{
|
2020-04-24 19:16:24 -07:00
|
|
|
variables: { wornItemIds, speciesId, colorId },
|
2020-04-23 23:43:39 -07:00
|
|
|
}
|
2020-04-23 13:31:39 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Text color="gray.50" d="flex" alignItems="center">
|
|
|
|
<Icon name="warning" />
|
|
|
|
<Box width={2} />
|
|
|
|
Could not load preview. Try again?
|
|
|
|
</Text>
|
|
|
|
</FullScreenCenter>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:19:36 -07:00
|
|
|
return (
|
|
|
|
<Box pos="relative" height="100%" width="100%">
|
2020-04-24 00:28:00 -07:00
|
|
|
<TransitionGroup>
|
|
|
|
{getVisibleLayers(data).map((layer) => (
|
|
|
|
<CSSTransition
|
|
|
|
key={layer.id}
|
|
|
|
classNames={{
|
|
|
|
exit: "outfit-preview-layer-exit",
|
|
|
|
exitActive: "outfit-preview-layer-exit-active",
|
|
|
|
}}
|
|
|
|
timeout={200}
|
|
|
|
>
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Image
|
|
|
|
src={layer.imageUrl}
|
|
|
|
objectFit="contain"
|
|
|
|
maxWidth="100%"
|
|
|
|
maxHeight="100%"
|
|
|
|
className="outfit-preview-layer-image"
|
|
|
|
/>
|
|
|
|
</FullScreenCenter>
|
|
|
|
</CSSTransition>
|
|
|
|
))}
|
|
|
|
</TransitionGroup>
|
2020-04-23 15:19:36 -07:00
|
|
|
{loading && (
|
|
|
|
<Delay>
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Box
|
|
|
|
width="100%"
|
|
|
|
height="100%"
|
|
|
|
backgroundColor="gray.900"
|
|
|
|
opacity="0.8"
|
|
|
|
/>
|
|
|
|
</FullScreenCenter>
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Spinner color="green.400" size="xl" />
|
|
|
|
</FullScreenCenter>
|
|
|
|
</Delay>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVisibleLayers(data) {
|
|
|
|
if (!data) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:44:06 -07:00
|
|
|
const allAppearances = [
|
|
|
|
data.petAppearance,
|
2020-04-23 23:43:39 -07:00
|
|
|
...(data.items || []).map((i) => i.appearanceOn),
|
|
|
|
].filter((a) => a);
|
|
|
|
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;
|
|
|
|
});
|
2020-04-23 14:44:06 -07:00
|
|
|
|
|
|
|
const allRestrictedZoneIds = allAppearances
|
|
|
|
.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);
|
2020-04-23 14:23:46 -07:00
|
|
|
|
2020-04-23 15:19:36 -07:00
|
|
|
return visibleLayers;
|
2020-04-23 13:31:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function FullScreenCenter({ children }) {
|
|
|
|
return (
|
|
|
|
<Flex
|
2020-04-23 15:19:36 -07:00
|
|
|
pos="absolute"
|
|
|
|
top="0"
|
|
|
|
right="0"
|
|
|
|
bottom="0"
|
|
|
|
left="0"
|
2020-04-23 13:31:39 -07:00
|
|
|
alignItems="center"
|
|
|
|
justifyContent="center"
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OutfitPreview;
|