sparkLine.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import Placeholder from 'app/components/placeholder';
  4. import theme from 'app/utils/theme';
  5. import {IncidentStats} from 'app/views/alerts/types';
  6. // Height of sparkline
  7. const SPARKLINE_HEIGHT = 38;
  8. type Props = {
  9. className?: string;
  10. eventStats: IncidentStats['eventStats'];
  11. error?: React.ReactNode;
  12. };
  13. const Sparklines = React.lazy(() => import('app/components/sparklines'));
  14. const SparklinesLine = React.lazy(() => import('app/components/sparklines/line'));
  15. class SparkLine extends React.Component<Props> {
  16. render() {
  17. const {className, error, eventStats} = this.props;
  18. if (error) {
  19. return <SparklineError error={error} />;
  20. }
  21. if (!eventStats) {
  22. return <SparkLinePlaceholder />;
  23. }
  24. const data = eventStats.data.map(([, value]) =>
  25. value && Array.isArray(value) && value.length ? value[0].count || 0 : 0
  26. );
  27. return (
  28. <React.Suspense fallback={<SparkLinePlaceholder />}>
  29. <div data-test-id="incident-sparkline" className={className}>
  30. <Sparklines data={data} width={100} height={32}>
  31. <SparklinesLine
  32. style={{stroke: theme.gray300, fill: 'none', strokeWidth: 2}}
  33. />
  34. </Sparklines>
  35. </div>
  36. </React.Suspense>
  37. );
  38. }
  39. }
  40. const StyledSparkLine = styled(SparkLine)`
  41. flex-shrink: 0;
  42. width: 100%;
  43. height: ${SPARKLINE_HEIGHT}px;
  44. `;
  45. const SparkLinePlaceholder = styled(Placeholder)`
  46. height: ${SPARKLINE_HEIGHT}px;
  47. `;
  48. const SparklineError = styled(SparkLinePlaceholder)`
  49. align-items: center;
  50. line-height: 1;
  51. `;
  52. export default StyledSparkLine;