import React, { useMemo } from 'react' import withDefaults from '../utils/with-defaults' import useTheme from '../use-theme' import { useProportions } from '../utils/calculations' import { HuiThemesPalette } from '../themes' interface Props { value?: number limit?: number color?: string className?: string } const defaultProps = { value: 0, limit: 100, color: '', className: '' } type NativeAttrs = Omit, keyof Props> export type CapacityProps = Props & typeof defaultProps & NativeAttrs const getColor = (val: number, palette: HuiThemesPalette): string => { if (val < 33) return palette.viridian if (val < 66) return palette.warning return palette.errorDark } const Capacity: React.FC = ({ value, limit, color: userColor, className, ...props }) => { const theme = useTheme() const percentValue = useProportions(value, limit) const color = useMemo(() => { if (userColor && userColor !== '') return userColor return getColor(percentValue, theme.palette) }, [userColor, percentValue, theme.palette]) return (
) } const MemoCapacity = React.memo(Capacity) export default withDefaults(MemoCapacity, defaultProps)