widgetCardChartContainer.tsx 6.8 KB

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