widgetCard.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {Fragment, useMemo} from 'react';
  2. import type {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {ErrorBoundary} from '@sentry/react';
  5. import type {Location} from 'history';
  6. import ErrorPanel from 'sentry/components/charts/errorPanel';
  7. import {HeaderTitle} from 'sentry/components/charts/styles';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import TextOverflow from 'sentry/components/textOverflow';
  10. import {IconSearch, IconWarning} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization, PageFilters} from 'sentry/types';
  14. import {useMetricsQuery} from 'sentry/utils/metrics/useMetricsQuery';
  15. import {MetricBigNumberContainer} from 'sentry/views/dashboards/metrics/bigNumber';
  16. import {MetricChartContainer} from 'sentry/views/dashboards/metrics/chart';
  17. import {MetricTableContainer} from 'sentry/views/dashboards/metrics/table';
  18. import {
  19. expressionsToApiQueries,
  20. getMetricExpressions,
  21. toMetricDisplayType,
  22. } from 'sentry/views/dashboards/metrics/utils';
  23. import type {DashboardFilters, Widget} from 'sentry/views/dashboards/types';
  24. import {DisplayType} from 'sentry/views/dashboards/types';
  25. import {WidgetCardPanel, WidgetTitleRow} from 'sentry/views/dashboards/widgetCard';
  26. import {DashboardsMEPContext} from 'sentry/views/dashboards/widgetCard/dashboardsMEPContext';
  27. import {Toolbar} from 'sentry/views/dashboards/widgetCard/toolbar';
  28. import WidgetCardContextMenu from 'sentry/views/dashboards/widgetCard/widgetCardContextMenu';
  29. import {getWidgetTitle} from 'sentry/views/ddm/widget';
  30. type Props = {
  31. isEditingDashboard: boolean;
  32. location: Location;
  33. organization: Organization;
  34. router: InjectedRouter;
  35. selection: PageFilters;
  36. widget: Widget;
  37. dashboardFilters?: DashboardFilters;
  38. index?: string;
  39. isMobile?: boolean;
  40. onDelete?: () => void;
  41. onDuplicate?: () => void;
  42. onEdit?: (index: string) => void;
  43. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  44. showContextMenu?: boolean;
  45. };
  46. export function MetricWidgetCard({
  47. organization,
  48. selection,
  49. widget,
  50. isEditingDashboard,
  51. onDelete,
  52. onDuplicate,
  53. location,
  54. router,
  55. dashboardFilters,
  56. renderErrorMessage,
  57. showContextMenu = true,
  58. }: Props) {
  59. const metricQueries = useMemo(
  60. () => expressionsToApiQueries(getMetricExpressions(widget, dashboardFilters)),
  61. [widget, dashboardFilters]
  62. );
  63. const widgetMQL = useMemo(() => getWidgetTitle(metricQueries), [metricQueries]);
  64. const {
  65. data: timeseriesData,
  66. isLoading,
  67. isError,
  68. error,
  69. } = useMetricsQuery(metricQueries, selection, {
  70. intervalLadder: widget.displayType === DisplayType.BAR ? 'bar' : 'dashboard',
  71. });
  72. const vizualizationComponent = useMemo(() => {
  73. if (widget.displayType === DisplayType.TABLE) {
  74. return (
  75. <MetricTableContainer
  76. metricQueries={metricQueries}
  77. timeseriesData={timeseriesData}
  78. isLoading={isLoading}
  79. />
  80. );
  81. }
  82. if (widget.displayType === DisplayType.BIG_NUMBER) {
  83. return (
  84. <MetricBigNumberContainer
  85. timeseriesData={timeseriesData}
  86. isLoading={isLoading}
  87. metricQueries={metricQueries}
  88. />
  89. );
  90. }
  91. return (
  92. <MetricChartContainer
  93. timeseriesData={timeseriesData}
  94. isLoading={isLoading}
  95. metricQueries={metricQueries}
  96. displayType={toMetricDisplayType(widget.displayType)}
  97. chartHeight={!showContextMenu ? 200 : undefined}
  98. />
  99. );
  100. }, [widget.displayType, metricQueries, timeseriesData, isLoading, showContextMenu]);
  101. if (!timeseriesData || isError) {
  102. if (isError) {
  103. const errorMessage =
  104. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  105. return (
  106. <Fragment>
  107. {renderErrorMessage?.(errorMessage)}
  108. <ErrorPanel>
  109. <IconWarning color="gray500" size="lg" />
  110. </ErrorPanel>
  111. </Fragment>
  112. );
  113. }
  114. }
  115. return (
  116. <DashboardsMEPContext.Provider
  117. value={{
  118. isMetricsData: undefined,
  119. setIsMetricsData: () => {},
  120. }}
  121. >
  122. <WidgetCardPanel isDragging={false}>
  123. <WidgetHeaderWrapper>
  124. <WidgetHeaderDescription>
  125. <WidgetTitleRow>
  126. <WidgetTitle>
  127. <TextOverflow>{widget.title || widgetMQL}</TextOverflow>
  128. </WidgetTitle>
  129. </WidgetTitleRow>
  130. </WidgetHeaderDescription>
  131. <ContextMenuWrapper>
  132. {showContextMenu && !isEditingDashboard && (
  133. <WidgetCardContextMenu
  134. organization={organization}
  135. widget={widget}
  136. selection={selection}
  137. showContextMenu
  138. isPreview={false}
  139. widgetLimitReached={false}
  140. onEdit={() => {
  141. router.push({
  142. pathname: `${location.pathname}${
  143. location.pathname.endsWith('/') ? '' : '/'
  144. }widget/${widget.id}/`,
  145. query: location.query,
  146. });
  147. }}
  148. router={router}
  149. location={location}
  150. onDelete={onDelete}
  151. onDuplicate={onDuplicate}
  152. />
  153. )}
  154. </ContextMenuWrapper>
  155. </WidgetHeaderWrapper>
  156. <ErrorBoundary>
  157. <WidgetCardBody
  158. isError={isError}
  159. noData={timeseriesData?.data.length === 0}
  160. renderErrorMessage={renderErrorMessage}
  161. error={error}
  162. >
  163. {vizualizationComponent}
  164. </WidgetCardBody>
  165. </ErrorBoundary>
  166. {isEditingDashboard && <Toolbar onDelete={onDelete} onDuplicate={onDuplicate} />}
  167. </WidgetCardPanel>
  168. </DashboardsMEPContext.Provider>
  169. );
  170. }
  171. function WidgetCardBody({children, isError, noData, renderErrorMessage, error}) {
  172. if (isError) {
  173. const errorMessage =
  174. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  175. return (
  176. <Fragment>
  177. {renderErrorMessage?.(errorMessage)}
  178. <ErrorPanel>
  179. <IconWarning color="gray500" size="lg" />
  180. </ErrorPanel>
  181. </Fragment>
  182. );
  183. }
  184. if (noData) {
  185. return (
  186. <EmptyMessage
  187. icon={<IconSearch size="xxl" />}
  188. title={t('No results')}
  189. description={t('No results found for the given query')}
  190. />
  191. );
  192. }
  193. return children;
  194. }
  195. const WidgetHeaderWrapper = styled('div')`
  196. min-height: 36px;
  197. width: 100%;
  198. display: flex;
  199. align-items: flex-start;
  200. justify-content: space-between;
  201. `;
  202. const ContextMenuWrapper = styled('div')`
  203. padding: ${space(2)} ${space(1)} 0 ${space(3)};
  204. `;
  205. const WidgetHeaderDescription = styled('div')`
  206. ${p => p.theme.overflowEllipsis};
  207. overflow-y: visible;
  208. `;
  209. const WidgetTitle = styled(HeaderTitle)`
  210. padding-left: ${space(3)};
  211. padding-top: ${space(2)};
  212. padding-right: ${space(1)};
  213. ${p => p.theme.overflowEllipsis};
  214. font-weight: normal;
  215. `;