widgetCardChartContainer.tsx 6.0 KB

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