visualizationStep.tsx 5.9 KB

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