widgetCard.tsx 6.3 KB

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