widgetChartRow.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {useState} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {PerformanceLayoutBodyRow} from 'sentry/components/performance/layouts';
  6. import {space} from 'sentry/styles/space';
  7. import EventView from 'sentry/utils/discover/eventView';
  8. import {usePerformanceDisplayType} from 'sentry/utils/performance/contexts/performanceDisplayContext';
  9. import {PROJECT_PERFORMANCE_TYPE} from 'sentry/views/performance/utils';
  10. import {getChartSetting} from '../utils';
  11. import {PerformanceWidgetSetting} from '../widgetDefinitions';
  12. import WidgetContainer from './widgetContainer';
  13. export type ChartRowProps = {
  14. allowedCharts: PerformanceWidgetSetting[];
  15. chartCount: number;
  16. chartHeight: number;
  17. eventView: EventView;
  18. location: Location;
  19. withStaticFilters: boolean;
  20. };
  21. function getInitialChartSettings(
  22. chartCount: number,
  23. chartHeight: number,
  24. performanceType: PROJECT_PERFORMANCE_TYPE,
  25. allowedCharts: PerformanceWidgetSetting[]
  26. ) {
  27. return new Array(chartCount)
  28. .fill(0)
  29. .map((_, index) =>
  30. getChartSetting(index, chartHeight, performanceType, allowedCharts[index])
  31. );
  32. }
  33. const ChartRow = (props: ChartRowProps) => {
  34. const {chartCount, chartHeight, allowedCharts} = props;
  35. const theme = useTheme();
  36. const performanceType = usePerformanceDisplayType();
  37. const palette = theme.charts.getColorPalette(chartCount);
  38. const [chartSettings, setChartSettings] = useState(
  39. getInitialChartSettings(chartCount, chartHeight, performanceType, allowedCharts)
  40. );
  41. if (props.allowedCharts.length < chartCount) {
  42. throw new Error('Not enough allowed chart types to show row.');
  43. }
  44. return (
  45. <StyledRow minSize={200}>
  46. {new Array(chartCount).fill(0).map((_, index) => (
  47. <WidgetContainer
  48. {...props}
  49. key={index}
  50. index={index}
  51. chartHeight={chartHeight}
  52. chartColor={palette[index]}
  53. defaultChartSetting={allowedCharts[index]}
  54. rowChartSettings={chartSettings}
  55. setRowChartSettings={setChartSettings}
  56. />
  57. ))}
  58. </StyledRow>
  59. );
  60. };
  61. export const TripleChartRow = (props: ChartRowProps) => <ChartRow {...props} />;
  62. TripleChartRow.defaultProps = {
  63. chartCount: 3,
  64. chartHeight: 100,
  65. };
  66. export const DoubleChartRow = (props: ChartRowProps) => <ChartRow {...props} />;
  67. DoubleChartRow.defaultProps = {
  68. chartCount: 2,
  69. chartHeight: 180,
  70. };
  71. const StyledRow = styled(PerformanceLayoutBodyRow)`
  72. margin-bottom: ${space(2)};
  73. `;