2023-08-10 15:56:36 -07:00
|
|
|
import * as React from "react";
|
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery, useMutation } from "@apollo/client";
|
|
|
|
import {
|
2024-09-09 16:10:45 -07:00
|
|
|
Badge,
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
Drawer,
|
|
|
|
DrawerBody,
|
|
|
|
DrawerCloseButton,
|
|
|
|
DrawerContent,
|
|
|
|
DrawerHeader,
|
|
|
|
DrawerOverlay,
|
|
|
|
Flex,
|
|
|
|
FormControl,
|
|
|
|
FormErrorMessage,
|
|
|
|
FormHelperText,
|
|
|
|
FormLabel,
|
|
|
|
HStack,
|
|
|
|
Link,
|
|
|
|
Select,
|
|
|
|
Spinner,
|
|
|
|
Stack,
|
|
|
|
Text,
|
|
|
|
useBreakpointValue,
|
|
|
|
useColorModeValue,
|
|
|
|
useDisclosure,
|
2023-08-10 15:56:36 -07:00
|
|
|
} from "@chakra-ui/react";
|
|
|
|
import {
|
2024-09-09 16:10:45 -07:00
|
|
|
CheckCircleIcon,
|
|
|
|
ChevronRightIcon,
|
|
|
|
ExternalLinkIcon,
|
2023-08-10 15:56:36 -07:00
|
|
|
} from "@chakra-ui/icons";
|
|
|
|
|
|
|
|
import AllItemLayersSupportModal from "./AllItemLayersSupportModal";
|
|
|
|
import Metadata, { MetadataLabel, MetadataValue } from "./Metadata";
|
|
|
|
import useOutfitAppearance from "../../components/useOutfitAppearance";
|
|
|
|
import { OutfitStateContext } from "../useOutfitState";
|
|
|
|
import useSupport from "./useSupport";
|
|
|
|
import ItemSupportAppearanceLayer from "./ItemSupportAppearanceLayer";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ItemSupportDrawer shows Support UI for the item when open.
|
|
|
|
*
|
|
|
|
* This component controls the drawer element. The actual content is imported
|
|
|
|
* from another lazy-loaded component!
|
|
|
|
*/
|
|
|
|
function ItemSupportDrawer({ item, isOpen, onClose }) {
|
2024-09-09 16:10:45 -07:00
|
|
|
const placement = useBreakpointValue({ base: "bottom", lg: "right" });
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return (
|
|
|
|
<Drawer
|
|
|
|
placement={placement}
|
|
|
|
size="md"
|
|
|
|
isOpen={isOpen}
|
|
|
|
onClose={onClose}
|
|
|
|
// blockScrollOnMount doesn't matter on our fullscreen UI, but the
|
|
|
|
// default implementation breaks out layout somehow 🤔 idk, let's not!
|
|
|
|
blockScrollOnMount={false}
|
|
|
|
>
|
|
|
|
<DrawerOverlay>
|
|
|
|
<DrawerContent
|
|
|
|
maxHeight={placement === "bottom" ? "90vh" : undefined}
|
|
|
|
overflow="auto"
|
|
|
|
>
|
|
|
|
<DrawerCloseButton />
|
|
|
|
<DrawerHeader>
|
|
|
|
{item.name}
|
|
|
|
<Badge colorScheme="pink" marginLeft="3">
|
|
|
|
Support <span aria-hidden="true">💖</span>
|
|
|
|
</Badge>
|
|
|
|
</DrawerHeader>
|
|
|
|
<DrawerBody paddingBottom="5">
|
|
|
|
<Metadata>
|
|
|
|
<MetadataLabel>Item ID:</MetadataLabel>
|
|
|
|
<MetadataValue>{item.id}</MetadataValue>
|
|
|
|
<MetadataLabel>Restricted zones:</MetadataLabel>
|
|
|
|
<MetadataValue>
|
|
|
|
<ItemSupportRestrictedZones item={item} />
|
|
|
|
</MetadataValue>
|
|
|
|
</Metadata>
|
|
|
|
<Stack spacing="8" marginTop="6">
|
|
|
|
<ItemSupportFields item={item} />
|
|
|
|
<ItemSupportAppearanceLayers item={item} />
|
|
|
|
</Stack>
|
|
|
|
</DrawerBody>
|
|
|
|
</DrawerContent>
|
|
|
|
</DrawerOverlay>
|
|
|
|
</Drawer>
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function ItemSupportRestrictedZones({ item }) {
|
2024-09-09 16:10:45 -07:00
|
|
|
const { speciesId, colorId } = React.useContext(OutfitStateContext);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
// NOTE: It would be a better reflection of the data to just query restricted
|
|
|
|
// zones right off the item... but we already have them in cache from
|
|
|
|
// the appearance, so query them that way to be instant in practice!
|
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
|
|
|
query ItemSupportRestrictedZones(
|
|
|
|
$itemId: ID!
|
|
|
|
$speciesId: ID!
|
|
|
|
$colorId: ID!
|
|
|
|
) {
|
|
|
|
item(id: $itemId) {
|
|
|
|
id
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{ variables: { itemId: item.id, speciesId, colorId } },
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
if (loading) {
|
|
|
|
return <Spinner size="xs" />;
|
|
|
|
}
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
if (error) {
|
|
|
|
return <Text color="red.400">{error.message}</Text>;
|
|
|
|
}
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const restrictedZones = data?.item?.appearanceOn?.restrictedZones || [];
|
|
|
|
if (restrictedZones.length === 0) {
|
|
|
|
return "None";
|
|
|
|
}
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return restrictedZones
|
|
|
|
.map((z) => `${z.label} (${z.id})`)
|
|
|
|
.sort()
|
|
|
|
.join(", ");
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function ItemSupportFields({ item }) {
|
2024-09-09 16:10:45 -07:00
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
|
|
|
query ItemSupportFields($itemId: ID!) {
|
|
|
|
item(id: $itemId) {
|
|
|
|
id
|
|
|
|
manualSpecialColor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
explicitlyBodySpecific
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
variables: { itemId: item.id },
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
// HACK: I think it's a bug in @apollo/client 3.1.1 that, if the
|
|
|
|
// optimistic response sets `manualSpecialColor` to null, the query
|
|
|
|
// doesn't update, even though its cache has updated :/
|
|
|
|
//
|
|
|
|
// This cheap trick of changing the display name every re-render
|
|
|
|
// persuades Apollo that this is a different query, so it re-checks
|
|
|
|
// its cache and finds the empty `manualSpecialColor`. Weird!
|
|
|
|
displayName: `ItemSupportFields-${new Date()}`,
|
|
|
|
},
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const errorColor = useColorModeValue("red.500", "red.300");
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{error && <Box color={errorColor}>{error.message}</Box>}
|
|
|
|
<ItemSupportSpecialColorFields
|
|
|
|
loading={loading}
|
|
|
|
error={error}
|
|
|
|
item={item}
|
|
|
|
manualSpecialColor={data?.item?.manualSpecialColor?.id}
|
|
|
|
/>
|
|
|
|
<ItemSupportPetCompatibilityRuleFields
|
|
|
|
loading={loading}
|
|
|
|
error={error}
|
|
|
|
item={item}
|
|
|
|
explicitlyBodySpecific={data?.item?.explicitlyBodySpecific}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function ItemSupportSpecialColorFields({
|
2024-09-09 16:10:45 -07:00
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
item,
|
|
|
|
manualSpecialColor,
|
2023-08-10 15:56:36 -07:00
|
|
|
}) {
|
2024-09-09 16:10:45 -07:00
|
|
|
const { supportSecret } = useSupport();
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const {
|
|
|
|
loading: colorsLoading,
|
|
|
|
error: colorsError,
|
|
|
|
data: colorsData,
|
|
|
|
} = useQuery(gql`
|
|
|
|
query ItemSupportDrawerAllColors {
|
|
|
|
allColors {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
isStandard
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const [
|
|
|
|
mutate,
|
|
|
|
{ loading: mutationLoading, error: mutationError, data: mutationData },
|
|
|
|
] = useMutation(gql`
|
|
|
|
mutation ItemSupportDrawerSetManualSpecialColor(
|
|
|
|
$itemId: ID!
|
|
|
|
$colorId: ID
|
|
|
|
$supportSecret: String!
|
|
|
|
) {
|
|
|
|
setManualSpecialColor(
|
|
|
|
itemId: $itemId
|
|
|
|
colorId: $colorId
|
|
|
|
supportSecret: $supportSecret
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
manualSpecialColor {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const onChange = React.useCallback(
|
|
|
|
(e) => {
|
|
|
|
const colorId = e.target.value || null;
|
|
|
|
const color =
|
|
|
|
colorId != null ? { __typename: "Color", id: colorId } : null;
|
|
|
|
mutate({
|
|
|
|
variables: {
|
|
|
|
itemId: item.id,
|
|
|
|
colorId,
|
|
|
|
supportSecret,
|
|
|
|
},
|
|
|
|
optimisticResponse: {
|
|
|
|
__typename: "Mutation",
|
|
|
|
setManualSpecialColor: {
|
|
|
|
__typename: "Item",
|
|
|
|
id: item.id,
|
|
|
|
manualSpecialColor: color,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).catch((e) => {
|
|
|
|
// Ignore errors from the promise, because we'll handle them on render!
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[item.id, mutate, supportSecret],
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const nonStandardColors =
|
|
|
|
colorsData?.allColors?.filter((c) => !c.isStandard) || [];
|
|
|
|
nonStandardColors.sort((a, b) => a.name.localeCompare(b.name));
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const linkColor = useColorModeValue("green.500", "green.300");
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return (
|
|
|
|
<FormControl isInvalid={Boolean(error || colorsError || mutationError)}>
|
|
|
|
<FormLabel>Special color</FormLabel>
|
|
|
|
<Select
|
|
|
|
placeholder={
|
|
|
|
loading || colorsLoading
|
|
|
|
? "Loading…"
|
|
|
|
: "Default: Auto-detect from item description"
|
|
|
|
}
|
|
|
|
value={manualSpecialColor?.id}
|
|
|
|
isDisabled={mutationLoading}
|
|
|
|
icon={
|
|
|
|
loading || colorsLoading || mutationLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : mutationData ? (
|
|
|
|
<CheckCircleIcon />
|
|
|
|
) : undefined
|
|
|
|
}
|
|
|
|
onChange={onChange}
|
|
|
|
>
|
|
|
|
{nonStandardColors.map((color) => (
|
|
|
|
<option key={color.id} value={color.id}>
|
|
|
|
{color.name}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
{colorsError && (
|
|
|
|
<FormErrorMessage>{colorsError.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
{mutationError && (
|
|
|
|
<FormErrorMessage>{mutationError.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
{!colorsError && !mutationError && (
|
|
|
|
<FormHelperText>
|
|
|
|
This controls which previews we show on the{" "}
|
|
|
|
<Link
|
|
|
|
href={`https://impress.openneo.net/items/${
|
|
|
|
item.id
|
|
|
|
}-${item.name.replace(/ /g, "-")}`}
|
|
|
|
color={linkColor}
|
|
|
|
isExternal
|
|
|
|
>
|
|
|
|
classic item page <ExternalLinkIcon />
|
|
|
|
</Link>
|
|
|
|
.
|
|
|
|
</FormHelperText>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function ItemSupportPetCompatibilityRuleFields({
|
2024-09-09 16:10:45 -07:00
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
item,
|
|
|
|
explicitlyBodySpecific,
|
2023-08-10 15:56:36 -07:00
|
|
|
}) {
|
2024-09-09 16:10:45 -07:00
|
|
|
const { supportSecret } = useSupport();
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const [
|
|
|
|
mutate,
|
|
|
|
{ loading: mutationLoading, error: mutationError, data: mutationData },
|
|
|
|
] = useMutation(gql`
|
|
|
|
mutation ItemSupportDrawerSetItemExplicitlyBodySpecific(
|
|
|
|
$itemId: ID!
|
|
|
|
$explicitlyBodySpecific: Boolean!
|
|
|
|
$supportSecret: String!
|
|
|
|
) {
|
|
|
|
setItemExplicitlyBodySpecific(
|
|
|
|
itemId: $itemId
|
|
|
|
explicitlyBodySpecific: $explicitlyBodySpecific
|
|
|
|
supportSecret: $supportSecret
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
explicitlyBodySpecific
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const onChange = React.useCallback(
|
|
|
|
(e) => {
|
|
|
|
const explicitlyBodySpecific = e.target.value === "true";
|
|
|
|
mutate({
|
|
|
|
variables: {
|
|
|
|
itemId: item.id,
|
|
|
|
explicitlyBodySpecific,
|
|
|
|
supportSecret,
|
|
|
|
},
|
|
|
|
optimisticResponse: {
|
|
|
|
__typename: "Mutation",
|
|
|
|
setItemExplicitlyBodySpecific: {
|
|
|
|
__typename: "Item",
|
|
|
|
id: item.id,
|
|
|
|
explicitlyBodySpecific,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}).catch((e) => {
|
|
|
|
// Ignore errors from the promise, because we'll handle them on render!
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[item.id, mutate, supportSecret],
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return (
|
|
|
|
<FormControl isInvalid={Boolean(error || mutationError)}>
|
|
|
|
<FormLabel>Pet compatibility rule</FormLabel>
|
|
|
|
<Select
|
|
|
|
value={explicitlyBodySpecific ? "true" : "false"}
|
|
|
|
isDisabled={mutationLoading}
|
|
|
|
icon={
|
|
|
|
loading || mutationLoading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : mutationData ? (
|
|
|
|
<CheckCircleIcon />
|
|
|
|
) : undefined
|
|
|
|
}
|
|
|
|
onChange={onChange}
|
|
|
|
>
|
|
|
|
{loading ? (
|
|
|
|
<option>Loading…</option>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<option value="false">
|
|
|
|
Default: Auto-detect whether this fits all pets
|
|
|
|
</option>
|
|
|
|
<option value="true">
|
|
|
|
Body specific: Always different for each pet body
|
|
|
|
</option>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Select>
|
|
|
|
{mutationError && (
|
|
|
|
<FormErrorMessage>{mutationError.message}</FormErrorMessage>
|
|
|
|
)}
|
|
|
|
{!mutationError && (
|
|
|
|
<FormHelperText>
|
|
|
|
By default, we assume Background-y zones fit all pets the same. When
|
|
|
|
items don't follow that rule, we can override it.
|
|
|
|
</FormHelperText>
|
|
|
|
)}
|
|
|
|
</FormControl>
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: This component takes `outfitState` from context, rather than as a prop
|
|
|
|
* from its parent, for performance reasons. We want `Item` to memoize
|
|
|
|
* and generally skip re-rendering on `outfitState` changes, and to make
|
|
|
|
* sure the context isn't accessed when the drawer is closed. So we use
|
|
|
|
* it here, only when the drawer is open!
|
|
|
|
*/
|
|
|
|
function ItemSupportAppearanceLayers({ item }) {
|
2024-09-09 16:10:45 -07:00
|
|
|
const outfitState = React.useContext(OutfitStateContext);
|
|
|
|
const { speciesId, colorId, pose, altStyleId, appearanceId } = outfitState;
|
|
|
|
const { error, visibleLayers } = useOutfitAppearance({
|
|
|
|
speciesId,
|
|
|
|
colorId,
|
|
|
|
pose,
|
|
|
|
altStyleId,
|
|
|
|
appearanceId,
|
|
|
|
wornItemIds: [item.id],
|
|
|
|
});
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const biologyLayers = visibleLayers.filter((l) => l.source === "pet");
|
|
|
|
const itemLayers = visibleLayers.filter((l) => l.source === "item");
|
|
|
|
itemLayers.sort((a, b) => a.zone.depth - b.zone.depth);
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
const modalState = useDisclosure();
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2024-09-09 16:10:45 -07:00
|
|
|
return (
|
|
|
|
<FormControl>
|
|
|
|
<Flex align="center">
|
|
|
|
<FormLabel>Appearance layers</FormLabel>
|
|
|
|
<Box width="4" flex="1 0 auto" />
|
|
|
|
<Button size="xs" onClick={modalState.onOpen}>
|
|
|
|
View on all pets <ChevronRightIcon />
|
|
|
|
</Button>
|
|
|
|
<AllItemLayersSupportModal
|
|
|
|
item={item}
|
|
|
|
isOpen={modalState.isOpen}
|
|
|
|
onClose={modalState.onClose}
|
|
|
|
/>
|
|
|
|
</Flex>
|
|
|
|
<HStack spacing="4" overflow="auto" paddingX="1">
|
|
|
|
{itemLayers.map((itemLayer) => (
|
|
|
|
<ItemSupportAppearanceLayer
|
|
|
|
key={itemLayer.id}
|
|
|
|
item={item}
|
|
|
|
itemLayer={itemLayer}
|
|
|
|
biologyLayers={biologyLayers}
|
|
|
|
outfitState={outfitState}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</HStack>
|
|
|
|
{error && <FormErrorMessage>{error.message}</FormErrorMessage>}
|
|
|
|
</FormControl>
|
|
|
|
);
|
2023-08-10 15:56:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ItemSupportDrawer;
|