widgetCardChartContainer.tsx 8.1 KB

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