visualizationStep.tsx 5.8 KB

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