orgDashboards.tsx 6.4 KB

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