visualizationStep.tsx 5.9 KB

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