All files / comparison_chart/components/ChartTooltip index.jsx

40.91% Statements 9/22
0% Branches 0/4
0% Functions 0/7
40.91% Lines 9/22
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                          2x                                                             2x                                             2x                 2x                                       2x                   2x                   2x       2x                             2x                        
import PropTypes from 'prop-types';
import React from 'react';
import Text from '@bufferapp/components/Text';
import moment from 'moment-timezone';
 
import {
  MetricIcon,
  TruncatedNumber,
} from '@bufferapp/analyze-shared-components';
 
import { white } from '@bufferapp/components/style/color';
import styled from 'styled-components';
 
const Wrapper = styled.div`
  cursor: default;
  width: 240px;
  padding: 10px;
  color: ${white};
  font-size: 1rem;
  pointer-events: none;
  white-space: normal;
  box-sizing: border-box;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
`;
 
function transformLabelForTooltip(label) {
  return `${label.toLowerCase()}`;
}
 
function postsWording(profileService) {
  let wording = '';
  switch (profileService) {
    case 'twitter':
      wording = 'tweet';
      break;
    default:
      wording = 'post';
      break;
  }
  return wording;
}
 
const MetricEntry = ({
  metric,
}) => (
  <span
    style={{
      whiteSpace: 'nowrap',
      overflow: 'hidden',
      textOverflow: 'ellipsis',
      display: 'inline-box',
    }}
  >
    <br />
    <Text color="white" size="small" weight="bold" >
      <MetricIcon metric={{ color: metric.color }} /> <TruncatedNumber>
        {metric.value}
      </TruncatedNumber>
    </Text>
    <Text color="white" size="small" > {transformLabelForTooltip(metric.label)}</Text>
    <Text color="white" size="small" > for </Text>
    <Text color="white" size="small" weight="bold" > {metric.username} </Text>
  </span>
);
 
MetricEntry.propTypes = {
  metric: PropTypes.shape({
    color: PropTypes.string,
    label: PropTypes.string,
    username: PropTypes.string,
    value: PropTypes.number,
  }).isRequired,
};
 
const NoDataEntry = ({
  metric,
}) => (
  <span
    style={{
      whiteSpace: 'nowrap',
      overflow: 'hidden',
      textOverflow: 'ellipsis',
      display: 'inline-box',
    }}
  >
    <br />
    <Text color="white" size="small" weight="bold" >
      <MetricIcon metric={{ color: metric.color }} />
    </Text>
    <Text color="white" size="small" > No {postsWording(metric.profileService)} were published for</Text>
    <Text color="white" size="small" weight="bold" > {metric.username} </Text>
  </span>
);
 
NoDataEntry.propTypes = {
  metric: PropTypes.shape({
    color: PropTypes.string,
    label: PropTypes.string,
    profileService: PropTypes.string,
    username: PropTypes.string,
    value: PropTypes.number,
  }).isRequired,
};
 
const Header = ({
  day,
}) => (
  <span>
    <Text color="mystic" size="small" >{moment.utc(day).format('D MMMM')}</Text>
    <br />
    <br />
  </span>
);
 
Header.propTypes = {
  day: PropTypes.number.isRequired,
};
 
const ComparisonChartTooltip = ({
  day,
  metrics,
}) => (
  <Wrapper>
    <Header day={day} />
    <span>
      {metrics.map(m => (m.value === null ?
        (<NoDataEntry key={m.username} metric={m} />) :
        (<MetricEntry key={m.username} metric={m} />)
      ))}
    </span>
  </Wrapper>
);
 
ComparisonChartTooltip.propTypes = {
  day: PropTypes.number.isRequired,
  metrics: PropTypes.arrayOf(PropTypes.shape({
    color: PropTypes.string,
    label: PropTypes.string,
    profileService: PropTypes.string,
    username: PropTypes.string,
    value: PropTypes.number,
  })).isRequired,
};
 
export default ComparisonChartTooltip;