visualizationStep.tsx 4.9 KB

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