orgDashboards.tsx 3.9 KB

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