index.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import {createContext} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import isEqual from 'lodash/isEqual';
  6. import pick from 'lodash/pick';
  7. import Alert from 'sentry/components/alert';
  8. import AsyncComponent from 'sentry/components/asyncComponent';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import NoProjectMessage from 'sentry/components/noProjectMessage';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  13. import PickProjectToContinue from 'sentry/components/pickProjectToContinue';
  14. import {PAGE_URL_PARAM, URL_PARAM} from 'sentry/constants/pageFilters';
  15. import {IconInfo} from 'sentry/icons';
  16. import {t} from 'sentry/locale';
  17. import {PageContent} from 'sentry/styles/organization';
  18. import space from 'sentry/styles/space';
  19. import {
  20. Deploy,
  21. Organization,
  22. PageFilters,
  23. ReleaseMeta,
  24. ReleaseProject,
  25. ReleaseWithHealth,
  26. SessionApiResponse,
  27. SessionFieldWithOperation,
  28. } from 'sentry/types';
  29. import {formatVersion} from 'sentry/utils/formatters';
  30. import routeTitleGen from 'sentry/utils/routeTitle';
  31. import {getCount} from 'sentry/utils/sessions';
  32. import withOrganization from 'sentry/utils/withOrganization';
  33. import withPageFilters from 'sentry/utils/withPageFilters';
  34. import AsyncView from 'sentry/views/asyncView';
  35. import {getReleaseBounds, ReleaseBounds} from '../utils';
  36. import ReleaseHeader from './header/releaseHeader';
  37. type ReleaseContextType = {
  38. deploys: Deploy[];
  39. hasHealthData: boolean;
  40. project: Required<ReleaseProject>;
  41. refetchData: () => void;
  42. release: ReleaseWithHealth;
  43. releaseBounds: ReleaseBounds;
  44. releaseMeta: ReleaseMeta;
  45. };
  46. const ReleaseContext = createContext<ReleaseContextType>({} as ReleaseContextType);
  47. type RouteParams = {
  48. orgId: string;
  49. release: string;
  50. };
  51. type Props = RouteComponentProps<RouteParams, {}> & {
  52. organization: Organization;
  53. releaseMeta: ReleaseMeta;
  54. selection: PageFilters;
  55. };
  56. type State = {
  57. deploys: Deploy[];
  58. release: ReleaseWithHealth;
  59. sessions: SessionApiResponse | null;
  60. } & AsyncView['state'];
  61. class ReleasesDetail extends AsyncView<Props, State> {
  62. shouldReload = true;
  63. getTitle() {
  64. const {params, organization, selection} = this.props;
  65. const {release} = this.state;
  66. // The release details page will always have only one project selected
  67. const project = release?.projects.find(p => p.id === selection.projects[0]);
  68. return routeTitleGen(
  69. t('Release %s', formatVersion(params.release)),
  70. organization.slug,
  71. false,
  72. project?.slug
  73. );
  74. }
  75. getDefaultState() {
  76. return {
  77. ...super.getDefaultState(),
  78. deploys: [],
  79. sessions: null,
  80. };
  81. }
  82. componentDidUpdate(prevProps: Props, prevState: State) {
  83. const {organization, params, location} = this.props;
  84. if (
  85. prevProps.params.release !== params.release ||
  86. prevProps.organization.slug !== organization.slug ||
  87. !isEqual(
  88. this.pickLocationQuery(prevProps.location),
  89. this.pickLocationQuery(location)
  90. )
  91. ) {
  92. super.componentDidUpdate(prevProps, prevState);
  93. }
  94. }
  95. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  96. const {organization, location, params, releaseMeta} = this.props;
  97. const basePath = `/organizations/${organization.slug}/releases/${encodeURIComponent(
  98. params.release
  99. )}/`;
  100. const endpoints: ReturnType<AsyncView['getEndpoints']> = [
  101. [
  102. 'release',
  103. basePath,
  104. {
  105. query: {
  106. adoptionStages: 1,
  107. ...normalizeDateTimeParams(this.pickLocationQuery(location)),
  108. },
  109. },
  110. ],
  111. ];
  112. if (releaseMeta.deployCount > 0) {
  113. endpoints.push(['deploys', `${basePath}deploys/`]);
  114. }
  115. // Used to figure out if the release has any health data
  116. endpoints.push([
  117. 'sessions',
  118. `/organizations/${organization.slug}/sessions/`,
  119. {
  120. query: {
  121. project: location.query.project,
  122. environment: location.query.environment ?? [],
  123. query: `release:"${params.release}"`,
  124. field: 'sum(session)',
  125. statsPeriod: '90d',
  126. interval: '1d',
  127. },
  128. },
  129. ]);
  130. return endpoints;
  131. }
  132. pickLocationQuery(location: Location) {
  133. return pick(location.query, [
  134. ...Object.values(URL_PARAM),
  135. ...Object.values(PAGE_URL_PARAM),
  136. ]);
  137. }
  138. renderError(...args) {
  139. const possiblyWrongProject = Object.values(this.state.errors).find(
  140. e => e?.status === 404 || e?.status === 403
  141. );
  142. if (possiblyWrongProject) {
  143. return (
  144. <PageContent>
  145. <Alert type="error" showIcon>
  146. {t('This release may not be in your selected project.')}
  147. </Alert>
  148. </PageContent>
  149. );
  150. }
  151. return super.renderError(...args);
  152. }
  153. renderLoading() {
  154. return (
  155. <PageContent>
  156. <LoadingIndicator />
  157. </PageContent>
  158. );
  159. }
  160. renderBody() {
  161. const {organization, location, selection, releaseMeta} = this.props;
  162. const {release, deploys, sessions, reloading} = this.state;
  163. const project = release?.projects.find(p => p.id === selection.projects[0]);
  164. const releaseBounds = getReleaseBounds(release);
  165. if (!project || !release) {
  166. if (reloading) {
  167. return <LoadingIndicator />;
  168. }
  169. return null;
  170. }
  171. return (
  172. <NoProjectMessage organization={organization}>
  173. <StyledPageContent>
  174. <ReleaseHeader
  175. location={location}
  176. organization={organization}
  177. release={release}
  178. project={project}
  179. releaseMeta={releaseMeta}
  180. refetchData={this.fetchData}
  181. />
  182. <ReleaseContext.Provider
  183. value={{
  184. release,
  185. project,
  186. deploys,
  187. releaseMeta,
  188. refetchData: this.fetchData,
  189. hasHealthData:
  190. getCount(sessions?.groups, SessionFieldWithOperation.SESSIONS) > 0,
  191. releaseBounds,
  192. }}
  193. >
  194. {this.props.children}
  195. </ReleaseContext.Provider>
  196. </StyledPageContent>
  197. </NoProjectMessage>
  198. );
  199. }
  200. }
  201. type ReleasesDetailContainerProps = Omit<Props, 'releaseMeta'>;
  202. type ReleasesDetailContainerState = {
  203. releaseMeta: ReleaseMeta | null;
  204. } & AsyncComponent['state'];
  205. class ReleasesDetailContainer extends AsyncComponent<
  206. ReleasesDetailContainerProps,
  207. ReleasesDetailContainerState
  208. > {
  209. shouldReload = true;
  210. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  211. const {organization, params} = this.props;
  212. // fetch projects this release belongs to
  213. return [
  214. [
  215. 'releaseMeta',
  216. `/organizations/${organization.slug}/releases/${encodeURIComponent(
  217. params.release
  218. )}/meta/`,
  219. ],
  220. ];
  221. }
  222. componentDidMount() {
  223. this.removeGlobalDateTimeFromUrl();
  224. }
  225. componentDidUpdate(
  226. prevProps: ReleasesDetailContainerProps,
  227. prevState: ReleasesDetailContainerState
  228. ) {
  229. const {organization, params} = this.props;
  230. this.removeGlobalDateTimeFromUrl();
  231. if (
  232. prevProps.params.release !== params.release ||
  233. prevProps.organization.slug !== organization.slug
  234. ) {
  235. super.componentDidUpdate(prevProps, prevState);
  236. }
  237. }
  238. removeGlobalDateTimeFromUrl() {
  239. const {router, location} = this.props;
  240. const {start, end, statsPeriod, utc, ...restQuery} = location.query;
  241. if (start || end || statsPeriod || utc) {
  242. router.replace({
  243. ...location,
  244. query: restQuery,
  245. });
  246. }
  247. }
  248. renderError(...args) {
  249. const has404Errors = Object.values(this.state.errors).find(e => e?.status === 404);
  250. if (has404Errors) {
  251. // This catches a 404 coming from the release endpoint and displays a custom error message.
  252. return (
  253. <PageContent>
  254. <Alert type="error" showIcon>
  255. {t('This release could not be found.')}
  256. </Alert>
  257. </PageContent>
  258. );
  259. }
  260. return super.renderError(...args);
  261. }
  262. isProjectMissingInUrl() {
  263. const projectId = this.props.location.query.project;
  264. return !projectId || typeof projectId !== 'string';
  265. }
  266. renderLoading() {
  267. return (
  268. <PageContent>
  269. <LoadingIndicator />
  270. </PageContent>
  271. );
  272. }
  273. renderProjectsFooterMessage() {
  274. return (
  275. <ProjectsFooterMessage>
  276. <IconInfo size="xs" /> {t('Only projects with this release are visible.')}
  277. </ProjectsFooterMessage>
  278. );
  279. }
  280. renderBody() {
  281. const {organization, params, router} = this.props;
  282. const {releaseMeta} = this.state;
  283. if (!releaseMeta) {
  284. return null;
  285. }
  286. const {projects} = releaseMeta;
  287. if (this.isProjectMissingInUrl()) {
  288. return (
  289. <PickProjectToContinue
  290. projects={projects.map(({id, slug}) => ({
  291. id: String(id),
  292. slug,
  293. }))}
  294. router={router}
  295. nextPath={{
  296. pathname: `/organizations/${organization.slug}/releases/${encodeURIComponent(
  297. params.release
  298. )}/`,
  299. }}
  300. noProjectRedirectPath={`/organizations/${organization.slug}/releases/`}
  301. />
  302. );
  303. }
  304. return (
  305. <PageFiltersContainer
  306. shouldForceProject={projects.length === 1}
  307. forceProject={
  308. projects.length === 1 ? {...projects[0], id: String(projects[0].id)} : undefined
  309. }
  310. specificProjectSlugs={projects.map(p => p.slug)}
  311. >
  312. <ReleasesDetail {...this.props} releaseMeta={releaseMeta} />
  313. </PageFiltersContainer>
  314. );
  315. }
  316. }
  317. const StyledPageContent = styled(PageContent)`
  318. padding: 0;
  319. `;
  320. const ProjectsFooterMessage = styled('div')`
  321. display: grid;
  322. align-items: center;
  323. grid-template-columns: min-content 1fr;
  324. gap: ${space(1)};
  325. `;
  326. export {ReleaseContext, ReleasesDetailContainer};
  327. export default withPageFilters(withOrganization(ReleasesDetailContainer));