withTitle.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { FC } from 'react';
  2. import styled from 'styled-components';
  3. import formatDate from 'date-fns/format';
  4. import { Flex } from 'reflexbox/styled-components';
  5. interface Props {
  6. data: number | any; // TODO: types
  7. period?: string;
  8. periodText?: string;
  9. title?: string;
  10. updatedAt: string;
  11. }
  12. const Title = styled.h3`
  13. margin-bottom: 12px;
  14. font-size: 24px;
  15. font-weight: 300;
  16. @media only screen and (max-width: 768px) {
  17. font-size: 18px;
  18. }
  19. `;
  20. const SubTitle = styled.span`
  21. margin-bottom: 32px;
  22. font-size: 13px;
  23. font-weight: 300;
  24. color: #aaa;
  25. @media only screen and (max-width: 768px) {
  26. font-size: 11px;
  27. }
  28. `;
  29. const Count = styled.span`
  30. font-weight: bold;
  31. border-bottom: 1px dotted #999;
  32. `;
  33. const withTitle = (ChartComponent: FC<any>) => {
  34. function WithTitle(props: Props) {
  35. return (
  36. <Flex
  37. flexGrow={1}
  38. flexShrink={1}
  39. flexBasis={['100%', '100%', '50%']}
  40. flexDirection="column"
  41. >
  42. <Title>
  43. {props.periodText && (
  44. <Count>{props.data.reduce((sum, view) => sum + view, 0)}</Count>
  45. )}
  46. {props.periodText
  47. ? ` tracked clicks in ${props.periodText}`
  48. : props.title}
  49. .
  50. </Title>
  51. {props.periodText && props.updatedAt && (
  52. <SubTitle>
  53. Last update in{' '}
  54. {formatDate(new Date(props.updatedAt), 'dddd, hh:mm aa')}.
  55. </SubTitle>
  56. )}
  57. <ChartComponent {...props} />
  58. </Flex>
  59. );
  60. }
  61. WithTitle.defaultProps = {
  62. title: '',
  63. periodText: '',
  64. };
  65. return WithTitle;
  66. };
  67. export default withTitle;