import React, { useContext } from 'react'
import emoji from 'react-easy-emoji'
import { animated, config, useSpring } from 'react-spring'
import useDisplayOnIntersecting from 'Components/utils/useDisplayOnIntersecting'
import { ThemeColorsContext } from 'Components/utils/colors'
import { formatValue } from 'Engine/format'
import { useTranslation } from 'react-i18next'
const ANIMATION_SPRING = config.gentle
type ChartItemBarProps = {
numberToPlot: number
unit?: string
style: React.CSSProperties
}
function ChartItemBar({ style, numberToPlot, unit }: ChartItemBarProps) {
const language = useTranslation().i18n.language
return (
{formatValue({ nodeValue: numberToPlot, unit, precision: 0, language })}
)
}
function BranchIcon({ icon }: { icon: string }) {
return (
{emoji(icon)}
)
}
type BarChartBranchProps = {
value: number
title: React.ReactNode
icon?: string
maximum: number
description?: string
unit?: string
}
export default function BarChartBranch({
value,
title,
icon,
maximum,
description,
unit
}: BarChartBranchProps) {
const [intersectionRef, brancheInViewport] = useDisplayOnIntersecting({
threshold: 0.5
})
const { color } = useContext(ThemeColorsContext)
const numberToPlot = brancheInViewport ? value : 0
const styles = useSpring({
config: ANIMATION_SPRING,
to: {
flex: numberToPlot / maximum,
opacity: numberToPlot ? 1 : 0
}
}) as { flex: number; opacity: number } // TODO: problème avec les types de react-spring ?
return (
{icon && }
{title}
{description && {description}}
)
}