widgetCard.tsx 6.3 KB

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