widgetCardChartContainer.tsx 6.0 KB

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