All files / posts-table/components/PostsTable/components/PostItem index.jsx

60% Statements 9/15
0% Branches 0/2
0% Functions 0/3
69.23% Lines 9/13
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                1x                                   1x         1x           1x           1x                   1x                       1x                                     1x                                                                                             1x                                                            
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment-timezone';
import styled from 'styled-components';
import { Text } from '@bufferapp/components';
 
import Attachment from './components/Attachment';
import Metrics from './components/Metrics';
import ViewPost from './components/ViewPost';
 
import {
  lightSlate,
  nevada,
  white
} from '@bufferapp/components/style/color';
 
const Container = styled.li`
  display: grid;
  grid-gap: 0;
  grid-template-columns: 14rem 1fr;
  grid-template-rows: minmax(0, 1fr) auto;
  height: 14rem;
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0;
  background: ${white};
  border: 1px solid #DBDBDB;
  border-radius: 3px;
 
  &:last-of-type {
    margin-bottom: 0;
  }
`;
 
const Side = styled.div`
  grid-column-start: 1;
  grid-row: 1 / span 2;
`;
 
const Top = styled.div`
  grid-column-start: 2;
  grid-row-start: 1;
  padding: 1.25rem 1.25rem 1.25rem 1.5rem;
`;
 
const Bottom = styled.div`
  grid-column-start: 2;
  grid-row-start: 2;
  padding: 1.25rem 1.25rem 1.25rem 1.5rem;
`;
 
const ContentDate = styled.div`
  color: ${nevada};
  margin-bottom: 0.5rem;
  text-decoration: none;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
`;
 
const ContentText = styled.span`
  color: ${lightSlate};
 
  a,
  a:link,
  a:visited,
  a:hover {
    color: ${lightSlate};
    text-decoration: none;
  }
`;
 
const ContentMain = styled.div`
  position: relative;
  padding: 0 10rem 0 0;
  line-height: 1.6em;
  height: 4.8em;
  overflow: hidden;
 
  &:after {
    content: "";
    text-align: right;
    position: absolute;
    bottom: 0;
    right: 0;
    width: 70%;
    height: 1.3em;
    background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 50%);
  }
`;
 
const PostItem = ({
  post,
  engagementMetrics,
  audienceMetrics,
  maxEngagementValue,
  maxAudienceValue,
  timezone,
  exporting,
}) => {
  const secondaryPostColumnMetrics = audienceMetrics.map(metric => ({
    ...metric,
    maxValue: maxAudienceValue,
    value: post.statistics[metric.key],
  }));
 
  const primaryPostColumnMetrics = engagementMetrics.map(metric => ({
    ...metric,
    maxValue: maxEngagementValue,
    value: post.statistics[metric.key],
  }));
 
  const dateFormat = 'MMMM D, hh:mm a';
  return (
    <Container>
      <Side>
        <Attachment type={post.type} media={post.media} />
      </Side>
      <Top>
        <ContentDate>
          <Text size="medium" weight="bold" color="outerSpace">
            {moment(post.date).tz(timezone).format(dateFormat)}
          </Text>
          {!exporting && <ViewPost serviceLink={post.serviceLink} />}
        </ContentDate>
        <ContentMain>
          <Text size="mini">
            <ContentText dangerouslySetInnerHTML={{ __html: post.text.replace(/[\n⠀\s+(?=\s)]+/g, ' ') }} />
          </Text>
        </ContentMain>
      </Top>
      <Bottom>
        <Metrics metrics={[...primaryPostColumnMetrics, ...secondaryPostColumnMetrics]} />
      </Bottom>
    </Container>
  );
};
 
PostItem.propTypes = {
  post: PropTypes.shape({
    date: PropTypes.number,
    id: PropTypes.string,
    media: PropTypes.shape({
      picture: PropTypes.string,
      thumbnail: PropTypes.string,
    }),
    serviceLink: PropTypes.string,
    statistics: PropTypes.shape({
      comments: PropTypes.number,
      postClicks: PropTypes.number,
      postImpressions: PropTypes.number,
      postReach: PropTypes.number,
      reactions: PropTypes.number,
      shares: PropTypes.number,
    }),
    text: PropTypes.string,
    type: PropTypes.string,
  }).isRequired,
  engagementMetrics: PropTypes.arrayOf(PropTypes.object).isRequired,
  audienceMetrics: PropTypes.arrayOf(PropTypes.object).isRequired,
  maxEngagementValue: PropTypes.number.isRequired,
  maxAudienceValue: PropTypes.number.isRequired,
  timezone: PropTypes.string.isRequired,
  index: PropTypes.number.isRequired,
  exporting: PropTypes.bool.isRequired,
};
 
export default PostItem;