widgetCard.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {Fragment, useMemo} from 'react';
  2. import type {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import ErrorPanel from 'sentry/components/charts/errorPanel';
  6. import {HeaderTitle} from 'sentry/components/charts/styles';
  7. import EmptyMessage from 'sentry/components/emptyMessage';
  8. import TextOverflow from 'sentry/components/textOverflow';
  9. import {IconSearch, 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/ddm/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. if (isError) {
  101. const errorMessage =
  102. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  103. return (
  104. <Fragment>
  105. {renderErrorMessage?.(errorMessage)}
  106. <ErrorPanel>
  107. <IconWarning color="gray500" size="lg" />
  108. </ErrorPanel>
  109. </Fragment>
  110. );
  111. }
  112. return (
  113. <DashboardsMEPContext.Provider
  114. value={{
  115. isMetricsData: undefined,
  116. setIsMetricsData: () => {},
  117. }}
  118. >
  119. <WidgetCardPanel isDragging={false}>
  120. <WidgetHeaderWrapper>
  121. <WidgetHeaderDescription>
  122. <WidgetTitleRow>
  123. <WidgetTitle>
  124. <TextOverflow>{widget.title || widgetMQL}</TextOverflow>
  125. </WidgetTitle>
  126. </WidgetTitleRow>
  127. </WidgetHeaderDescription>
  128. <ContextMenuWrapper>
  129. {showContextMenu && !isEditingDashboard && (
  130. <WidgetCardContextMenu
  131. organization={organization}
  132. widget={widget}
  133. selection={selection}
  134. showContextMenu
  135. isPreview={false}
  136. widgetLimitReached={false}
  137. onEdit={() => {
  138. router.push({
  139. pathname: `${location.pathname}${
  140. location.pathname.endsWith('/') ? '' : '/'
  141. }widget/${widget.id}/`,
  142. query: location.query,
  143. });
  144. }}
  145. router={router}
  146. location={location}
  147. onDelete={onDelete}
  148. onDuplicate={onDuplicate}
  149. />
  150. )}
  151. </ContextMenuWrapper>
  152. </WidgetHeaderWrapper>
  153. <WidgetCardBody
  154. isError={isError}
  155. noData={timeseriesData?.data.length === 0}
  156. renderErrorMessage={renderErrorMessage}
  157. error={error}
  158. >
  159. {vizualizationComponent}
  160. </WidgetCardBody>
  161. {isEditingDashboard && <Toolbar onDelete={onDelete} onDuplicate={onDuplicate} />}
  162. </WidgetCardPanel>
  163. </DashboardsMEPContext.Provider>
  164. );
  165. }
  166. function WidgetCardBody({children, isError, noData, renderErrorMessage, error}) {
  167. if (isError) {
  168. const errorMessage =
  169. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  170. return (
  171. <Fragment>
  172. {renderErrorMessage?.(errorMessage)}
  173. <ErrorPanel>
  174. <IconWarning color="gray500" size="lg" />
  175. </ErrorPanel>
  176. </Fragment>
  177. );
  178. }
  179. if (noData) {
  180. return (
  181. <EmptyMessage
  182. icon={<IconSearch size="xxl" />}
  183. title={t('No results')}
  184. description={t('No results found for the given query')}
  185. />
  186. );
  187. }
  188. return children;
  189. }
  190. const WidgetHeaderWrapper = styled('div')`
  191. min-height: 36px;
  192. width: 100%;
  193. display: flex;
  194. align-items: flex-start;
  195. justify-content: space-between;
  196. `;
  197. const ContextMenuWrapper = styled('div')`
  198. padding: ${space(2)} ${space(1)} 0 ${space(3)};
  199. `;
  200. const WidgetHeaderDescription = styled('div')`
  201. ${p => p.theme.overflowEllipsis};
  202. overflow-y: visible;
  203. `;
  204. const WidgetTitle = styled(HeaderTitle)`
  205. padding-left: ${space(3)};
  206. padding-top: ${space(2)};
  207. padding-right: ${space(1)};
  208. ${p => p.theme.overflowEllipsis};
  209. font-weight: normal;
  210. `;