visualizationStep.tsx 5.1 KB

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