widgetCardChartContainer.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 {DashboardFilters, Widget, WidgetType} from '../types';
  16. import WidgetCardChart, {AugmentedEChartDataZoomHandler} from './chart';
  17. import {IssueWidgetCard} from './issueWidgetCard';
  18. import IssueWidgetQueries from './issueWidgetQueries';
  19. import ReleaseWidgetQueries from './releaseWidgetQueries';
  20. import WidgetQueries from './widgetQueries';
  21. type Props = {
  22. api: Client;
  23. location: Location;
  24. organization: Organization;
  25. selection: PageFilters;
  26. widget: Widget;
  27. chartZoomOptions?: DataZoomComponentOption;
  28. dashboardFilters?: DashboardFilters;
  29. expandNumbers?: boolean;
  30. isMobile?: boolean;
  31. legendOptions?: LegendComponentOption;
  32. noPadding?: boolean;
  33. onDataFetched?: (results: {
  34. pageLinks?: string;
  35. tableResults?: TableDataWithTitle[];
  36. timeseriesResults?: Series[];
  37. timeseriesResultsTypes?: Record<string, AggregationOutputType>;
  38. totalIssuesCount?: string;
  39. }) => void;
  40. onLegendSelectChanged?: EChartEventHandler<{
  41. name: string;
  42. selected: Record<string, boolean>;
  43. type: 'legendselectchanged';
  44. }>;
  45. onZoom?: AugmentedEChartDataZoomHandler;
  46. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  47. showSlider?: boolean;
  48. tableItemLimit?: number;
  49. windowWidth?: number;
  50. };
  51. export function WidgetCardChartContainer({
  52. api,
  53. organization,
  54. selection,
  55. widget,
  56. dashboardFilters,
  57. isMobile,
  58. renderErrorMessage,
  59. tableItemLimit,
  60. windowWidth,
  61. onZoom,
  62. onLegendSelectChanged,
  63. legendOptions,
  64. expandNumbers,
  65. onDataFetched,
  66. showSlider,
  67. noPadding,
  68. chartZoomOptions,
  69. }: Props) {
  70. const location = useLocation();
  71. const router = useRouter();
  72. if (widget.widgetType === WidgetType.ISSUE) {
  73. return (
  74. <IssueWidgetQueries
  75. api={api}
  76. organization={organization}
  77. widget={widget}
  78. selection={selection}
  79. limit={tableItemLimit}
  80. onDataFetched={onDataFetched}
  81. dashboardFilters={dashboardFilters}
  82. >
  83. {({tableResults, errorMessage, loading}) => {
  84. return (
  85. <Fragment>
  86. {typeof renderErrorMessage === 'function'
  87. ? renderErrorMessage(errorMessage)
  88. : null}
  89. <LoadingScreen loading={loading} />
  90. <IssueWidgetCard
  91. transformedResults={tableResults?.[0].data ?? []}
  92. loading={loading}
  93. errorMessage={errorMessage}
  94. widget={widget}
  95. organization={organization}
  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. return (
  147. <WidgetQueries
  148. api={api}
  149. organization={organization}
  150. widget={widget}
  151. selection={selection}
  152. limit={tableItemLimit}
  153. onDataFetched={onDataFetched}
  154. dashboardFilters={dashboardFilters}
  155. >
  156. {({
  157. tableResults,
  158. timeseriesResults,
  159. errorMessage,
  160. loading,
  161. timeseriesResultsTypes,
  162. }) => {
  163. return (
  164. <Fragment>
  165. {typeof renderErrorMessage === 'function'
  166. ? renderErrorMessage(errorMessage)
  167. : null}
  168. <WidgetCardChart
  169. timeseriesResults={timeseriesResults}
  170. tableResults={tableResults}
  171. errorMessage={errorMessage}
  172. loading={loading}
  173. location={location}
  174. widget={widget}
  175. selection={selection}
  176. router={router}
  177. organization={organization}
  178. isMobile={isMobile}
  179. windowWidth={windowWidth}
  180. onZoom={onZoom}
  181. onLegendSelectChanged={onLegendSelectChanged}
  182. legendOptions={legendOptions}
  183. expandNumbers={expandNumbers}
  184. showSlider={showSlider}
  185. noPadding={noPadding}
  186. chartZoomOptions={chartZoomOptions}
  187. timeseriesResultsTypes={timeseriesResultsTypes}
  188. />
  189. </Fragment>
  190. );
  191. }}
  192. </WidgetQueries>
  193. );
  194. }
  195. export default WidgetCardChartContainer;
  196. const StyledTransparentLoadingMask = styled(props => (
  197. <TransparentLoadingMask {...props} maskBackgroundColor="transparent" />
  198. ))`
  199. display: flex;
  200. justify-content: center;
  201. align-items: center;
  202. `;
  203. const LoadingScreen = ({loading}: {loading: boolean}) => {
  204. if (!loading) {
  205. return null;
  206. }
  207. return (
  208. <StyledTransparentLoadingMask visible={loading}>
  209. <LoadingIndicator mini />
  210. </StyledTransparentLoadingMask>
  211. );
  212. };