orgDashboards.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. const queryParamFilters = new Set([
  79. 'project',
  80. 'environment',
  81. 'statsPeriod',
  82. 'start',
  83. 'end',
  84. 'utc',
  85. 'release',
  86. ]);
  87. if (
  88. organization.features.includes('dashboards-top-level-filter') &&
  89. stateKey === 'selectedDashboard' &&
  90. // Only redirect if there are saved filters and none of the filters
  91. // appear in the query params
  92. hasSavedPageFilters(data) &&
  93. isEmpty(
  94. Object.keys(location.query).filter(unsavedQueryParam =>
  95. queryParamFilters.has(unsavedQueryParam)
  96. )
  97. )
  98. ) {
  99. browserHistory.replace({
  100. ...location,
  101. query: {
  102. ...location.query,
  103. project: data.projects,
  104. environment: data.environment,
  105. statsPeriod: data.period,
  106. start: data.start,
  107. end: data.end,
  108. utc: data.utc,
  109. },
  110. });
  111. }
  112. return;
  113. }
  114. // If we don't have a selected dashboard, and one isn't going to arrive
  115. // we can redirect to the first dashboard in the list.
  116. const dashboardId = data.length ? data[0].id : 'default-overview';
  117. const url = `/organizations/${organization.slug}/dashboard/${dashboardId}/`;
  118. browserHistory.replace({
  119. pathname: url,
  120. query: {
  121. ...location.query,
  122. },
  123. });
  124. }
  125. renderLoading() {
  126. return (
  127. <PageContent>
  128. <LoadingIndicator />
  129. </PageContent>
  130. );
  131. }
  132. renderBody() {
  133. const {children} = this.props;
  134. const {selectedDashboard, error} = this.state;
  135. let dashboard = selectedDashboard;
  136. // Ensure there are always tempIds for grid layout
  137. // This is needed because there are cases where the dashboard
  138. // renders before the onRequestSuccess setState is processed
  139. // and will caused stacked widgets because of missing tempIds
  140. dashboard = selectedDashboard
  141. ? {
  142. ...selectedDashboard,
  143. widgets: selectedDashboard.widgets.map(assignTempId),
  144. }
  145. : null;
  146. return children({
  147. error,
  148. dashboard,
  149. dashboards: this.getDashboards(),
  150. onDashboardUpdate: (updatedDashboard: DashboardDetails) =>
  151. this.onDashboardUpdate(updatedDashboard),
  152. });
  153. }
  154. renderError(error: Error) {
  155. const notFound = Object.values(this.state.errors).find(
  156. resp => resp && resp.status === 404
  157. );
  158. if (notFound) {
  159. return <NotFound />;
  160. }
  161. return super.renderError(error, true);
  162. }
  163. renderComponent() {
  164. const {organization, location} = this.props;
  165. const {loading, selectedDashboard} = this.state;
  166. if (!organization.features.includes('dashboards-basic')) {
  167. // Redirect to Dashboards v1
  168. browserHistory.replace({
  169. pathname: `/organizations/${organization.slug}/dashboards/`,
  170. query: {
  171. ...location.query,
  172. },
  173. });
  174. return null;
  175. }
  176. if (
  177. loading &&
  178. organization.features.includes('dashboards-top-level-filter') &&
  179. selectedDashboard &&
  180. hasSavedPageFilters(selectedDashboard) &&
  181. isEmpty(location.query)
  182. ) {
  183. // Block dashboard from rendering if the dashboard has filters and
  184. // the URL does not contain filters yet. The filters can either match the
  185. // saved filters, or can be different (i.e. sharing an unsaved state)
  186. return this.renderLoading();
  187. }
  188. return (
  189. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  190. {super.renderComponent() as React.ReactChild}
  191. </SentryDocumentTitle>
  192. );
  193. }
  194. }
  195. export default OrgDashboards;