widgetCardChartContainer.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {DataZoomComponentOption} from 'echarts';
  4. import {LegendComponentOption} from 'echarts';
  5. import {Location} from 'history';
  6. import {Client} from 'sentry/api';
  7. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {Organization, PageFilters} from 'sentry/types';
  10. import {EChartEventHandler, Series} from 'sentry/types/echarts';
  11. import {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  12. import {AggregationOutputType} from 'sentry/utils/discover/fields';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import useRouter from 'sentry/utils/useRouter';
  15. import MetricWidgetQueries from 'sentry/views/dashboards/widgetCard/metricWidgetQueries';
  16. import {DashboardFilters, Widget, WidgetType} from '../types';
  17. import WidgetCardChart, {AugmentedEChartDataZoomHandler} from './chart';
  18. import {IssueWidgetCard} from './issueWidgetCard';
  19. import IssueWidgetQueries from './issueWidgetQueries';
  20. import ReleaseWidgetQueries from './releaseWidgetQueries';
  21. import WidgetQueries from './widgetQueries';
  22. type Props = {
  23. api: Client;
  24. location: Location;
  25. organization: Organization;
  26. selection: PageFilters;
  27. widget: Widget;
  28. chartZoomOptions?: DataZoomComponentOption;
  29. dashboardFilters?: DashboardFilters;
  30. expandNumbers?: boolean;
  31. isMobile?: boolean;
  32. legendOptions?: LegendComponentOption;
  33. noPadding?: boolean;
  34. onDataFetched?: (results: {
  35. pageLinks?: string;
  36. tableResults?: TableDataWithTitle[];
  37. timeseriesResults?: Series[];
  38. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  39. totalIssuesCount?: string;
  40. }) => void;
  41. onLegendSelectChanged?: EChartEventHandler<{
  42. name: string;
  43. selected: Record<string, boolean>;
  44. type: 'legendselectchanged';
  45. }>;
  46. onZoom?: AugmentedEChartDataZoomHandler;
  47. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  48. showSlider?: boolean;
  49. tableItemLimit?: number;
  50. windowWidth?: number;
  51. };
  52. export function WidgetCardChartContainer({
  53. api,
  54. organization,
  55. selection,
  56. widget,
  57. dashboardFilters,
  58. isMobile,
  59. renderErrorMessage,
  60. tableItemLimit,
  61. windowWidth,
  62. onZoom,
  63. onLegendSelectChanged,
  64. legendOptions,
  65. expandNumbers,
  66. onDataFetched,
  67. showSlider,
  68. noPadding,
  69. chartZoomOptions,
  70. }: Props) {
  71. const location = useLocation();
  72. const router = useRouter();
  73. if (widget.widgetType === WidgetType.ISSUE) {
  74. return (
  75. <IssueWidgetQueries
  76. api={api}
  77. organization={organization}
  78. widget={widget}
  79. selection={selection}
  80. limit={tableItemLimit}
  81. onDataFetched={onDataFetched}
  82. dashboardFilters={dashboardFilters}
  83. >
  84. {({tableResults, errorMessage, loading}) => {
  85. return (
  86. <Fragment>
  87. {typeof renderErrorMessage === 'function'
  88. ? renderErrorMessage(errorMessage)
  89. : null}
  90. <LoadingScreen loading={loading} />
  91. <IssueWidgetCard
  92. transformedResults={tableResults?.[0].data ?? []}
  93. loading={loading}
  94. errorMessage={errorMessage}
  95. widget={widget}
  96. organization={organization}
  97. location={location}
  98. selection={selection}
  99. />
  100. </Fragment>
  101. );
  102. }}
  103. </IssueWidgetQueries>
  104. );
  105. }
  106. if (widget.widgetType === WidgetType.RELEASE) {
  107. return (
  108. <ReleaseWidgetQueries
  109. api={api}
  110. organization={organization}
  111. widget={widget}
  112. selection={selection}
  113. limit={widget.limit ?? tableItemLimit}
  114. onDataFetched={onDataFetched}
  115. dashboardFilters={dashboardFilters}
  116. >
  117. {({tableResults, timeseriesResults, errorMessage, loading}) => {
  118. return (
  119. <Fragment>
  120. {typeof renderErrorMessage === 'function'
  121. ? renderErrorMessage(errorMessage)
  122. : null}
  123. <WidgetCardChart
  124. timeseriesResults={timeseriesResults}
  125. tableResults={tableResults}
  126. errorMessage={errorMessage}
  127. loading={loading}
  128. location={location}
  129. widget={widget}
  130. selection={selection}
  131. router={router}
  132. organization={organization}
  133. isMobile={isMobile}
  134. windowWidth={windowWidth}
  135. expandNumbers={expandNumbers}
  136. onZoom={onZoom}
  137. showSlider={showSlider}
  138. noPadding={noPadding}
  139. chartZoomOptions={chartZoomOptions}
  140. />
  141. </Fragment>
  142. );
  143. }}
  144. </ReleaseWidgetQueries>
  145. );
  146. }
  147. if (widget.widgetType === WidgetType.METRICS) {
  148. return (
  149. <MetricWidgetQueries
  150. api={api}
  151. organization={organization}
  152. widget={widget}
  153. selection={selection}
  154. limit={widget.limit ?? tableItemLimit}
  155. onDataFetched={onDataFetched}
  156. dashboardFilters={dashboardFilters}
  157. >
  158. {({tableResults, timeseriesResults, errorMessage, loading}) => {
  159. return (
  160. <Fragment>
  161. {typeof renderErrorMessage === 'function'
  162. ? renderErrorMessage(errorMessage)
  163. : null}
  164. <WidgetCardChart
  165. timeseriesResults={timeseriesResults}
  166. tableResults={tableResults}
  167. errorMessage={errorMessage}
  168. loading={loading}
  169. location={location}
  170. widget={widget}
  171. selection={selection}
  172. router={router}
  173. organization={organization}
  174. isMobile={isMobile}
  175. windowWidth={windowWidth}
  176. expandNumbers={expandNumbers}
  177. onZoom={onZoom}
  178. showSlider={showSlider}
  179. noPadding={noPadding}
  180. chartZoomOptions={chartZoomOptions}
  181. />
  182. </Fragment>
  183. );
  184. }}
  185. </MetricWidgetQueries>
  186. );
  187. }
  188. return (
  189. <WidgetQueries
  190. api={api}
  191. organization={organization}
  192. widget={widget}
  193. selection={selection}
  194. limit={tableItemLimit}
  195. onDataFetched={onDataFetched}
  196. dashboardFilters={dashboardFilters}
  197. >
  198. {({
  199. tableResults,
  200. timeseriesResults,
  201. errorMessage,
  202. loading,
  203. timeseriesResultsTypes,
  204. }) => {
  205. return (
  206. <Fragment>
  207. {typeof renderErrorMessage === 'function'
  208. ? renderErrorMessage(errorMessage)
  209. : null}
  210. <WidgetCardChart
  211. timeseriesResults={timeseriesResults}
  212. tableResults={tableResults}
  213. errorMessage={errorMessage}
  214. loading={loading}
  215. location={location}
  216. widget={widget}
  217. selection={selection}
  218. router={router}
  219. organization={organization}
  220. isMobile={isMobile}
  221. windowWidth={windowWidth}
  222. onZoom={onZoom}
  223. onLegendSelectChanged={onLegendSelectChanged}
  224. legendOptions={legendOptions}
  225. expandNumbers={expandNumbers}
  226. showSlider={showSlider}
  227. noPadding={noPadding}
  228. chartZoomOptions={chartZoomOptions}
  229. timeseriesResultsTypes={timeseriesResultsTypes}
  230. />
  231. </Fragment>
  232. );
  233. }}
  234. </WidgetQueries>
  235. );
  236. }
  237. export default WidgetCardChartContainer;
  238. const StyledTransparentLoadingMask = styled(props => (
  239. <TransparentLoadingMask {...props} maskBackgroundColor="transparent" />
  240. ))`
  241. display: flex;
  242. justify-content: center;
  243. align-items: center;
  244. `;
  245. function LoadingScreen({loading}: {loading: boolean}) {
  246. if (!loading) {
  247. return null;
  248. }
  249. return (
  250. <StyledTransparentLoadingMask visible={loading}>
  251. <LoadingIndicator mini />
  252. </StyledTransparentLoadingMask>
  253. );
  254. }