orgDashboards.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {browserHistory} from 'react-router';
  2. import {Location} from 'history';
  3. import isEmpty from 'lodash/isEmpty';
  4. import isEqual from 'lodash/isEqual';
  5. import {Client} from 'sentry/api';
  6. import AsyncComponent from 'sentry/components/asyncComponent';
  7. import NotFound from 'sentry/components/errors/notFound';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  10. import {t} from 'sentry/locale';
  11. import {PageContent} from 'sentry/styles/organization';
  12. import {Organization} from 'sentry/types';
  13. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  14. import {assignTempId} from './layoutUtils';
  15. import {DashboardDetails, DashboardListItem} from './types';
  16. import {hasSavedPageFilters} from './utils';
  17. type OrgDashboardsChildrenProps = {
  18. dashboard: DashboardDetails | null;
  19. dashboards: DashboardListItem[];
  20. error: boolean;
  21. onDashboardUpdate: (updatedDashboard: DashboardDetails) => void;
  22. };
  23. type Props = {
  24. api: Client;
  25. children: (props: OrgDashboardsChildrenProps) => React.ReactNode;
  26. location: Location;
  27. organization: Organization;
  28. params: {orgId: string; dashboardId?: string};
  29. };
  30. type State = {
  31. // endpoint response
  32. dashboards: DashboardListItem[] | null;
  33. /**
  34. * The currently selected dashboard.
  35. */
  36. selectedDashboard: DashboardDetails | null;
  37. } & AsyncComponent['state'];
  38. class OrgDashboards extends AsyncComponent<Props, State> {
  39. state: State = {
  40. // AsyncComponent state
  41. loading: true,
  42. reloading: false,
  43. error: false,
  44. errors: {},
  45. dashboards: [],
  46. selectedDashboard: null,
  47. };
  48. componentDidUpdate(prevProps: Props) {
  49. if (!isEqual(prevProps.params.dashboardId, this.props.params.dashboardId)) {
  50. this.remountComponent();
  51. }
  52. }
  53. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  54. const {organization, params} = this.props;
  55. const url = `/organizations/${organization.slug}/dashboards/`;
  56. const endpoints: ReturnType<AsyncComponent['getEndpoints']> = [['dashboards', url]];
  57. if (params.dashboardId) {
  58. endpoints.push(['selectedDashboard', `${url}${params.dashboardId}/`]);
  59. trackAnalyticsEvent({
  60. eventKey: 'dashboards2.view',
  61. eventName: 'Dashboards2: View dashboard',
  62. organization_id: parseInt(this.props.organization.id, 10),
  63. dashboard_id: params.dashboardId,
  64. });
  65. }
  66. return endpoints;
  67. }
  68. onDashboardUpdate(updatedDashboard: DashboardDetails) {
  69. this.setState({selectedDashboard: updatedDashboard});
  70. }
  71. getDashboards(): DashboardListItem[] {
  72. const {dashboards} = this.state;
  73. return Array.isArray(dashboards) ? dashboards : [];
  74. }
  75. onRequestSuccess({stateKey, data}) {
  76. const {params, organization, location} = this.props;
  77. if (params.dashboardId || stateKey === 'selectedDashboard') {
  78. if (
  79. organization.features.includes('dashboards-top-level-filter') &&
  80. stateKey === 'selectedDashboard' &&
  81. hasSavedPageFilters(data) &&
  82. isEmpty(location.query)
  83. ) {
  84. browserHistory.replace({
  85. ...location,
  86. query: {
  87. ...location.query,
  88. project: data.projects,
  89. environment: data.environment,
  90. statsPeriod: data.period,
  91. start: data.start,
  92. end: data.end,
  93. },
  94. });
  95. }
  96. return;
  97. }
  98. // If we don't have a selected dashboard, and one isn't going to arrive
  99. // we can redirect to the first dashboard in the list.
  100. const dashboardId = data.length ? data[0].id : 'default-overview';
  101. const url = `/organizations/${organization.slug}/dashboard/${dashboardId}/`;
  102. browserHistory.replace({
  103. pathname: url,
  104. query: {
  105. ...location.query,
  106. },
  107. });
  108. }
  109. renderLoading() {
  110. return (
  111. <PageContent>
  112. <LoadingIndicator />
  113. </PageContent>
  114. );
  115. }
  116. renderBody() {
  117. const {children, organization} = this.props;
  118. const {selectedDashboard, error} = this.state;
  119. let dashboard = selectedDashboard;
  120. if (organization.features.includes('dashboard-grid-layout')) {
  121. // Ensure there are always tempIds for grid layout
  122. // This is needed because there are cases where the dashboard
  123. // renders before the onRequestSuccess setState is processed
  124. // and will caused stacked widgets because of missing tempIds
  125. dashboard = selectedDashboard
  126. ? {
  127. ...selectedDashboard,
  128. widgets: selectedDashboard.widgets.map(assignTempId),
  129. }
  130. : null;
  131. }
  132. return children({
  133. error,
  134. dashboard,
  135. dashboards: this.getDashboards(),
  136. onDashboardUpdate: (updatedDashboard: DashboardDetails) =>
  137. this.onDashboardUpdate(updatedDashboard),
  138. });
  139. }
  140. renderError(error: Error) {
  141. const notFound = Object.values(this.state.errors).find(
  142. resp => resp && resp.status === 404
  143. );
  144. if (notFound) {
  145. return <NotFound />;
  146. }
  147. return super.renderError(error, true);
  148. }
  149. renderComponent() {
  150. const {organization, location} = this.props;
  151. const {loading, selectedDashboard} = this.state;
  152. if (!organization.features.includes('dashboards-basic')) {
  153. // Redirect to Dashboards v1
  154. browserHistory.replace({
  155. pathname: `/organizations/${organization.slug}/dashboards/`,
  156. query: {
  157. ...location.query,
  158. },
  159. });
  160. return null;
  161. }
  162. if (
  163. loading ||
  164. (organization.features.includes('dashboards-top-level-filter') &&
  165. selectedDashboard &&
  166. hasSavedPageFilters(selectedDashboard) &&
  167. isEmpty(location.query))
  168. ) {
  169. // Block dashboard from rendering if the dashboard has filters and
  170. // the URL does not contain filters yet. The filters can either match the
  171. // saved filters, or can be different (i.e. sharing an unsaved state)
  172. return this.renderLoading();
  173. }
  174. return (
  175. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  176. {super.renderComponent() as React.ReactChild}
  177. </SentryDocumentTitle>
  178. );
  179. }
  180. }
  181. export default OrgDashboards;