visualizationStep.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 {WidgetCardPanel} from 'sentry/views/dashboards/widgetCard/widgetCardPanel';
  22. import WidgetLegendNameEncoderDecoder from 'sentry/views/dashboards/widgetLegendNameEncoderDecoder';
  23. import {IndexedEventsSelectionAlert} from '../../indexedEventsSelectionAlert';
  24. import {getDashboardFiltersFromURL} from '../../utils';
  25. import WidgetCard from '../../widgetCard';
  26. import type WidgetLegendSelectionState from '../../widgetLegendSelectionState';
  27. import {displayTypes} from '../utils';
  28. import {BuildStep} from './buildStep';
  29. interface Props {
  30. displayType: DisplayType;
  31. isWidgetInvalid: boolean;
  32. location: Location;
  33. onChange: (displayType: DisplayType) => void;
  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. pageFilters,
  44. displayType,
  45. error,
  46. onChange,
  47. widget,
  48. onDataFetched,
  49. dashboardFilters,
  50. location,
  51. isWidgetInvalid,
  52. onWidgetSplitDecision,
  53. widgetLegendState,
  54. }: Props) {
  55. const organization = useOrganization();
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  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. isWidgetInvalid={isWidgetInvalid}
  128. onDataFetched={onDataFetched}
  129. onWidgetSplitDecision={onWidgetSplitDecision}
  130. shouldResize={false}
  131. onLegendSelectChanged={() => {}}
  132. legendOptions={
  133. widgetLegendState.widgetRequiresLegendUnselection(widget)
  134. ? {selected: unselectedReleasesForCharts}
  135. : undefined
  136. }
  137. widgetLegendState={widgetLegendState}
  138. disableFullscreen
  139. />
  140. <IndexedEventsSelectionAlert widget={widget} />
  141. </VisualizationWrapper>
  142. </StyledBuildStep>
  143. );
  144. }
  145. const StyledBuildStep = styled(BuildStep)`
  146. position: sticky;
  147. top: 0;
  148. z-index: 100;
  149. background: ${p => p.theme.background};
  150. &::before {
  151. margin-top: 1px;
  152. }
  153. `;
  154. const VisualizationWrapper = styled('div')<{displayType: DisplayType}>`
  155. padding-right: ${space(2)};
  156. ${WidgetCardPanel} {
  157. height: initial;
  158. min-height: 120px;
  159. }
  160. ${p =>
  161. p.displayType === DisplayType.TABLE &&
  162. css`
  163. overflow: hidden;
  164. ${TableCell} {
  165. /* 24px ActorContainer height + 16px top and bottom padding + 1px border = 41px */
  166. height: 41px;
  167. }
  168. ${WidgetCardPanel} {
  169. /* total size of a table, if it would display 5 rows of content */
  170. height: 301px;
  171. }
  172. `};
  173. `;