widgetCard.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 {getWidgetTitle} from 'sentry/utils/metrics';
  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. getMetricQueries,
  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. type Props = {
  29. isEditingDashboard: boolean;
  30. location: Location;
  31. organization: Organization;
  32. router: InjectedRouter;
  33. selection: PageFilters;
  34. widget: Widget;
  35. dashboardFilters?: DashboardFilters;
  36. index?: string;
  37. isMobile?: boolean;
  38. onDelete?: () => void;
  39. onDuplicate?: () => void;
  40. onEdit?: (index: string) => void;
  41. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  42. showContextMenu?: boolean;
  43. };
  44. export function MetricWidgetCard({
  45. organization,
  46. selection,
  47. widget,
  48. isEditingDashboard,
  49. onDelete,
  50. onDuplicate,
  51. location,
  52. router,
  53. dashboardFilters,
  54. renderErrorMessage,
  55. showContextMenu = true,
  56. }: Props) {
  57. const metricQueries = useMemo(
  58. () => getMetricQueries(widget, dashboardFilters),
  59. [widget, dashboardFilters]
  60. );
  61. const widgetMQL = useMemo(() => getWidgetTitle(metricQueries), [metricQueries]);
  62. const {
  63. data: timeseriesData,
  64. isLoading,
  65. isError,
  66. error,
  67. } = useMetricsQuery(metricQueries, selection, {
  68. intervalLadder: widget.displayType === DisplayType.BAR ? 'bar' : 'dashboard',
  69. });
  70. const vizualizationComponent = useMemo(() => {
  71. if (widget.displayType === DisplayType.TABLE) {
  72. return (
  73. <MetricTableContainer
  74. metricQueries={metricQueries}
  75. timeseriesData={timeseriesData}
  76. isLoading={isLoading}
  77. />
  78. );
  79. }
  80. if (widget.displayType === DisplayType.BIG_NUMBER) {
  81. return (
  82. <MetricBigNumberContainer
  83. timeseriesData={timeseriesData}
  84. isLoading={isLoading}
  85. metricQueries={metricQueries}
  86. />
  87. );
  88. }
  89. return (
  90. <MetricChartContainer
  91. timeseriesData={timeseriesData}
  92. isLoading={isLoading}
  93. metricQueries={metricQueries}
  94. displayType={toMetricDisplayType(widget.displayType)}
  95. chartHeight={!showContextMenu ? 200 : undefined}
  96. />
  97. );
  98. }, [widget.displayType, metricQueries, timeseriesData, isLoading, showContextMenu]);
  99. if (isError) {
  100. const errorMessage =
  101. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  102. return (
  103. <Fragment>
  104. {renderErrorMessage?.(errorMessage)}
  105. <ErrorPanel>
  106. <IconWarning color="gray500" size="lg" />
  107. </ErrorPanel>
  108. </Fragment>
  109. );
  110. }
  111. return (
  112. <DashboardsMEPContext.Provider
  113. value={{
  114. isMetricsData: undefined,
  115. setIsMetricsData: () => {},
  116. }}
  117. >
  118. <WidgetCardPanel isDragging={false}>
  119. <WidgetHeaderWrapper>
  120. <WidgetHeaderDescription>
  121. <WidgetTitleRow>
  122. <WidgetTitle>
  123. <TextOverflow>{widget.title || widgetMQL}</TextOverflow>
  124. </WidgetTitle>
  125. </WidgetTitleRow>
  126. </WidgetHeaderDescription>
  127. <ContextMenuWrapper>
  128. {showContextMenu && !isEditingDashboard && (
  129. <WidgetCardContextMenu
  130. organization={organization}
  131. widget={widget}
  132. selection={selection}
  133. showContextMenu
  134. isPreview={false}
  135. widgetLimitReached={false}
  136. onEdit={() => {
  137. router.push({
  138. pathname: `${location.pathname}${
  139. location.pathname.endsWith('/') ? '' : '/'
  140. }widget/${widget.id}/`,
  141. query: location.query,
  142. });
  143. }}
  144. router={router}
  145. location={location}
  146. onDelete={onDelete}
  147. onDuplicate={onDuplicate}
  148. />
  149. )}
  150. </ContextMenuWrapper>
  151. </WidgetHeaderWrapper>
  152. <WidgetCardBody
  153. isError={isError}
  154. noData={timeseriesData?.data.length === 0}
  155. renderErrorMessage={renderErrorMessage}
  156. error={error}
  157. >
  158. {vizualizationComponent}
  159. </WidgetCardBody>
  160. {isEditingDashboard && <Toolbar onDelete={onDelete} onDuplicate={onDuplicate} />}
  161. </WidgetCardPanel>
  162. </DashboardsMEPContext.Provider>
  163. );
  164. }
  165. function WidgetCardBody({children, isError, noData, renderErrorMessage, error}) {
  166. if (isError) {
  167. const errorMessage =
  168. error?.responseJSON?.detail?.toString() || t('Error while fetching metrics data');
  169. return (
  170. <Fragment>
  171. {renderErrorMessage?.(errorMessage)}
  172. <ErrorPanel>
  173. <IconWarning color="gray500" size="lg" />
  174. </ErrorPanel>
  175. </Fragment>
  176. );
  177. }
  178. if (noData) {
  179. return (
  180. <EmptyMessage
  181. icon={<IconSearch size="xxl" />}
  182. title={t('No results')}
  183. description={t('No results found for the given query')}
  184. />
  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: normal;
  209. `;