orgDashboards.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import * as React from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {Location} from 'history';
  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 './dashboard';
  15. import {DashboardDetails, DashboardListItem} from './types';
  16. type OrgDashboardsChildrenProps = {
  17. dashboard: DashboardDetails | null;
  18. dashboards: DashboardListItem[];
  19. error: boolean;
  20. onDashboardUpdate: (updatedDashboard: DashboardDetails) => void;
  21. };
  22. type Props = {
  23. api: Client;
  24. organization: Organization;
  25. params: {orgId: string; dashboardId?: string};
  26. location: Location;
  27. children: (props: OrgDashboardsChildrenProps) => React.ReactNode;
  28. };
  29. type State = {
  30. // endpoint response
  31. dashboards: DashboardListItem[] | null;
  32. /**
  33. * The currently selected dashboard.
  34. */
  35. selectedDashboard: DashboardDetails | null;
  36. } & AsyncComponent['state'];
  37. class OrgDashboards extends AsyncComponent<Props, State> {
  38. state: State = {
  39. // AsyncComponent state
  40. loading: true,
  41. reloading: false,
  42. error: false,
  43. errors: {},
  44. dashboards: [],
  45. selectedDashboard: null,
  46. };
  47. componentDidUpdate(prevProps: Props) {
  48. if (!isEqual(prevProps.params.dashboardId, this.props.params.dashboardId)) {
  49. this.remountComponent();
  50. }
  51. }
  52. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  53. const {organization, params} = this.props;
  54. const url = `/organizations/${organization.slug}/dashboards/`;
  55. const endpoints: ReturnType<AsyncComponent['getEndpoints']> = [['dashboards', url]];
  56. if (params.dashboardId) {
  57. endpoints.push(['selectedDashboard', `${url}${params.dashboardId}/`]);
  58. trackAnalyticsEvent({
  59. eventKey: 'dashboards2.view',
  60. eventName: 'Dashboards2: View dashboard',
  61. organization_id: parseInt(this.props.organization.id, 10),
  62. dashboard_id: params.dashboardId,
  63. });
  64. }
  65. return endpoints;
  66. }
  67. onDashboardUpdate(updatedDashboard: DashboardDetails) {
  68. this.setState({selectedDashboard: updatedDashboard});
  69. }
  70. getDashboards(): DashboardListItem[] {
  71. const {dashboards} = this.state;
  72. return Array.isArray(dashboards) ? dashboards : [];
  73. }
  74. onRequestSuccess({stateKey, data}) {
  75. const {params, organization, location} = this.props;
  76. if (stateKey === 'selectedDashboard') {
  77. if (organization.features.includes('dashboard-grid-layout')) {
  78. // Ensure unique IDs even on viewing default dashboard
  79. this.setState({[stateKey]: {...data, widgets: data.widgets.map(assignTempId)}});
  80. }
  81. return;
  82. }
  83. if (params.dashboardId) {
  84. return;
  85. }
  86. // If we don't have a selected dashboard, and one isn't going to arrive
  87. // we can redirect to the first dashboard in the list.
  88. const dashboardId = data.length ? data[0].id : 'default-overview';
  89. const url = `/organizations/${organization.slug}/dashboard/${dashboardId}/`;
  90. browserHistory.replace({
  91. pathname: url,
  92. query: {
  93. ...location.query,
  94. },
  95. });
  96. }
  97. renderLoading() {
  98. return (
  99. <PageContent>
  100. <LoadingIndicator />
  101. </PageContent>
  102. );
  103. }
  104. renderBody() {
  105. const {children} = this.props;
  106. const {selectedDashboard, error} = this.state;
  107. return children({
  108. error,
  109. dashboard: selectedDashboard,
  110. dashboards: this.getDashboards(),
  111. onDashboardUpdate: (updatedDashboard: DashboardDetails) =>
  112. this.onDashboardUpdate(updatedDashboard),
  113. });
  114. }
  115. renderError(error: Error) {
  116. const notFound = Object.values(this.state.errors).find(
  117. resp => resp && resp.status === 404
  118. );
  119. if (notFound) {
  120. return <NotFound />;
  121. }
  122. return super.renderError(error, true);
  123. }
  124. renderComponent() {
  125. const {organization, location} = this.props;
  126. if (!organization.features.includes('dashboards-basic')) {
  127. // Redirect to Dashboards v1
  128. browserHistory.replace({
  129. pathname: `/organizations/${organization.slug}/dashboards/`,
  130. query: {
  131. ...location.query,
  132. },
  133. });
  134. return null;
  135. }
  136. return (
  137. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  138. {super.renderComponent() as React.ReactChild}
  139. </SentryDocumentTitle>
  140. );
  141. }
  142. }
  143. export default OrgDashboards;