StatsCharts.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import Area from './Area';
  5. import Pie from './Pie';
  6. import Bar from './Bar';
  7. const ChartsWrapper = styled.div`
  8. display: flex;
  9. flex-direction: column;
  10. align-items: stretch;
  11. padding: 32px;
  12. @media only screen and (max-width: 768px) {
  13. padding: 16px 16px 32px 16px;
  14. }
  15. `;
  16. const Row = styled.div`
  17. display: flex;
  18. border-bottom: 1px dotted #aaa;
  19. padding: 0 0 40px 0;
  20. margin: 0 0 32px 0;
  21. :last-child {
  22. border: none;
  23. margin: 0;
  24. }
  25. @media only screen and (max-width: 768px) {
  26. flex-direction: column;
  27. padding-bottom: 0;
  28. margin-bottom: 0;
  29. border-bottom: none;
  30. > *:not(:last-child) {
  31. padding-bottom: 24px;
  32. margin-bottom: 16px;
  33. border-bottom: 1px dotted #aaa;
  34. }
  35. }
  36. `;
  37. const StatsCharts = ({ stats, period, updatedAt }) => {
  38. const periodText = period.includes('last')
  39. ? `the last ${period.replace('last', '').toLocaleLowerCase()}`
  40. : 'all time';
  41. const hasView = stats.views.filter(view => view > 0);
  42. return (
  43. <ChartsWrapper>
  44. <Row>
  45. <Area data={stats.views} period={period} updatedAt={updatedAt} periodText={periodText} />
  46. </Row>
  47. {hasView.length
  48. ? [
  49. <Row key="second-row">
  50. <Pie data={stats.stats.referrer} updatedAt={updatedAt} title="Referrals" />
  51. <Bar data={stats.stats.browser} updatedAt={updatedAt} title="Browsers" />
  52. </Row>,
  53. <Row key="third-row">
  54. <Pie data={stats.stats.country} updatedAt={updatedAt} title="Country" />
  55. <Bar
  56. data={stats.stats.os.map(o => ({
  57. ...o,
  58. name: o.name === 'Mac Os X' ? 'macOS' : o.name,
  59. }))}
  60. updatedAt={updatedAt}
  61. title="OS"
  62. />
  63. </Row>,
  64. ]
  65. : null}
  66. </ChartsWrapper>
  67. );
  68. };
  69. StatsCharts.propTypes = {
  70. updatedAt: PropTypes.string.isRequired,
  71. period: PropTypes.string.isRequired,
  72. stats: PropTypes.shape({
  73. stats: PropTypes.object.isRequired,
  74. views: PropTypes.array.isRequired,
  75. }).isRequired,
  76. };
  77. export default StatsCharts;