widgetCardChartContainer.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 {Organization, PageFilters} from 'sentry/types';
  9. import type {EChartEventHandler, Series} from 'sentry/types/echarts';
  10. import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  11. import type {AggregationOutputType} from 'sentry/utils/discover/fields';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import useRouter from 'sentry/utils/useRouter';
  14. import MetricWidgetQueries from 'sentry/views/dashboards/widgetCard/metricWidgetQueries';
  15. import type {DashboardFilters, Widget} from '../types';
  16. import {WidgetType} from '../types';
  17. import type {AugmentedEChartDataZoomHandler} from './chart';
  18. import WidgetCardChart from './chart';
  19. import {IssueWidgetCard} from './issueWidgetCard';
  20. import IssueWidgetQueries from './issueWidgetQueries';
  21. import ReleaseWidgetQueries from './releaseWidgetQueries';
  22. import WidgetQueries from './widgetQueries';
  23. type Props = {
  24. api: Client;
  25. location: Location;
  26. organization: Organization;
  27. selection: PageFilters;
  28. widget: Widget;
  29. chartZoomOptions?: DataZoomComponentOption;
  30. dashboardFilters?: DashboardFilters;
  31. expandNumbers?: boolean;
  32. isMobile?: boolean;
  33. legendOptions?: LegendComponentOption;
  34. noPadding?: boolean;
  35. onDataFetched?: (results: {
  36. pageLinks?: string;
  37. tableResults?: TableDataWithTitle[];
  38. timeseriesResults?: Series[];
  39. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  40. totalIssuesCount?: string;
  41. }) => void;
  42. onLegendSelectChanged?: EChartEventHandler<{
  43. name: string;
  44. selected: Record<string, boolean>;
  45. type: 'legendselectchanged';
  46. }>;
  47. onZoom?: AugmentedEChartDataZoomHandler;
  48. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  49. showSlider?: boolean;
  50. tableItemLimit?: number;
  51. windowWidth?: number;
  52. };
  53. export function WidgetCardChartContainer({
  54. api,
  55. organization,
  56. selection,
  57. widget,
  58. dashboardFilters,
  59. isMobile,
  60. renderErrorMessage,
  61. tableItemLimit,
  62. windowWidth,
  63. onZoom,
  64. onLegendSelectChanged,
  65. legendOptions,
  66. expandNumbers,
  67. onDataFetched,
  68. showSlider,
  69. noPadding,
  70. chartZoomOptions,
  71. }: Props) {
  72. const location = useLocation();
  73. const router = useRouter();
  74. if (widget.widgetType === WidgetType.ISSUE) {
  75. return (
  76. <IssueWidgetQueries
  77. api={api}
  78. organization={organization}
  79. widget={widget}
  80. selection={selection}
  81. limit={tableItemLimit}
  82. onDataFetched={onDataFetched}
  83. dashboardFilters={dashboardFilters}
  84. >
  85. {({tableResults, errorMessage, loading}) => {
  86. return (
  87. <Fragment>
  88. {typeof renderErrorMessage === 'function'
  89. ? renderErrorMessage(errorMessage)
  90. : null}
  91. <LoadingScreen loading={loading} />
  92. <IssueWidgetCard
  93. transformedResults={tableResults?.[0].data ?? []}
  94. loading={loading}
  95. errorMessage={errorMessage}
  96. widget={widget}
  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. }