import { orderBy } from "lodash"; import React from "react"; import { Tooltip } from "@dndbeyond/character-common-components/es"; import { ResistanceIcon, ImmunityIcon, VulnerabilityIcon, } from "@dndbeyond/character-components/es"; import { CharacterTheme, DefenseAdjustmentGroup, FormatUtils, } from "@dndbeyond/character-rules-engine/es"; const DEFENSE_GROUP = { RESISTANCE: "Resistance", IMMUNITY: "Immunity", VULNERABILITY: "Vulnerability", }; interface Props { resistances: Array; immunities: Array; vulnerabilities: Array; onClick?: () => void; theme?: CharacterTheme; } export default class DefensesSummary extends React.PureComponent { handleClick = (evt: React.MouseEvent): void => { const { onClick } = this.props; if (onClick) { evt.stopPropagation(); evt.nativeEvent.stopImmediatePropagation(); onClick(); } }; renderDamageAdjustmentList = ( defenses: Array ): React.ReactNode => { const { theme } = this.props; let orderedDefenses = orderBy(defenses, (group) => group.name); return ( {orderedDefenses.map((defense: DefenseAdjustmentGroup, idx) => ( {defense.name} {defense.hasCustom ? "*" : ""} ))} ); //` }; renderDamageAdjustmentGroup = ( groupType: string, label: string, damageAdjustments: Array, isSingleLine: boolean, theme: CharacterTheme | undefined ): React.ReactNode => { if (!damageAdjustments.length) { return null; } let classNames: Array = ["ct-defenses-summary__group"]; if (isSingleLine) { classNames.push("ct-defenses-summary__group--single"); } let iconComponent: React.ReactNode; switch (FormatUtils.slugify(groupType)) { case "resistance": iconComponent = ; break; case "immunity": iconComponent = ; break; case "vulnerability": iconComponent = ; break; default: iconComponent = null; break; } return (
{iconComponent} {this.renderDamageAdjustmentList(damageAdjustments)}
); //` }; renderDefault = (): React.ReactNode => { const { theme } = this.props; return (
Resistances, Immunities, or Vulnerabilities
); }; renderDefenseGroups = (): React.ReactNode => { const { resistances, vulnerabilities, immunities, theme } = this.props; let isSingleLine: boolean = resistances.length > 0 && vulnerabilities.length > 0 && immunities.length > 0; return ( {this.renderDamageAdjustmentGroup( DEFENSE_GROUP.RESISTANCE, "Resistances", resistances, isSingleLine, theme )} {this.renderDamageAdjustmentGroup( DEFENSE_GROUP.IMMUNITY, "Immunity", immunities, isSingleLine, theme )} {this.renderDamageAdjustmentGroup( DEFENSE_GROUP.VULNERABILITY, "Vulnerabilities", vulnerabilities, isSingleLine, theme )} ); }; render() { const { resistances, vulnerabilities, immunities } = this.props; let contentNode: React.ReactNode; if (!resistances.length && !vulnerabilities.length && !immunities.length) { contentNode = this.renderDefault(); } else { contentNode = this.renderDefenseGroups(); } return (
{contentNode}
); } }