visualizationStep.tsx 4.8 KB

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