visualizationStep.tsx 4.9 KB

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