import clsx from "clsx"; import { FC, HTMLAttributes, MouseEvent } from "react"; import { characterActions, Item, ItemPlan, ItemPlanUtils, ItemUtils, } from "@dndbeyond/character-rules-engine"; import { Button } from "~/components/Button"; import { useSidebar } from "~/contexts/Sidebar"; import { useCharacterEngine } from "~/hooks/useCharacterEngine"; import { useDispatch } from "~/hooks/useDispatch"; import { PaneIdentifierUtils } from "~/tools/js/Shared/utils"; import ItemPreview from "~/tools/js/smartComponents/ItemPreview"; import { PaneComponentEnum } from "../Sidebar/types"; import styles from "./styles.module.css"; export interface MagicItemPlansProps extends HTMLAttributes {} export const MagicItemPlans: FC = ({ className, ...props }) => { const { characterTheme, itemPlans, maxReplicatedItemsCount } = useCharacterEngine(); const { pane: { paneHistoryStart }, } = useSidebar(); const dispatch = useDispatch(); const itemPlanWithMappingsCount = itemPlans.filter((plan) => ItemPlanUtils.getItem(plan) ).length; const atMax = maxReplicatedItemsCount !== null && itemPlanWithMappingsCount >= maxReplicatedItemsCount; const handleItemPlansManagePaneShow = ( e: MouseEvent ): void => { e.preventDefault(); e.stopPropagation(); paneHistoryStart(PaneComponentEnum.ITEM_PLANS_MANAGE); }; const handleItemClick = (item: Item): void => { const inventoryMappingId = ItemUtils.getMappingId(item); if (inventoryMappingId) { paneHistoryStart( PaneComponentEnum.ITEM_DETAIL, PaneIdentifierUtils.generateItem(inventoryMappingId) ); } }; const handleReplicateItem = (itemPlan: ItemPlan): void => { const itemDefinitionKey = ItemPlanUtils.getItemDefinitionKey(itemPlan); const itemPlanOriginDefinitionKey = ItemPlanUtils.getOriginDefinitionKey(itemPlan); if (itemDefinitionKey) { dispatch( characterActions.itemPlanInventoryMappingCreate( itemDefinitionKey, 1, itemPlanOriginDefinitionKey ) ); } }; return (
{itemPlans.length > 0 ? ( itemPlans.map((plan) => { const planOriginDefinitionKey = ItemPlanUtils.getOriginDefinitionKey(plan); const planName = ItemPlanUtils.getItemName(plan); const itemDefinitionKey = ItemPlanUtils.getItemDefinitionKey(plan); const item = ItemPlanUtils.getItem(plan); const planDescription = ItemPlanUtils.getDescription(plan); return itemDefinitionKey ? (

{planName}

{planDescription}

{item ? (
handleItemClick(item)} />
) : ( )}
) : null; }) ) : (
No Magic Item plans have been selected for this character
)}
); };