widgetCardChartContainer.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. location={location}
  97. selection={selection}
  98. />
  99. </Fragment>
  100. );
  101. }}
  102. </IssueWidgetQueries>
  103. );
  104. }
  105. if (widget.widgetType === WidgetType.RELEASE) {
  106. return (
  107. <ReleaseWidgetQueries
  108. api={api}
  109. organization={organization}
  110. widget={widget}
  111. selection={selection}
  112. limit={widget.limit ?? tableItemLimit}
  113. onDataFetched={onDataFetched}
  114. dashboardFilters={dashboardFilters}
  115. >
  116. {({tableResults, timeseriesResults, errorMessage, loading}) => {
  117. return (
  118. <Fragment>
  119. {typeof renderErrorMessage === 'function'
  120. ? renderErrorMessage(errorMessage)
  121. : null}
  122. <WidgetCardChart
  123. timeseriesResults={timeseriesResults}
  124. tableResults={tableResults}
  125. errorMessage={errorMessage}
  126. loading={loading}
  127. location={location}
  128. widget={widget}
  129. selection={selection}
  130. router={router}
  131. organization={organization}
  132. isMobile={isMobile}
  133. windowWidth={windowWidth}
  134. expandNumbers={expandNumbers}
  135. onZoom={onZoom}
  136. showSlider={showSlider}
  137. noPadding={noPadding}
  138. chartZoomOptions={chartZoomOptions}
  139. />
  140. </Fragment>
  141. );
  142. }}
  143. </ReleaseWidgetQueries>
  144. );
  145. }
  146. if (widget.widgetType === WidgetType.METRICS) {
  147. return (
  148. <MetricWidgetQueries
  149. api={api}
  150. organization={organization}
  151. widget={widget}
  152. selection={selection}
  153. limit={widget.limit ?? tableItemLimit}
  154. onDataFetched={onDataFetched}
  155. dashboardFilters={dashboardFilters}
  156. >
  157. {({tableResults, timeseriesResults, errorMessage, loading}) => {
  158. return (
  159. <Fragment>
  160. {typeof renderErrorMessage === 'function'
  161. ? renderErrorMessage(errorMessage)
  162. : null}
  163. <WidgetCardChart
  164. timeseriesResults={timeseriesResults}
  165. tableResults={tableResults}
  166. errorMessage={errorMessage}
  167. loading={loading}
  168. location={location}
  169. widget={widget}
  170. selection={selection}
  171. router={router}
  172. organization={organization}
  173. isMobile={isMobile}
  174. windowWidth={windowWidth}
  175. expandNumbers={expandNumbers}
  176. onZoom={onZoom}
  177. showSlider={showSlider}
  178. noPadding={noPadding}
  179. chartZoomOptions={chartZoomOptions}
  180. />
  181. </Fragment>
  182. );
  183. }}
  184. </MetricWidgetQueries>
  185. );
  186. }
  187. return (
  188. <WidgetQueries
  189. api={api}
  190. organization={organization}
  191. widget={widget}
  192. selection={selection}
  193. limit={tableItemLimit}
  194. onDataFetched={onDataFetched}
  195. dashboardFilters={dashboardFilters}
  196. >
  197. {({
  198. tableResults,
  199. timeseriesResults,
  200. errorMessage,
  201. loading,
  202. timeseriesResultsTypes,
  203. }) => {
  204. return (
  205. <Fragment>
  206. {typeof renderErrorMessage === 'function'
  207. ? renderErrorMessage(errorMessage)
  208. : null}
  209. <WidgetCardChart
  210. timeseriesResults={timeseriesResults}
  211. tableResults={tableResults}
  212. errorMessage={errorMessage}
  213. loading={loading}
  214. location={location}
  215. widget={widget}
  216. selection={selection}
  217. router={router}
  218. organization={organization}
  219. isMobile={isMobile}
  220. windowWidth={windowWidth}
  221. onZoom={onZoom}
  222. onLegendSelectChanged={onLegendSelectChanged}
  223. legendOptions={legendOptions}
  224. expandNumbers={expandNumbers}
  225. showSlider={showSlider}
  226. noPadding={noPadding}
  227. chartZoomOptions={chartZoomOptions}
  228. timeseriesResultsTypes={timeseriesResultsTypes}
  229. />
  230. </Fragment>
  231. );
  232. }}
  233. </WidgetQueries>
  234. );
  235. }
  236. export default WidgetCardChartContainer;
  237. const StyledTransparentLoadingMask = styled(props => (
  238. <TransparentLoadingMask {...props} maskBackgroundColor="transparent" />
  239. ))`
  240. display: flex;
  241. justify-content: center;
  242. align-items: center;
  243. `;
  244. function LoadingScreen({loading}: {loading: boolean}) {
  245. if (!loading) {
  246. return null;
  247. }
  248. return (
  249. <StyledTransparentLoadingMask visible={loading}>
  250. <LoadingIndicator mini />
  251. </StyledTransparentLoadingMask>
  252. );
  253. }