# All files / components LatestValueTransform.tsx

20% Statements 4/20
0% Branches 0/17
0% Functions 0/2
21.05% Lines 4/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    1x

    1x
    1x

    1x

     // Libraries
    import React, {useMemo, FunctionComponent} from 'react'
    import {Table} from '../types'

    // Utils
    import {getLatestValues} from '../utils/getLatestValues'
    import {isString} from '../utils/isString'

    interface Props {
      table: Table
      children: (latestValue: number) => JSX.Element
      allowString: boolean
      // If `quiet` is set and a latest value can't be found, this component will
      // display nothing instead of an empty graph error message
      quiet?: boolean
    }

    export const LatestValueTransform: FunctionComponent<Props> = ({
      table,
      quiet = false,
      children,
      allowString,
    }) => {
      const latestValues = useMemo(() => getLatestValues(table), [table])

      if (latestValues.length === 0 && quiet) {
        return null
      }

      if (latestValues.length === 0) {
        return (
          <div>
            <h4>No latest value found</h4>
          </div>
        )
      }

      const latestValue = latestValues[0]

      if (isString(latestValue) && !allowString && quiet) {
        return null
      }

      if (isString(latestValue) && !allowString) {
        return (
          <div>
            <h4>String value cannot be displayed in this graph type</h4>
          </div>
        )
      }

      return children(latestValue)
    }