widgetCard.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import * as React from 'react';
  2. import LazyLoad from 'react-lazyload';
  3. import {Link, withRouter, WithRouterProps} from 'react-router';
  4. import {useSortable} from '@dnd-kit/sortable';
  5. import styled from '@emotion/styled';
  6. import {Location} from 'history';
  7. import isEqual from 'lodash/isEqual';
  8. import {openDashboardWidgetQuerySelectorModal} from 'sentry/actionCreators/modal';
  9. import {Client} from 'sentry/api';
  10. import {HeaderTitle} from 'sentry/components/charts/styles';
  11. import ErrorBoundary from 'sentry/components/errorBoundary';
  12. import MenuItem from 'sentry/components/menuItem';
  13. import {isSelectionEqual} from 'sentry/components/organizations/globalSelectionHeader/utils';
  14. import {Panel} from 'sentry/components/panels';
  15. import Placeholder from 'sentry/components/placeholder';
  16. import {IconDelete, IconEdit, IconGrabbable} from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import overflowEllipsis from 'sentry/styles/overflowEllipsis';
  19. import space from 'sentry/styles/space';
  20. import {GlobalSelection, Organization} from 'sentry/types';
  21. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  22. import {DisplayModes} from 'sentry/utils/discover/types';
  23. import withApi from 'sentry/utils/withApi';
  24. import withGlobalSelection from 'sentry/utils/withGlobalSelection';
  25. import withOrganization from 'sentry/utils/withOrganization';
  26. import {eventViewFromWidget} from 'sentry/views/dashboardsV2/utils';
  27. import {DisplayType} from 'sentry/views/dashboardsV2/widget/utils';
  28. import {DRAG_HANDLE_CLASS} from './gridLayout/dashboard';
  29. import ContextMenu from './contextMenu';
  30. import {Widget} from './types';
  31. import WidgetCardChart from './widgetCardChart';
  32. import WidgetQueries from './widgetQueries';
  33. type DraggableProps = Pick<ReturnType<typeof useSortable>, 'attributes' | 'listeners'>;
  34. type Props = WithRouterProps & {
  35. api: Client;
  36. organization: Organization;
  37. location: Location;
  38. isEditing: boolean;
  39. widget: Widget;
  40. selection: GlobalSelection;
  41. onDelete: () => void;
  42. onEdit: () => void;
  43. isSorting: boolean;
  44. currentWidgetDragging: boolean;
  45. showContextMenu?: boolean;
  46. hideToolbar?: boolean;
  47. draggableProps?: DraggableProps;
  48. renderErrorMessage?: (errorMessage?: string) => React.ReactNode;
  49. noLazyLoad?: boolean;
  50. };
  51. class WidgetCard extends React.Component<Props> {
  52. shouldComponentUpdate(nextProps: Props): boolean {
  53. if (
  54. !isEqual(nextProps.widget, this.props.widget) ||
  55. !isSelectionEqual(nextProps.selection, this.props.selection) ||
  56. this.props.isEditing !== nextProps.isEditing ||
  57. this.props.isSorting !== nextProps.isSorting ||
  58. this.props.hideToolbar !== nextProps.hideToolbar
  59. ) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. isAllowWidgetsToDiscover() {
  65. const {organization} = this.props;
  66. return organization.features.includes('connect-discover-and-dashboards');
  67. }
  68. renderToolbar() {
  69. const {onEdit, onDelete, draggableProps, hideToolbar, isEditing} = this.props;
  70. if (!isEditing) {
  71. return null;
  72. }
  73. return (
  74. <ToolbarPanel>
  75. <IconContainer style={{visibility: hideToolbar ? 'hidden' : 'visible'}}>
  76. <IconClick>
  77. <StyledIconGrabbable
  78. color="textColor"
  79. className={DRAG_HANDLE_CLASS}
  80. {...draggableProps?.listeners}
  81. {...draggableProps?.attributes}
  82. />
  83. </IconClick>
  84. <IconClick
  85. data-test-id="widget-edit"
  86. onClick={() => {
  87. onEdit();
  88. }}
  89. >
  90. <IconEdit color="textColor" />
  91. </IconClick>
  92. <IconClick
  93. data-test-id="widget-delete"
  94. onClick={() => {
  95. onDelete();
  96. }}
  97. >
  98. <IconDelete color="textColor" />
  99. </IconClick>
  100. </IconContainer>
  101. </ToolbarPanel>
  102. );
  103. }
  104. renderContextMenu() {
  105. const {widget, selection, organization, showContextMenu} = this.props;
  106. if (!showContextMenu) {
  107. return null;
  108. }
  109. const menuOptions: React.ReactNode[] = [];
  110. if (
  111. (widget.displayType === 'table' || this.isAllowWidgetsToDiscover()) &&
  112. organization.features.includes('discover-basic')
  113. ) {
  114. // Open Widget in Discover
  115. if (widget.queries.length) {
  116. const eventView = eventViewFromWidget(
  117. widget.title,
  118. widget.queries[0],
  119. selection,
  120. widget.displayType
  121. );
  122. const discoverLocation = eventView.getResultsViewUrlTarget(organization.slug);
  123. if (this.isAllowWidgetsToDiscover()) {
  124. // Pull a max of 3 valid Y-Axis from the widget
  125. const yAxisOptions = eventView.getYAxisOptions().map(({value}) => value);
  126. discoverLocation.query.yAxis = widget.queries[0].fields
  127. .filter(field => yAxisOptions.includes(field))
  128. .slice(0, 3);
  129. switch (widget.displayType) {
  130. case DisplayType.WORLD_MAP:
  131. discoverLocation.query.display = DisplayModes.WORLDMAP;
  132. break;
  133. case DisplayType.BAR:
  134. discoverLocation.query.display = DisplayModes.BAR;
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. if (widget.queries.length === 1) {
  141. menuOptions.push(
  142. <Link
  143. key="open-discover-link"
  144. to={discoverLocation}
  145. onClick={() => {
  146. trackAdvancedAnalyticsEvent('dashboards_views.open_in_discover.opened', {
  147. organization,
  148. widget_type: widget.displayType,
  149. });
  150. }}
  151. >
  152. <StyledMenuItem key="open-discover">{t('Open in Discover')}</StyledMenuItem>
  153. </Link>
  154. );
  155. } else {
  156. menuOptions.push(
  157. <StyledMenuItem
  158. key="open-discover"
  159. onClick={event => {
  160. event.preventDefault();
  161. trackAdvancedAnalyticsEvent('dashboards_views.query_selector.opened', {
  162. organization,
  163. widget_type: widget.displayType,
  164. });
  165. openDashboardWidgetQuerySelectorModal({organization, widget});
  166. }}
  167. >
  168. {t('Open in Discover')}
  169. </StyledMenuItem>
  170. );
  171. }
  172. }
  173. }
  174. if (!menuOptions.length) {
  175. return null;
  176. }
  177. return (
  178. <ContextWrapper>
  179. <ContextMenu>{menuOptions}</ContextMenu>
  180. </ContextWrapper>
  181. );
  182. }
  183. renderChart() {
  184. const {widget, api, organization, selection, renderErrorMessage, location, router} =
  185. this.props;
  186. return (
  187. <WidgetQueries
  188. api={api}
  189. organization={organization}
  190. widget={widget}
  191. selection={selection}
  192. >
  193. {({tableResults, timeseriesResults, errorMessage, loading}) => {
  194. return (
  195. <React.Fragment>
  196. {typeof renderErrorMessage === 'function'
  197. ? renderErrorMessage(errorMessage)
  198. : null}
  199. <WidgetCardChart
  200. timeseriesResults={timeseriesResults}
  201. tableResults={tableResults}
  202. errorMessage={errorMessage}
  203. loading={loading}
  204. location={location}
  205. widget={widget}
  206. selection={selection}
  207. router={router}
  208. organization={organization}
  209. />
  210. {this.renderToolbar()}
  211. </React.Fragment>
  212. );
  213. }}
  214. </WidgetQueries>
  215. );
  216. }
  217. render() {
  218. const {widget, noLazyLoad} = this.props;
  219. return (
  220. <ErrorBoundary
  221. customComponent={<ErrorCard>{t('Error loading widget data')}</ErrorCard>}
  222. >
  223. <StyledPanel isDragging={false}>
  224. <WidgetHeader>
  225. <WidgetTitle>{widget.title}</WidgetTitle>
  226. {this.renderContextMenu()}
  227. </WidgetHeader>
  228. {noLazyLoad ? (
  229. this.renderChart()
  230. ) : (
  231. <LazyLoad once resize height={200}>
  232. {this.renderChart()}
  233. </LazyLoad>
  234. )}
  235. </StyledPanel>
  236. </ErrorBoundary>
  237. );
  238. }
  239. }
  240. export default withApi(withOrganization(withGlobalSelection(withRouter(WidgetCard))));
  241. const ErrorCard = styled(Placeholder)`
  242. display: flex;
  243. align-items: center;
  244. justify-content: center;
  245. background-color: ${p => p.theme.alert.error.backgroundLight};
  246. border: 1px solid ${p => p.theme.alert.error.border};
  247. color: ${p => p.theme.alert.error.textLight};
  248. border-radius: ${p => p.theme.borderRadius};
  249. margin-bottom: ${space(2)};
  250. `;
  251. const StyledPanel = styled(Panel, {
  252. shouldForwardProp: prop => prop !== 'isDragging',
  253. })<{
  254. isDragging: boolean;
  255. }>`
  256. margin: 0;
  257. visibility: ${p => (p.isDragging ? 'hidden' : 'visible')};
  258. /* If a panel overflows due to a long title stretch its grid sibling */
  259. height: 100%;
  260. min-height: 96px;
  261. display: flex;
  262. flex-direction: column;
  263. `;
  264. const ToolbarPanel = styled('div')`
  265. position: absolute;
  266. top: 0;
  267. left: 0;
  268. z-index: auto;
  269. width: 100%;
  270. height: 100%;
  271. display: flex;
  272. justify-content: flex-end;
  273. align-items: flex-start;
  274. background-color: ${p => p.theme.overlayBackgroundAlpha};
  275. border-radius: ${p => p.theme.borderRadius};
  276. `;
  277. const IconContainer = styled('div')`
  278. display: flex;
  279. margin: 10px ${space(2)};
  280. touch-action: none;
  281. `;
  282. const IconClick = styled('div')`
  283. padding: ${space(1)};
  284. &:hover {
  285. cursor: pointer;
  286. }
  287. `;
  288. const StyledIconGrabbable = styled(IconGrabbable)`
  289. &:hover {
  290. cursor: grab;
  291. }
  292. `;
  293. const WidgetTitle = styled(HeaderTitle)`
  294. ${overflowEllipsis};
  295. `;
  296. const WidgetHeader = styled('div')`
  297. padding: ${space(2)} ${space(3)} 0 ${space(3)};
  298. width: 100%;
  299. display: flex;
  300. justify-content: space-between;
  301. `;
  302. const ContextWrapper = styled('div')`
  303. margin-left: ${space(1)};
  304. `;
  305. const StyledMenuItem = styled(MenuItem)`
  306. white-space: nowrap;
  307. color: ${p => p.theme.textColor};
  308. :hover {
  309. color: ${p => p.theme.textColor};
  310. }
  311. `;