widgetCard.tsx 7.1 KB

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