widgetCardChartContainer.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {DataZoomComponentOption, LegendComponentOption} from 'echarts';
  4. import type {Location} from 'history';
  5. import type {Client} from 'sentry/api';
  6. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import type {PageFilters} from 'sentry/types/core';
  9. import type {EChartEventHandler, Series} from 'sentry/types/echarts';
  10. import type {Organization} from 'sentry/types/organization';
  11. import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  12. import type {AggregationOutputType} from 'sentry/utils/discover/fields';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import WidgetLegendNameEncoderDecoder from 'sentry/views/dashboards/widgetLegendNameEncoderDecoder';
  15. import type {DashboardFilters, Widget} from '../types';
  16. import {WidgetType} from '../types';
  17. import type WidgetLegendSelectionState from '../widgetLegendSelectionState';
  18. import type {AugmentedEChartDataZoomHandler} from './chart';
  19. import WidgetCardChart from './chart';
  20. import {IssueWidgetCard} from './issueWidgetCard';
  21. import {WidgetCardDataLoader} from './widgetCardDataLoader';
  22. type Props = {
  23. api: Client;
  24. location: Location;
  25. organization: Organization;
  26. selection: PageFilters;
  27. widget: Widget;
  28. widgetLegendState: WidgetLegendSelectionState;
  29. chartGroup?: string;
  30. chartZoomOptions?: DataZoomComponentOption;
  31. dashboardFilters?: DashboardFilters;
  32. expandNumbers?: boolean;
  33. isMobile?: boolean;
  34. legendOptions?: LegendComponentOption;
  35. noPadding?: boolean;
  36. onDataFetched?: (results: {
  37. pageLinks?: string;
  38. tableResults?: TableDataWithTitle[];
  39. timeseriesResults?: Series[];
  40. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  41. totalIssuesCount?: string;
  42. }) => void;
  43. onLegendSelectChanged?: EChartEventHandler<{
  44. name: string;
  45. selected: Record<string, boolean>;
  46. type: 'legendselectchanged';
  47. }>;
  48. onWidgetSplitDecision?: (splitDecision: WidgetType) => void;
  49. onZoom?: AugmentedEChartDataZoomHandler;
  50. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  51. shouldResize?: boolean;
  52. showSlider?: boolean;
  53. tableItemLimit?: number;
  54. windowWidth?: number;
  55. };
  56. export function WidgetCardChartContainer({
  57. organization,
  58. selection,
  59. widget,
  60. dashboardFilters,
  61. isMobile,
  62. renderErrorMessage,
  63. tableItemLimit,
  64. windowWidth,
  65. onZoom,
  66. onLegendSelectChanged,
  67. legendOptions,
  68. expandNumbers,
  69. onDataFetched,
  70. showSlider,
  71. noPadding,
  72. chartZoomOptions,
  73. onWidgetSplitDecision,
  74. chartGroup,
  75. shouldResize,
  76. widgetLegendState,
  77. }: Props) {
  78. const location = useLocation();
  79. function keepLegendState({
  80. selected,
  81. }: {
  82. selected: Record<string, boolean>;
  83. type: 'legendselectchanged';
  84. }) {
  85. widgetLegendState.setWidgetSelectionState(selected, widget);
  86. }
  87. return (
  88. <WidgetCardDataLoader
  89. widget={widget}
  90. dashboardFilters={dashboardFilters}
  91. selection={selection}
  92. onDataFetched={onDataFetched}
  93. onWidgetSplitDecision={onWidgetSplitDecision}
  94. tableItemLimit={tableItemLimit}
  95. >
  96. {({
  97. tableResults,
  98. timeseriesResults,
  99. errorMessage,
  100. loading,
  101. timeseriesResultsTypes,
  102. }) => {
  103. if (widget.widgetType === WidgetType.ISSUE) {
  104. return (
  105. <Fragment>
  106. {typeof renderErrorMessage === 'function'
  107. ? renderErrorMessage(errorMessage)
  108. : null}
  109. <LoadingScreen loading={loading} />
  110. <IssueWidgetCard
  111. transformedResults={tableResults?.[0].data ?? []}
  112. loading={loading}
  113. errorMessage={errorMessage}
  114. widget={widget}
  115. location={location}
  116. selection={selection}
  117. />
  118. </Fragment>
  119. );
  120. }
  121. // Bind timeseries to widget for ability to control each widget's legend individually
  122. const modifiedTimeseriesResults =
  123. WidgetLegendNameEncoderDecoder.modifyTimeseriesNames(widget, timeseriesResults);
  124. return (
  125. <Fragment>
  126. {typeof renderErrorMessage === 'function'
  127. ? renderErrorMessage(errorMessage)
  128. : null}
  129. <WidgetCardChart
  130. timeseriesResults={modifiedTimeseriesResults}
  131. tableResults={tableResults}
  132. errorMessage={errorMessage}
  133. loading={loading}
  134. location={location}
  135. widget={widget}
  136. selection={selection}
  137. organization={organization}
  138. isMobile={isMobile}
  139. windowWidth={windowWidth}
  140. expandNumbers={expandNumbers}
  141. onZoom={onZoom}
  142. showSlider={showSlider}
  143. timeseriesResultsTypes={timeseriesResultsTypes}
  144. noPadding={noPadding}
  145. chartZoomOptions={chartZoomOptions}
  146. chartGroup={chartGroup}
  147. shouldResize={shouldResize}
  148. onLegendSelectChanged={
  149. onLegendSelectChanged ? onLegendSelectChanged : keepLegendState
  150. }
  151. legendOptions={
  152. legendOptions
  153. ? legendOptions
  154. : {selected: widgetLegendState.getWidgetSelectionState(widget)}
  155. }
  156. widgetLegendState={widgetLegendState}
  157. />
  158. </Fragment>
  159. );
  160. }}
  161. </WidgetCardDataLoader>
  162. );
  163. }
  164. export default WidgetCardChartContainer;
  165. const StyledTransparentLoadingMask = styled(props => (
  166. <TransparentLoadingMask {...props} maskBackgroundColor="transparent" />
  167. ))`
  168. display: flex;
  169. justify-content: center;
  170. align-items: center;
  171. `;
  172. export function LoadingScreen({loading}: {loading: boolean}) {
  173. if (!loading) {
  174. return null;
  175. }
  176. return (
  177. <StyledTransparentLoadingMask visible={loading}>
  178. <LoadingIndicator mini />
  179. </StyledTransparentLoadingMask>
  180. );
  181. }