2021-01-08 01:23:24 -08:00
|
|
|
import React from "react";
|
2021-06-17 15:37:01 -07:00
|
|
|
import { Box, Center, DarkMode } from "@chakra-ui/react";
|
2021-01-08 01:23:24 -08:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/client";
|
2021-05-05 00:22:28 -07:00
|
|
|
import * as Sentry from "@sentry/react";
|
2021-01-08 01:23:24 -08:00
|
|
|
|
2021-09-03 15:37:38 -07:00
|
|
|
import OutfitThumbnail from "../components/OutfitThumbnail";
|
2021-02-10 13:52:20 -08:00
|
|
|
import { useOutfitPreview } from "../components/OutfitPreview";
|
2021-05-05 00:22:28 -07:00
|
|
|
import { loadable, MajorErrorMessage, TestErrorSender } from "../util";
|
2021-01-08 01:23:24 -08:00
|
|
|
|
2021-02-10 12:52:00 -08:00
|
|
|
const OutfitControls = loadable(() => import("./OutfitControls"));
|
|
|
|
|
|
|
|
function WardrobePreviewAndControls({
|
2021-01-08 01:23:24 -08:00
|
|
|
isLoading,
|
|
|
|
outfitState,
|
2021-02-10 12:52:00 -08:00
|
|
|
dispatchToOutfit,
|
2021-01-08 01:23:24 -08:00
|
|
|
}) {
|
2021-02-10 12:52:00 -08:00
|
|
|
// Whether the current outfit preview has animations. Determines whether we
|
|
|
|
// show the play/pause button.
|
|
|
|
const [hasAnimations, setHasAnimations] = React.useState(false);
|
|
|
|
|
2021-02-10 13:35:34 -08:00
|
|
|
const { appearance, preview } = useOutfitPreview({
|
|
|
|
isLoading: isLoading,
|
|
|
|
speciesId: outfitState.speciesId,
|
|
|
|
colorId: outfitState.colorId,
|
|
|
|
pose: outfitState.pose,
|
|
|
|
appearanceId: outfitState.appearanceId,
|
|
|
|
wornItemIds: outfitState.wornItemIds,
|
|
|
|
onChangeHasAnimations: setHasAnimations,
|
2021-04-28 16:16:15 -07:00
|
|
|
placeholder: <OutfitThumbnailIfCached outfitId={outfitState.id} />,
|
2021-04-16 03:04:39 -07:00
|
|
|
"data-test-id": "wardrobe-outfit-preview",
|
2021-02-10 13:35:34 -08:00
|
|
|
});
|
|
|
|
|
2021-01-08 01:23:24 -08:00
|
|
|
return (
|
2021-05-05 00:22:28 -07:00
|
|
|
<Sentry.ErrorBoundary fallback={MajorErrorMessage}>
|
|
|
|
<TestErrorSender />
|
2021-06-17 15:37:01 -07:00
|
|
|
<Center position="absolute" top="0" bottom="0" left="0" right="0">
|
2021-02-10 13:35:34 -08:00
|
|
|
<DarkMode>{preview}</DarkMode>
|
2021-06-17 15:37:01 -07:00
|
|
|
</Center>
|
2021-02-10 12:52:00 -08:00
|
|
|
<Box position="absolute" top="0" bottom="0" left="0" right="0">
|
|
|
|
<OutfitControls
|
|
|
|
outfitState={outfitState}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
|
|
|
showAnimationControls={hasAnimations}
|
2021-02-10 13:35:34 -08:00
|
|
|
appearance={appearance}
|
2021-02-10 12:52:00 -08:00
|
|
|
/>
|
|
|
|
</Box>
|
2021-05-05 00:22:28 -07:00
|
|
|
</Sentry.ErrorBoundary>
|
2021-01-08 01:23:24 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OutfitThumbnailIfCached will render an OutfitThumbnail as a placeholder for
|
|
|
|
* the outfit preview... but only if we already have the data to generate the
|
|
|
|
* thumbnail stored in our local Apollo GraphQL cache.
|
|
|
|
*
|
|
|
|
* This means that, when you come from the Your Outfits page, we can show the
|
|
|
|
* outfit thumbnail instantly while everything else loads. But on direct
|
|
|
|
* navigation, this does nothing, and we just wait for the preview to load in
|
|
|
|
* like usual!
|
|
|
|
*/
|
|
|
|
function OutfitThumbnailIfCached({ outfitId }) {
|
|
|
|
const { data } = useQuery(
|
|
|
|
gql`
|
2021-09-03 15:37:38 -07:00
|
|
|
query OutfitThumbnailIfCached($outfitId: ID!) {
|
2021-01-08 01:23:24 -08:00
|
|
|
outfit(id: $outfitId) {
|
|
|
|
id
|
2021-09-03 15:37:38 -07:00
|
|
|
updatedAt
|
2021-01-08 01:23:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
outfitId,
|
2021-09-03 15:37:38 -07:00
|
|
|
skip: outfitId == null,
|
2021-01-08 01:23:24 -08:00
|
|
|
},
|
|
|
|
fetchPolicy: "cache-only",
|
2021-09-03 15:37:38 -07:00
|
|
|
onError: (e) => console.error(e),
|
2021-01-08 01:23:24 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!data?.outfit) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<OutfitThumbnail
|
2021-09-03 15:37:38 -07:00
|
|
|
outfitId={data.outfit.id}
|
|
|
|
updatedAt={data.outfit.updatedAt}
|
2021-01-08 01:23:24 -08:00
|
|
|
alt=""
|
|
|
|
objectFit="contain"
|
|
|
|
width="100%"
|
|
|
|
height="100%"
|
|
|
|
filter="blur(2px)"
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-10 12:52:00 -08:00
|
|
|
export default WardrobePreviewAndControls;
|