widgetChartRow.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {PerformanceLayoutBodyRow} from 'sentry/components/performance/layouts';
  5. import {getChartColorPalette} from 'sentry/constants/chartPalette';
  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 performanceType = usePerformanceDisplayType();
  36. const palette = getChartColorPalette(chartCount);
  37. const [chartSettings, setChartSettings] = useState(
  38. getInitialChartSettings(chartCount, chartHeight, performanceType, allowedCharts)
  39. );
  40. if (props.allowedCharts.length < chartCount) {
  41. throw new Error('Not enough allowed chart types to show row.');
  42. }
  43. return (
  44. <StyledRow minSize={200}>
  45. {new Array(chartCount).fill(0).map((_, index) => (
  46. <WidgetContainer
  47. {...props}
  48. key={index}
  49. index={index}
  50. chartHeight={chartHeight}
  51. chartColor={palette[index]}
  52. defaultChartSetting={allowedCharts[index]!}
  53. rowChartSettings={chartSettings}
  54. setRowChartSettings={setChartSettings}
  55. />
  56. ))}
  57. </StyledRow>
  58. );
  59. }
  60. type DefaultProps = 'chartCount' | 'chartHeight';
  61. type ChartRowPropsWithDefaults = Omit<ChartRowProps, DefaultProps> &
  62. Pick<Partial<ChartRowProps>, DefaultProps>;
  63. export function TripleChartRow(props: ChartRowPropsWithDefaults) {
  64. return <ChartRow chartCount={3} chartHeight={100} {...props} />;
  65. }
  66. export function DoubleChartRow(props: ChartRowPropsWithDefaults) {
  67. return <ChartRow chartCount={2} chartHeight={150} {...props} />;
  68. }
  69. const StyledRow = styled(PerformanceLayoutBodyRow)`
  70. margin-bottom: ${space(2)};
  71. `;