StatsCharts.tsx 2.3 KB

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