organizationContext.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import * as React from 'react';
  2. import {createContext} from 'react';
  3. import DocumentTitle from 'react-document-title';
  4. import {PlainRoute, RouteComponentProps} from 'react-router';
  5. import styled from '@emotion/styled';
  6. import * as Sentry from '@sentry/react';
  7. import {openSudo} from 'sentry/actionCreators/modal';
  8. import {fetchOrganizationDetails} from 'sentry/actionCreators/organization';
  9. import ProjectActions from 'sentry/actions/projectActions';
  10. import {Client} from 'sentry/api';
  11. import Alert from 'sentry/components/alert';
  12. import LoadingError from 'sentry/components/loadingError';
  13. import LoadingIndicator from 'sentry/components/loadingIndicator';
  14. import Sidebar from 'sentry/components/sidebar';
  15. import {ORGANIZATION_FETCH_ERROR_TYPES} from 'sentry/constants';
  16. import {t} from 'sentry/locale';
  17. import SentryTypes from 'sentry/sentryTypes';
  18. import ConfigStore from 'sentry/stores/configStore';
  19. import HookStore from 'sentry/stores/hookStore';
  20. import OrganizationStore from 'sentry/stores/organizationStore';
  21. import space from 'sentry/styles/space';
  22. import {Organization} from 'sentry/types';
  23. import {metric} from 'sentry/utils/analytics';
  24. import {callIfFunction} from 'sentry/utils/callIfFunction';
  25. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  26. import RequestError from 'sentry/utils/requestError/requestError';
  27. import withApi from 'sentry/utils/withApi';
  28. import withOrganizations from 'sentry/utils/withOrganizations';
  29. type Props = RouteComponentProps<{orgId: string}, {}> & {
  30. api: Client;
  31. routes: PlainRoute[];
  32. includeSidebar: boolean;
  33. useLastOrganization: boolean;
  34. organizationsLoading: boolean;
  35. organizations: Organization[];
  36. };
  37. type State = {
  38. organization: Organization | null;
  39. loading: boolean;
  40. dirty?: boolean;
  41. errorType?: string | null;
  42. error?: RequestError | null;
  43. hooks?: React.ReactNode[];
  44. prevProps: {
  45. orgId: string;
  46. organizationsLoading: boolean;
  47. location: RouteComponentProps<{orgId: string}, {}>['location'];
  48. };
  49. };
  50. const OrganizationContext = createContext<Organization | null>(null);
  51. class OrganizationContextContainer extends React.Component<Props, State> {
  52. static getDerivedStateFromProps(props: Readonly<Props>, prevState: State): State {
  53. const {prevProps} = prevState;
  54. if (OrganizationContextContainer.shouldRemount(prevProps, props)) {
  55. return OrganizationContextContainer.getDefaultState(props);
  56. }
  57. const {organizationsLoading, location, params} = props;
  58. const {orgId} = params;
  59. return {
  60. ...prevState,
  61. prevProps: {
  62. orgId,
  63. organizationsLoading,
  64. location,
  65. },
  66. };
  67. }
  68. static shouldRemount(prevProps: State['prevProps'], props: Props): boolean {
  69. const hasOrgIdAndChanged =
  70. prevProps.orgId && props.params.orgId && prevProps.orgId !== props.params.orgId;
  71. const hasOrgId =
  72. props.params.orgId ||
  73. (props.useLastOrganization && ConfigStore.get('lastOrganization'));
  74. // protect against the case where we finish fetching org details
  75. // and then `OrganizationsStore` finishes loading:
  76. // only fetch in the case where we don't have an orgId
  77. //
  78. // Compare `getOrganizationSlug` because we may have a last used org from server
  79. // if there is no orgId in the URL
  80. const organizationLoadingChanged =
  81. prevProps.organizationsLoading !== props.organizationsLoading &&
  82. props.organizationsLoading === false;
  83. return (
  84. hasOrgIdAndChanged ||
  85. (!hasOrgId && organizationLoadingChanged) ||
  86. (props.location.state === 'refresh' && prevProps.location.state !== 'refresh')
  87. );
  88. }
  89. static getDefaultState(props: Props): State {
  90. const prevProps = {
  91. orgId: props.params.orgId,
  92. organizationsLoading: props.organizationsLoading,
  93. location: props.location,
  94. };
  95. if (OrganizationContextContainer.isOrgStorePopulatedCorrectly(props)) {
  96. // retrieve initial state from store
  97. return {
  98. ...OrganizationStore.get(),
  99. prevProps,
  100. };
  101. }
  102. return {
  103. loading: true,
  104. error: null,
  105. errorType: null,
  106. organization: null,
  107. prevProps,
  108. };
  109. }
  110. static getOrganizationSlug(props: Props) {
  111. return (
  112. props.params.orgId ||
  113. ((props.useLastOrganization &&
  114. (ConfigStore.get('lastOrganization') ||
  115. props.organizations?.[0]?.slug)) as string)
  116. );
  117. }
  118. static isOrgChanging(props: Props): boolean {
  119. const {organization} = OrganizationStore.get();
  120. if (!organization) {
  121. return false;
  122. }
  123. return organization.slug !== OrganizationContextContainer.getOrganizationSlug(props);
  124. }
  125. static isOrgStorePopulatedCorrectly(props: Props) {
  126. const {organization, dirty} = OrganizationStore.get();
  127. return !dirty && organization && !OrganizationContextContainer.isOrgChanging(props);
  128. }
  129. static childContextTypes = {
  130. organization: SentryTypes.Organization,
  131. };
  132. constructor(props: Props) {
  133. super(props);
  134. this.state = OrganizationContextContainer.getDefaultState(props);
  135. }
  136. getChildContext() {
  137. return {
  138. organization: this.state.organization,
  139. };
  140. }
  141. componentDidMount() {
  142. this.fetchData(true);
  143. }
  144. componentDidUpdate(prevProps: Props) {
  145. const remountPrevProps: State['prevProps'] = {
  146. orgId: prevProps.params.orgId,
  147. organizationsLoading: prevProps.organizationsLoading,
  148. location: prevProps.location,
  149. };
  150. if (OrganizationContextContainer.shouldRemount(remountPrevProps, this.props)) {
  151. this.remountComponent();
  152. }
  153. }
  154. componentWillUnmount() {
  155. this.unlisteners.forEach(callIfFunction);
  156. }
  157. unlisteners = [
  158. ProjectActions.createSuccess.listen(() => this.onProjectCreation(), undefined),
  159. OrganizationStore.listen(data => this.loadOrganization(data), undefined),
  160. ];
  161. remountComponent = () => {
  162. this.setState(
  163. OrganizationContextContainer.getDefaultState(this.props),
  164. this.fetchData
  165. );
  166. };
  167. onProjectCreation() {
  168. // If a new project was created, we need to re-fetch the
  169. // org details endpoint, which will propagate re-rendering
  170. // for the entire component tree
  171. fetchOrganizationDetails(
  172. this.props.api,
  173. OrganizationContextContainer.getOrganizationSlug(this.props),
  174. true,
  175. false
  176. );
  177. }
  178. isLoading() {
  179. // In the absence of an organization slug, the loading state should be
  180. // derived from this.props.organizationsLoading from OrganizationsStore
  181. if (!OrganizationContextContainer.getOrganizationSlug(this.props)) {
  182. return this.props.organizationsLoading;
  183. }
  184. return this.state.loading;
  185. }
  186. fetchData(isInitialFetch = false) {
  187. if (!OrganizationContextContainer.getOrganizationSlug(this.props)) {
  188. return;
  189. }
  190. // fetch from the store, then fetch from the API if necessary
  191. if (OrganizationContextContainer.isOrgStorePopulatedCorrectly(this.props)) {
  192. return;
  193. }
  194. metric.mark({name: 'organization-details-fetch-start'});
  195. fetchOrganizationDetails(
  196. this.props.api,
  197. OrganizationContextContainer.getOrganizationSlug(this.props),
  198. !OrganizationContextContainer.isOrgChanging(this.props), // if true, will preserve a lightweight org that was fetched,
  199. isInitialFetch
  200. );
  201. }
  202. loadOrganization(orgData: State) {
  203. const {organization, error} = orgData;
  204. const hooks: React.ReactNode[] = [];
  205. if (organization && !error) {
  206. HookStore.get('organization:header').forEach(cb => {
  207. hooks.push(cb(organization));
  208. });
  209. // Configure scope to have organization tag
  210. Sentry.configureScope(scope => {
  211. // XXX(dcramer): this is duplicated in sdk.py on the backend
  212. scope.setTag('organization', organization.id);
  213. scope.setTag('organization.slug', organization.slug);
  214. scope.setContext('organization', {id: organization.id, slug: organization.slug});
  215. });
  216. } else if (error) {
  217. // If user is superuser, open sudo window
  218. const user = ConfigStore.get('user');
  219. if (!user || !user.isSuperuser || error.status !== 403) {
  220. // This `catch` can swallow up errors in development (and tests)
  221. // So let's log them. This may create some noise, especially the test case where
  222. // we specifically test this branch
  223. console.error(error); // eslint-disable-line no-console
  224. } else {
  225. openSudo({
  226. retryRequest: () => Promise.resolve(this.fetchData()),
  227. });
  228. }
  229. }
  230. this.setState({...orgData, hooks}, () => {
  231. // Take a measurement for when organization details are done loading and the new state is applied
  232. if (organization) {
  233. metric.measure({
  234. name: 'app.component.perf',
  235. start: 'organization-details-fetch-start',
  236. data: {
  237. name: 'org-details',
  238. route: getRouteStringFromRoutes(this.props.routes),
  239. organization_id: parseInt(organization.id, 10),
  240. },
  241. });
  242. }
  243. });
  244. }
  245. getOrganizationDetailsEndpoint() {
  246. return `/organizations/${OrganizationContextContainer.getOrganizationSlug(
  247. this.props
  248. )}/`;
  249. }
  250. getTitle() {
  251. if (this.state.organization) {
  252. return this.state.organization.name;
  253. }
  254. return 'Sentry';
  255. }
  256. renderSidebar(): React.ReactNode {
  257. if (!this.props.includeSidebar) {
  258. return null;
  259. }
  260. const {children: _, ...props} = this.props;
  261. return <Sidebar {...props} organization={this.state.organization as Organization} />;
  262. }
  263. renderError() {
  264. let errorComponent: React.ReactElement;
  265. switch (this.state.errorType) {
  266. case ORGANIZATION_FETCH_ERROR_TYPES.ORG_NO_ACCESS:
  267. // We can still render when an org can't be loaded due to 401. The
  268. // backend will handle redirects when this is a problem.
  269. return this.renderBody();
  270. case ORGANIZATION_FETCH_ERROR_TYPES.ORG_NOT_FOUND:
  271. errorComponent = (
  272. <Alert type="error">
  273. {t('The organization you were looking for was not found.')}
  274. </Alert>
  275. );
  276. break;
  277. default:
  278. errorComponent = <LoadingError onRetry={this.remountComponent} />;
  279. }
  280. return <ErrorWrapper>{errorComponent}</ErrorWrapper>;
  281. }
  282. renderBody() {
  283. return (
  284. <DocumentTitle title={this.getTitle()}>
  285. <OrganizationContext.Provider value={this.state.organization}>
  286. <div className="app">
  287. {this.state.hooks}
  288. {this.renderSidebar()}
  289. {this.props.children}
  290. </div>
  291. </OrganizationContext.Provider>
  292. </DocumentTitle>
  293. );
  294. }
  295. render() {
  296. if (this.isLoading()) {
  297. return (
  298. <LoadingIndicator triangle>
  299. {t('Loading data for your organization.')}
  300. </LoadingIndicator>
  301. );
  302. }
  303. if (this.state.error) {
  304. return (
  305. <React.Fragment>
  306. {this.renderSidebar()}
  307. {this.renderError()}
  308. </React.Fragment>
  309. );
  310. }
  311. return this.renderBody();
  312. }
  313. }
  314. export default withApi(
  315. withOrganizations(Sentry.withProfiler(OrganizationContextContainer))
  316. );
  317. export {OrganizationContextContainer as OrganizationLegacyContext, OrganizationContext};
  318. const ErrorWrapper = styled('div')`
  319. padding: ${space(3)};
  320. `;