import classNames from 'classnames' import { useMemo } from 'react' import HealthIcon from 'components/Account/Health/HealthIcon' import HealthTooltip from 'components/Account/Health/HealthTooltip' import { DEFAULT_SETTINGS } from 'constants/defaultSettings' import { LocalStorageKeys } from 'constants/localStorageKeys' import useHealthColor from 'hooks/useHealthColor' import useLocalStorage from 'hooks/useLocalStorage' import { getHealthIndicatorColors } from 'utils/healthIndicator' interface Props { className?: string hasLabel?: boolean health: number healthFactor: number height?: string iconClassName?: string updatedHealth?: number updatedHealthFactor?: number showIcon?: boolean } function calculateHealth(health: number): number { const firstBarEnd = 43 const secondBarStart = 46 const secondBarEnd = 93 const thirdBarStart = 96 const thirdBarEnd = 184 if (health <= 0) return 0 if (health <= 10) return (firstBarEnd / 10) * health if (health <= 30) return ((secondBarEnd - secondBarStart) / 20) * (health - 10) + secondBarStart return ((thirdBarEnd - thirdBarStart) / 70) * (health - 30) + thirdBarStart } export default function HealthBar({ health = 0, updatedHealth = 0, healthFactor = 0, updatedHealthFactor = 0, className, height = '4', iconClassName = 'w-5', showIcon = false, }: Props) { const [reduceMotion] = useLocalStorage( LocalStorageKeys.REDUCE_MOTION, DEFAULT_SETTINGS.reduceMotion, ) const width = calculateHealth(health) const updatedWidth = calculateHealth(updatedHealth ?? 0) const isUpdated = updatedWidth > 0 && updatedWidth !== width const isIncrease = isUpdated && updatedWidth > width const color = useHealthColor(health, 'fill') const updatedColor = useHealthColor(updatedHealth ?? 0, 'fill') const [backgroundColor, foreGroundColor] = useMemo( () => getHealthIndicatorColors(color, updatedColor, 'fill', isUpdated, isIncrease), [color, updatedColor, isUpdated, isIncrease], ) return ( <> {showIcon && ( )}
{isUpdated && ( )}
) }