Skip to content
Snippets Groups Projects
Hash.tsx 2.31 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hugo NOUTS's avatar
    Hugo NOUTS committed
    import React, { useState, useEffect } from 'react'
    import { ScaleBand } from 'd3-scale'
    import { ITimePeriod } from 'services/dataConsumptionContracts'
    import { DateTime } from 'luxon'
    
    interface HashProps {
      challengePeriod: ITimePeriod | null
      multiFluid: boolean
      xScale: ScaleBand<string>
      padding: number
      width: number
      height: number
    }
    
    const Hash = (props: HashProps) => {
      const { challengePeriod, multiFluid, xScale, padding, width, height } = props
    
      /*
        number array with
        - the start position of the area
        - the width of the area
      */
      const [scale, setScale] = useState<number[]>([])
    
      useEffect(() => {
        function loadScale() {
          if (challengePeriod) {
            const startScale = xScale(
              challengePeriod.startDate
                .startOf('day')
                .toLocaleString(DateTime.DATETIME_SHORT)
            )
            const endScale = xScale(
              challengePeriod.endDate
                .startOf('day')
                .toLocaleString(DateTime.DATETIME_SHORT)
            )
            if (!startScale && !endScale) {
              setScale([])
            } else if (startScale && !endScale) {
              setScale([startScale - padding, width - startScale])
            } else if (!startScale && endScale) {
              setScale([0, endScale - padding])
            } else if (startScale && endScale) {
              setScale([startScale - padding, endScale - startScale])
            }
          } else {
            setScale([])
          }
        }
        loadScale()
      }, [xScale])
    
      return (
        <g>
          {scale.length > 0 && multiFluid ? (
            <g>
              <g transform={`translate(${scale[0] + 1}, -40)`}>
                <defs>
                  <pattern
                    id="hash"
                    width="8"
                    height="8"
                    patternUnits="userSpaceOnUse"
                    patternTransform="rotate(45)"
                  >
                    <rect width="3" height="8" fill="#255454"></rect>
                  </pattern>
                </defs>
                <rect
                  x="0"
                  y="0"
                  width={scale[1]}
                  height={height + 40}
                  fill="url(#hash)"
                />
              </g>
              <g transform={`translate(${scale[0] + 1}, ${height - 2})`}>
                <rect x="0" y="0" width={scale[1]} height={2} fill="#61F0F2" />
              </g>
            </g>
          ) : null}
        </g>
      )
    }
    
    export default Hash