widgetCard.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 {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/metrics/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 timeseriesData={timeseriesData} isLoading={isLoading} />
  85. );
  86. }
  87. return (
  88. <MetricChartContainer
  89. timeseriesData={timeseriesData}
  90. isLoading={isLoading}
  91. metricQueries={metricQueries}
  92. displayType={toMetricDisplayType(widget.displayType)}
  93. chartHeight={!showContextMenu ? 200 : undefined}
  94. showLegend
  95. />
  96. );
  97. }, [widget.displayType, metricQueries, timeseriesData, isLoading, showContextMenu]);
  98. return (
  99. <DashboardsMEPContext.Provider
  100. value={{
  101. isMetricsData: undefined,
  102. setIsMetricsData: () => {},
  103. }}
  104. >
  105. <WidgetCardPanel isDragging={false}>
  106. <WidgetHeaderWrapper>
  107. <WidgetHeaderDescription>
  108. <WidgetTitleRow>
  109. <WidgetTitle>
  110. <TextOverflow>{widget.title || widgetMQL}</TextOverflow>
  111. </WidgetTitle>
  112. </WidgetTitleRow>
  113. </WidgetHeaderDescription>
  114. <ContextMenuWrapper>
  115. {showContextMenu && !isEditingDashboard && (
  116. <WidgetCardContextMenu
  117. organization={organization}
  118. widget={widget}
  119. selection={selection}
  120. showContextMenu
  121. isPreview={false}
  122. widgetLimitReached={false}
  123. onEdit={() => {
  124. router.push({
  125. pathname: `${location.pathname}${
  126. location.pathname.endsWith('/') ? '' : '/'
  127. }widget/${widget.id}/`,
  128. query: location.query,
  129. });
  130. }}
  131. router={router}
  132. location={location}
  133. onDelete={onDelete}
  134. onDuplicate={onDuplicate}
  135. />
  136. )}
  137. </ContextMenuWrapper>
  138. </WidgetHeaderWrapper>
  139. <ErrorBoundary>
  140. <WidgetCardBody
  141. isError={isError}
  142. timeseriesData={timeseriesData}
  143. renderErrorMessage={renderErrorMessage}
  144. error={error}
  145. >
  146. {vizualizationComponent}
  147. </WidgetCardBody>
  148. </ErrorBoundary>
  149. {isEditingDashboard && <Toolbar onDelete={onDelete} onDuplicate={onDuplicate} />}
  150. </WidgetCardPanel>
  151. </DashboardsMEPContext.Provider>
  152. );
  153. }
  154. function WidgetCardBody({children, isError, timeseriesData, renderErrorMessage, error}) {
  155. if (isError && !timeseriesData) {
  156. const errorMessage =
  157. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  158. return (
  159. <ErrorWrapper>
  160. {renderErrorMessage?.(errorMessage)}
  161. <ErrorPanel>
  162. <IconWarning color="gray500" size="lg" />
  163. </ErrorPanel>
  164. </ErrorWrapper>
  165. );
  166. }
  167. return children;
  168. }
  169. const WidgetHeaderWrapper = styled('div')`
  170. min-height: 36px;
  171. width: 100%;
  172. display: flex;
  173. align-items: flex-start;
  174. justify-content: space-between;
  175. `;
  176. const ContextMenuWrapper = styled('div')`
  177. padding: ${space(2)} ${space(1)} 0 ${space(3)};
  178. `;
  179. const WidgetHeaderDescription = styled('div')`
  180. ${p => p.theme.overflowEllipsis};
  181. overflow-y: visible;
  182. `;
  183. const WidgetTitle = styled(HeaderTitle)`
  184. padding-left: ${space(3)};
  185. padding-top: ${space(2)};
  186. padding-right: ${space(1)};
  187. ${p => p.theme.overflowEllipsis};
  188. font-weight: ${p => p.theme.fontWeightNormal};
  189. `;
  190. const ErrorWrapper = styled('div')`
  191. padding-top: ${space(1)};
  192. `;