visualizationStep.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {CSSProperties, useCallback, useEffect, useState} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import debounce from 'lodash/debounce';
  6. import isEqual from 'lodash/isEqual';
  7. import {TableCell} from 'sentry/components/charts/simpleTableChart';
  8. import SelectControl from 'sentry/components/forms/controls/selectControl';
  9. import FieldGroup from 'sentry/components/forms/fieldGroup';
  10. import PanelAlert from 'sentry/components/panels/panelAlert';
  11. import {DEFAULT_DEBOUNCE_DURATION} from 'sentry/constants';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {Organization, PageFilters, SelectValue} from 'sentry/types';
  15. import usePrevious from 'sentry/utils/usePrevious';
  16. import {DashboardFilters, DisplayType, Widget} from 'sentry/views/dashboards/types';
  17. import {getDashboardFiltersFromURL} from '../../utils';
  18. import WidgetCard, {WidgetCardPanel} from '../../widgetCard';
  19. import {displayTypes} from '../utils';
  20. import {BuildStep} from './buildStep';
  21. interface Props {
  22. displayType: DisplayType;
  23. isWidgetInvalid: boolean;
  24. location: Location;
  25. onChange: (displayType: DisplayType) => void;
  26. organization: Organization;
  27. pageFilters: PageFilters;
  28. widget: Widget;
  29. dashboardFilters?: DashboardFilters;
  30. error?: string;
  31. noDashboardsMEPProvider?: boolean;
  32. }
  33. export function VisualizationStep({
  34. organization,
  35. pageFilters,
  36. displayType,
  37. error,
  38. onChange,
  39. widget,
  40. noDashboardsMEPProvider,
  41. dashboardFilters,
  42. location,
  43. isWidgetInvalid,
  44. }: Props) {
  45. const [debouncedWidget, setDebouncedWidget] = useState(widget);
  46. const previousWidget = usePrevious(widget);
  47. // Disabling for now because we use debounce to avoid excessively hitting
  48. // our endpoints, but useCallback wants an inline function and not one
  49. // returned from debounce
  50. // eslint-disable-next-line react-hooks/exhaustive-deps
  51. const debounceWidget = useCallback(
  52. debounce((value: Widget, shouldCancelUpdates: boolean) => {
  53. if (shouldCancelUpdates) {
  54. return;
  55. }
  56. setDebouncedWidget(value);
  57. }, DEFAULT_DEBOUNCE_DURATION),
  58. []
  59. );
  60. useEffect(() => {
  61. let shouldCancelUpdates = false;
  62. if (!isEqual(previousWidget, widget)) {
  63. debounceWidget(widget, shouldCancelUpdates);
  64. }
  65. return () => {
  66. shouldCancelUpdates = true;
  67. };
  68. }, [widget, previousWidget, debounceWidget]);
  69. const displayOptions = Object.keys(displayTypes).map(value => ({
  70. label: displayTypes[value],
  71. value,
  72. }));
  73. return (
  74. <BuildStep
  75. title={t('Choose your visualization')}
  76. description={t(
  77. 'This is a preview of how your widget will appear in the dashboard.'
  78. )}
  79. >
  80. <FieldGroup error={error} inline={false} flexibleControlStateSize stacked>
  81. <SelectControl
  82. name="displayType"
  83. options={displayOptions}
  84. value={displayType}
  85. onChange={(option: SelectValue<DisplayType>) => {
  86. onChange(option.value);
  87. }}
  88. styles={{
  89. singleValue: (provided: CSSProperties) => ({
  90. ...provided,
  91. width: `calc(100% - ${space(1)})`,
  92. }),
  93. }}
  94. />
  95. </FieldGroup>
  96. <VisualizationWrapper displayType={displayType}>
  97. <WidgetCard
  98. organization={organization}
  99. selection={pageFilters}
  100. widget={debouncedWidget}
  101. dashboardFilters={getDashboardFiltersFromURL(location) ?? dashboardFilters}
  102. isEditing={false}
  103. widgetLimitReached={false}
  104. renderErrorMessage={errorMessage =>
  105. typeof errorMessage === 'string' && (
  106. <PanelAlert type="error">{errorMessage}</PanelAlert>
  107. )
  108. }
  109. noLazyLoad
  110. showStoredAlert
  111. noDashboardsMEPProvider={noDashboardsMEPProvider}
  112. isWidgetInvalid={isWidgetInvalid}
  113. />
  114. </VisualizationWrapper>
  115. </BuildStep>
  116. );
  117. }
  118. const VisualizationWrapper = styled('div')<{displayType: DisplayType}>`
  119. padding-right: ${space(2)};
  120. ${WidgetCardPanel} {
  121. height: initial;
  122. }
  123. ${p =>
  124. p.displayType === DisplayType.TABLE &&
  125. css`
  126. overflow: hidden;
  127. ${TableCell} {
  128. /* 24px ActorContainer height + 16px top and bottom padding + 1px border = 41px */
  129. height: 41px;
  130. }
  131. ${WidgetCardPanel} {
  132. /* total size of a table, if it would display 5 rows of content */
  133. height: 301px;
  134. }
  135. `};
  136. `;