widgetCardChartContainer.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 type {DashboardFilters, Widget} from '../types';
  15. import {WidgetType} from '../types';
  16. import type {AugmentedEChartDataZoomHandler} from './chart';
  17. import WidgetCardChart 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. chartGroup?: string;
  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. chartGroup,
  72. }: Props) {
  73. const location = useLocation();
  74. const router = useRouter();
  75. if (widget.widgetType === WidgetType.ISSUE) {
  76. return (
  77. <IssueWidgetQueries
  78. api={api}
  79. organization={organization}
  80. widget={widget}
  81. selection={selection}
  82. limit={tableItemLimit}
  83. onDataFetched={onDataFetched}
  84. dashboardFilters={dashboardFilters}
  85. >
  86. {({tableResults, errorMessage, loading}) => {
  87. return (
  88. <Fragment>
  89. {typeof renderErrorMessage === 'function'
  90. ? renderErrorMessage(errorMessage)
  91. : null}
  92. <LoadingScreen loading={loading} />
  93. <IssueWidgetCard
  94. transformedResults={tableResults?.[0].data ?? []}
  95. loading={loading}
  96. errorMessage={errorMessage}
  97. widget={widget}
  98. location={location}
  99. selection={selection}
  100. />
  101. </Fragment>
  102. );
  103. }}
  104. </IssueWidgetQueries>
  105. );
  106. }
  107. if (widget.widgetType === WidgetType.RELEASE) {
  108. return (
  109. <ReleaseWidgetQueries
  110. api={api}
  111. organization={organization}
  112. widget={widget}
  113. selection={selection}
  114. limit={widget.limit ?? tableItemLimit}
  115. onDataFetched={onDataFetched}
  116. dashboardFilters={dashboardFilters}
  117. >
  118. {({tableResults, timeseriesResults, errorMessage, loading}) => {
  119. return (
  120. <Fragment>
  121. {typeof renderErrorMessage === 'function'
  122. ? renderErrorMessage(errorMessage)
  123. : null}
  124. <WidgetCardChart
  125. timeseriesResults={timeseriesResults}
  126. tableResults={tableResults}
  127. errorMessage={errorMessage}
  128. loading={loading}
  129. location={location}
  130. widget={widget}
  131. selection={selection}
  132. router={router}
  133. organization={organization}
  134. isMobile={isMobile}
  135. windowWidth={windowWidth}
  136. expandNumbers={expandNumbers}
  137. onZoom={onZoom}
  138. showSlider={showSlider}
  139. noPadding={noPadding}
  140. chartZoomOptions={chartZoomOptions}
  141. chartGroup={chartGroup}
  142. />
  143. </Fragment>
  144. );
  145. }}
  146. </ReleaseWidgetQueries>
  147. );
  148. }
  149. return (
  150. <WidgetQueries
  151. api={api}
  152. organization={organization}
  153. widget={widget}
  154. selection={selection}
  155. limit={tableItemLimit}
  156. onDataFetched={onDataFetched}
  157. dashboardFilters={dashboardFilters}
  158. >
  159. {({
  160. tableResults,
  161. timeseriesResults,
  162. errorMessage,
  163. loading,
  164. timeseriesResultsTypes,
  165. }) => {
  166. return (
  167. <Fragment>
  168. {typeof renderErrorMessage === 'function'
  169. ? renderErrorMessage(errorMessage)
  170. : null}
  171. <WidgetCardChart
  172. timeseriesResults={timeseriesResults}
  173. tableResults={tableResults}
  174. errorMessage={errorMessage}
  175. loading={loading}
  176. location={location}
  177. widget={widget}
  178. selection={selection}
  179. router={router}
  180. organization={organization}
  181. isMobile={isMobile}
  182. windowWidth={windowWidth}
  183. onZoom={onZoom}
  184. onLegendSelectChanged={onLegendSelectChanged}
  185. legendOptions={legendOptions}
  186. expandNumbers={expandNumbers}
  187. showSlider={showSlider}
  188. noPadding={noPadding}
  189. chartZoomOptions={chartZoomOptions}
  190. timeseriesResultsTypes={timeseriesResultsTypes}
  191. chartGroup={chartGroup}
  192. />
  193. </Fragment>
  194. );
  195. }}
  196. </WidgetQueries>
  197. );
  198. }
  199. export default WidgetCardChartContainer;
  200. const StyledTransparentLoadingMask = styled(props => (
  201. <TransparentLoadingMask {...props} maskBackgroundColor="transparent" />
  202. ))`
  203. display: flex;
  204. justify-content: center;
  205. align-items: center;
  206. `;
  207. export function LoadingScreen({loading}: {loading: boolean}) {
  208. if (!loading) {
  209. return null;
  210. }
  211. return (
  212. <StyledTransparentLoadingMask visible={loading}>
  213. <LoadingIndicator mini />
  214. </StyledTransparentLoadingMask>
  215. );
  216. }