dashboard.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import {Component} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import {closestCenter, DndContext} from '@dnd-kit/core';
  4. import {arrayMove, rectSortingStrategy, SortableContext} from '@dnd-kit/sortable';
  5. import styled from '@emotion/styled';
  6. import {Location} from 'history';
  7. import {validateWidget} from 'app/actionCreators/dashboards';
  8. import {addErrorMessage} from 'app/actionCreators/indicator';
  9. import {openAddDashboardWidgetModal} from 'app/actionCreators/modal';
  10. import {loadOrganizationTags} from 'app/actionCreators/tags';
  11. import {Client} from 'app/api';
  12. import space from 'app/styles/space';
  13. import {GlobalSelection, Organization} from 'app/types';
  14. import withApi from 'app/utils/withApi';
  15. import withGlobalSelection from 'app/utils/withGlobalSelection';
  16. import {DataSet} from './widget/utils';
  17. import AddWidget, {ADD_WIDGET_BUTTON_DRAG_ID} from './addWidget';
  18. import SortableWidget from './sortableWidget';
  19. import {DashboardDetails, Widget} from './types';
  20. type Props = {
  21. api: Client;
  22. organization: Organization;
  23. dashboard: DashboardDetails;
  24. selection: GlobalSelection;
  25. isEditing: boolean;
  26. router: InjectedRouter;
  27. location: Location;
  28. /**
  29. * Fired when widgets are added/removed/sorted.
  30. */
  31. onUpdate: (widgets: Widget[]) => void;
  32. onSetWidgetToBeUpdated: (widget: Widget) => void;
  33. paramDashboardId?: string;
  34. newWidget?: Widget;
  35. };
  36. class Dashboard extends Component<Props> {
  37. async componentDidMount() {
  38. const {api, organization, isEditing, newWidget} = this.props;
  39. // Load organization tags when in edit mode.
  40. if (isEditing) {
  41. this.fetchTags();
  42. }
  43. if (newWidget) {
  44. try {
  45. await validateWidget(api, organization.slug, newWidget);
  46. this.handleAddComplete(newWidget);
  47. } catch (error) {
  48. // Don't do anything, widget isn't valid
  49. addErrorMessage(error);
  50. }
  51. }
  52. }
  53. componentDidUpdate(prevProps: Props) {
  54. const {isEditing} = this.props;
  55. // Load organization tags when going into edit mode.
  56. // We use tags on the add widget modal.
  57. if (prevProps.isEditing !== isEditing && isEditing) {
  58. this.fetchTags();
  59. }
  60. }
  61. fetchTags() {
  62. const {api, organization, selection} = this.props;
  63. loadOrganizationTags(api, organization.slug, selection);
  64. }
  65. handleStartAdd = () => {
  66. const {organization, dashboard, selection} = this.props;
  67. openAddDashboardWidgetModal({
  68. organization,
  69. dashboard,
  70. selection,
  71. onAddWidget: this.handleAddComplete,
  72. });
  73. };
  74. handleOpenWidgetBuilder = () => {
  75. const {router, paramDashboardId, organization, location} = this.props;
  76. if (paramDashboardId) {
  77. router.push({
  78. pathname: `/organizations/${organization.slug}/dashboard/${paramDashboardId}/widget/new/`,
  79. query: {
  80. ...location.query,
  81. dataSet: DataSet.EVENTS,
  82. },
  83. });
  84. return;
  85. }
  86. router.push({
  87. pathname: `/organizations/${organization.slug}/dashboards/new/widget/new/`,
  88. query: {
  89. ...location.query,
  90. dataSet: DataSet.EVENTS,
  91. },
  92. });
  93. };
  94. handleAddComplete = (widget: Widget) => {
  95. this.props.onUpdate([...this.props.dashboard.widgets, widget]);
  96. };
  97. handleUpdateComplete = (index: number) => (nextWidget: Widget) => {
  98. const nextList = [...this.props.dashboard.widgets];
  99. nextList[index] = nextWidget;
  100. this.props.onUpdate(nextList);
  101. };
  102. handleDeleteWidget = (index: number) => () => {
  103. const nextList = [...this.props.dashboard.widgets];
  104. nextList.splice(index, 1);
  105. this.props.onUpdate(nextList);
  106. };
  107. handleEditWidget = (widget: Widget, index: number) => () => {
  108. const {
  109. organization,
  110. dashboard,
  111. selection,
  112. router,
  113. location,
  114. paramDashboardId,
  115. onSetWidgetToBeUpdated,
  116. } = this.props;
  117. if (organization.features.includes('metrics')) {
  118. onSetWidgetToBeUpdated(widget);
  119. if (paramDashboardId) {
  120. router.push({
  121. pathname: `/organizations/${organization.slug}/dashboard/${paramDashboardId}/widget/${index}/edit/`,
  122. query: {
  123. ...location.query,
  124. dataSet: DataSet.EVENTS,
  125. },
  126. });
  127. return;
  128. }
  129. router.push({
  130. pathname: `/organizations/${organization.slug}/dashboards/new/widget/${index}/edit/`,
  131. query: {
  132. ...location.query,
  133. dataSet: DataSet.EVENTS,
  134. },
  135. });
  136. }
  137. openAddDashboardWidgetModal({
  138. organization,
  139. dashboard,
  140. widget,
  141. selection,
  142. onAddWidget: this.handleAddComplete,
  143. onUpdateWidget: this.handleUpdateComplete(index),
  144. });
  145. };
  146. getWidgetIds() {
  147. return [
  148. ...this.props.dashboard.widgets.map((widget, index): string => {
  149. return generateWidgetId(widget, index);
  150. }),
  151. ADD_WIDGET_BUTTON_DRAG_ID,
  152. ];
  153. }
  154. renderWidget(widget: Widget, index: number) {
  155. const {isEditing} = this.props;
  156. const key = generateWidgetId(widget, index);
  157. const dragId = key;
  158. return (
  159. <SortableWidget
  160. key={key}
  161. widget={widget}
  162. dragId={dragId}
  163. isEditing={isEditing}
  164. onDelete={this.handleDeleteWidget(index)}
  165. onEdit={this.handleEditWidget(widget, index)}
  166. />
  167. );
  168. }
  169. render() {
  170. const {
  171. isEditing,
  172. onUpdate,
  173. dashboard: {widgets},
  174. organization,
  175. } = this.props;
  176. const items = this.getWidgetIds();
  177. return (
  178. <DndContext
  179. collisionDetection={closestCenter}
  180. onDragEnd={({over, active}) => {
  181. const activeDragId = active.id;
  182. const getIndex = items.indexOf.bind(items);
  183. const activeIndex = activeDragId ? getIndex(activeDragId) : -1;
  184. if (over && over.id !== ADD_WIDGET_BUTTON_DRAG_ID) {
  185. const overIndex = getIndex(over.id);
  186. if (activeIndex !== overIndex) {
  187. onUpdate(arrayMove(widgets, activeIndex, overIndex));
  188. }
  189. }
  190. }}
  191. >
  192. <WidgetContainer>
  193. <SortableContext items={items} strategy={rectSortingStrategy}>
  194. {widgets.map((widget, index) => this.renderWidget(widget, index))}
  195. {isEditing && (
  196. <AddWidget
  197. orgFeatures={organization.features}
  198. onAddWidget={this.handleStartAdd}
  199. onOpenWidgetBuilder={this.handleOpenWidgetBuilder}
  200. />
  201. )}
  202. </SortableContext>
  203. </WidgetContainer>
  204. </DndContext>
  205. );
  206. }
  207. }
  208. export default withApi(withGlobalSelection(Dashboard));
  209. const WidgetContainer = styled('div')`
  210. display: grid;
  211. grid-template-columns: repeat(2, minmax(0, 1fr));
  212. grid-auto-flow: row dense;
  213. grid-gap: ${space(2)};
  214. @media (min-width: ${p => p.theme.breakpoints[1]}) {
  215. grid-template-columns: repeat(4, minmax(0, 1fr));
  216. }
  217. @media (min-width: ${p => p.theme.breakpoints[3]}) {
  218. grid-template-columns: repeat(6, minmax(0, 1fr));
  219. }
  220. @media (min-width: ${p => p.theme.breakpoints[4]}) {
  221. grid-template-columns: repeat(8, minmax(0, 1fr));
  222. }
  223. `;
  224. function generateWidgetId(widget: Widget, index: number) {
  225. return widget.id ? `${widget.id}-index-${index}` : `index-${index}`;
  226. }