widgetChartRow.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {useState} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {PerformanceLayoutBodyRow} from 'sentry/components/performance/layouts';
  6. import {space} from 'sentry/styles/space';
  7. import type EventView from 'sentry/utils/discover/eventView';
  8. import {usePerformanceDisplayType} from 'sentry/utils/performance/contexts/performanceDisplayContext';
  9. import type {ProjectPerformanceType} from 'sentry/views/performance/utils';
  10. import {getChartSetting} from '../utils';
  11. import type {PerformanceWidgetSetting} from '../widgetDefinitions';
  12. import WidgetContainer from './widgetContainer';
  13. export interface 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: ProjectPerformanceType,
  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. function 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 function TripleChartRow(props: ChartRowProps) {
  62. return <ChartRow {...props} />;
  63. }
  64. TripleChartRow.defaultProps = {
  65. chartCount: 3,
  66. chartHeight: 100,
  67. };
  68. export function DoubleChartRow(props: ChartRowProps) {
  69. return <ChartRow {...props} />;
  70. }
  71. DoubleChartRow.defaultProps = {
  72. chartCount: 2,
  73. chartHeight: 150,
  74. };
  75. const StyledRow = styled(PerformanceLayoutBodyRow)`
  76. margin-bottom: ${space(2)};
  77. `;