orgDashboards.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, organization} = this.props;
  134. const {selectedDashboard, error} = this.state;
  135. let dashboard = selectedDashboard;
  136. if (organization.features.includes('dashboard-grid-layout')) {
  137. // Ensure there are always tempIds for grid layout
  138. // This is needed because there are cases where the dashboard
  139. // renders before the onRequestSuccess setState is processed
  140. // and will caused stacked widgets because of missing tempIds
  141. dashboard = selectedDashboard
  142. ? {
  143. ...selectedDashboard,
  144. widgets: selectedDashboard.widgets.map(assignTempId),
  145. }
  146. : null;
  147. }
  148. return children({
  149. error,
  150. dashboard,
  151. dashboards: this.getDashboards(),
  152. onDashboardUpdate: (updatedDashboard: DashboardDetails) =>
  153. this.onDashboardUpdate(updatedDashboard),
  154. });
  155. }
  156. renderError(error: Error) {
  157. const notFound = Object.values(this.state.errors).find(
  158. resp => resp && resp.status === 404
  159. );
  160. if (notFound) {
  161. return <NotFound />;
  162. }
  163. return super.renderError(error, true);
  164. }
  165. renderComponent() {
  166. const {organization, location} = this.props;
  167. const {loading, selectedDashboard} = this.state;
  168. if (!organization.features.includes('dashboards-basic')) {
  169. // Redirect to Dashboards v1
  170. browserHistory.replace({
  171. pathname: `/organizations/${organization.slug}/dashboards/`,
  172. query: {
  173. ...location.query,
  174. },
  175. });
  176. return null;
  177. }
  178. if (
  179. loading &&
  180. organization.features.includes('dashboards-top-level-filter') &&
  181. selectedDashboard &&
  182. hasSavedPageFilters(selectedDashboard) &&
  183. isEmpty(location.query)
  184. ) {
  185. // Block dashboard from rendering if the dashboard has filters and
  186. // the URL does not contain filters yet. The filters can either match the
  187. // saved filters, or can be different (i.e. sharing an unsaved state)
  188. return this.renderLoading();
  189. }
  190. return (
  191. <SentryDocumentTitle title={t('Dashboards')} orgSlug={organization.slug}>
  192. {super.renderComponent() as React.ReactChild}
  193. </SentryDocumentTitle>
  194. );
  195. }
  196. }
  197. export default OrgDashboards;