123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- import {createContext} from 'react';
- import {RouteComponentProps} from 'react-router';
- import styled from '@emotion/styled';
- import pick from 'lodash/pick';
- import Alert from 'app/components/alert';
- import AsyncComponent from 'app/components/asyncComponent';
- import LoadingIndicator from 'app/components/loadingIndicator';
- import NoProjectMessage from 'app/components/noProjectMessage';
- import GlobalSelectionHeader from 'app/components/organizations/globalSelectionHeader';
- import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
- import PickProjectToContinue from 'app/components/pickProjectToContinue';
- import {URL_PARAM} from 'app/constants/globalSelectionHeader';
- import {IconInfo, IconWarning} from 'app/icons';
- import {t} from 'app/locale';
- import {PageContent} from 'app/styles/organization';
- import space from 'app/styles/space';
- import {
- Deploy,
- GlobalSelection,
- Organization,
- ReleaseMeta,
- ReleaseProject,
- ReleaseWithHealth,
- SessionApiResponse,
- SessionField,
- } from 'app/types';
- import {formatVersion} from 'app/utils/formatters';
- import routeTitleGen from 'app/utils/routeTitle';
- import {getCount} from 'app/utils/sessions';
- import withGlobalSelection from 'app/utils/withGlobalSelection';
- import withOrganization from 'app/utils/withOrganization';
- import AsyncView from 'app/views/asyncView';
- import {getReleaseBounds, ReleaseBounds} from '../utils';
- import ReleaseHeader from './releaseHeader';
- type ReleaseContextType = {
- release: ReleaseWithHealth;
- project: Required<ReleaseProject>;
- deploys: Deploy[];
- releaseMeta: ReleaseMeta;
- refetchData: () => void;
- hasHealthData: boolean;
- releaseBounds: ReleaseBounds;
- };
- const ReleaseContext = createContext<ReleaseContextType>({} as ReleaseContextType);
- type RouteParams = {
- orgId: string;
- release: string;
- };
- type Props = RouteComponentProps<RouteParams, {}> & {
- organization: Organization;
- selection: GlobalSelection;
- releaseMeta: ReleaseMeta;
- };
- type State = {
- release: ReleaseWithHealth;
- deploys: Deploy[];
- sessions: SessionApiResponse | null;
- } & AsyncView['state'];
- class ReleasesDetail extends AsyncView<Props, State> {
- shouldReload = true;
- getTitle() {
- const {params, organization, selection} = this.props;
- const {release} = this.state;
- // The release details page will always have only one project selected
- const project = release?.projects.find(p => p.id === selection.projects[0]);
- return routeTitleGen(
- t('Release %s', formatVersion(params.release)),
- organization.slug,
- false,
- project?.slug
- );
- }
- getDefaultState() {
- return {
- ...super.getDefaultState(),
- deploys: [],
- sessions: null,
- };
- }
- getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
- const {organization, location, params, releaseMeta} = this.props;
- const basePath = `/organizations/${organization.slug}/releases/${encodeURIComponent(
- params.release
- )}/`;
- const endpoints: ReturnType<AsyncView['getEndpoints']> = [
- [
- 'release',
- basePath,
- {
- query: {
- adoptionStages: 1,
- ...getParams(pick(location.query, [...Object.values(URL_PARAM)])),
- },
- },
- ],
- ];
- if (releaseMeta.deployCount > 0) {
- endpoints.push(['deploys', `${basePath}deploys/`]);
- }
- // Used to figure out if the release has any health data
- endpoints.push([
- 'sessions',
- `/organizations/${organization.slug}/sessions/`,
- {
- query: {
- project: location.query.project,
- environment: location.query.environment ?? [],
- query: `release:"${params.release}"`,
- field: 'sum(session)',
- statsPeriod: '90d',
- interval: '1d',
- },
- },
- ]);
- return endpoints;
- }
- renderError(...args) {
- const possiblyWrongProject = Object.values(this.state.errors).find(
- e => e?.status === 404 || e?.status === 403
- );
- if (possiblyWrongProject) {
- return (
- <PageContent>
- <Alert type="error" icon={<IconWarning />}>
- {t('This release may not be in your selected project.')}
- </Alert>
- </PageContent>
- );
- }
- return super.renderError(...args);
- }
- renderLoading() {
- return (
- <PageContent>
- <LoadingIndicator />
- </PageContent>
- );
- }
- renderBody() {
- const {organization, location, selection, releaseMeta} = this.props;
- const {release, deploys, sessions, reloading} = this.state;
- const project = release?.projects.find(p => p.id === selection.projects[0]);
- const releaseBounds = getReleaseBounds(release);
- if (!project || !release) {
- if (reloading) {
- return <LoadingIndicator />;
- }
- return null;
- }
- return (
- <NoProjectMessage organization={organization}>
- <StyledPageContent>
- <ReleaseHeader
- location={location}
- organization={organization}
- release={release}
- project={project}
- releaseMeta={releaseMeta}
- refetchData={this.fetchData}
- />
- <ReleaseContext.Provider
- value={{
- release,
- project,
- deploys,
- releaseMeta,
- refetchData: this.fetchData,
- hasHealthData: getCount(sessions?.groups, SessionField.SESSIONS) > 0,
- releaseBounds,
- }}
- >
- {this.props.children}
- </ReleaseContext.Provider>
- </StyledPageContent>
- </NoProjectMessage>
- );
- }
- }
- class ReleasesDetailContainer extends AsyncComponent<
- Omit<Props, 'releaseMeta'>,
- {releaseMeta: ReleaseMeta | null} & AsyncComponent['state']
- > {
- shouldReload = true;
- getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
- const {organization, params} = this.props;
- // fetch projects this release belongs to
- return [
- [
- 'releaseMeta',
- `/organizations/${organization.slug}/releases/${encodeURIComponent(
- params.release
- )}/meta/`,
- ],
- ];
- }
- componentDidMount() {
- this.removeGlobalDateTimeFromUrl();
- }
- componentDidUpdate(prevProps, prevContext: Record<string, any>) {
- super.componentDidUpdate(prevProps, prevContext);
- this.removeGlobalDateTimeFromUrl();
- }
- removeGlobalDateTimeFromUrl() {
- const {router, location} = this.props;
- const {start, end, statsPeriod, utc, ...restQuery} = location.query;
- if (start || end || statsPeriod || utc) {
- router.replace({
- ...location,
- query: restQuery,
- });
- }
- }
- renderError(...args) {
- const has404Errors = Object.values(this.state.errors).find(e => e?.status === 404);
- if (has404Errors) {
- // This catches a 404 coming from the release endpoint and displays a custom error message.
- return (
- <PageContent>
- <Alert type="error" icon={<IconWarning />}>
- {t('This release could not be found.')}
- </Alert>
- </PageContent>
- );
- }
- return super.renderError(...args);
- }
- isProjectMissingInUrl() {
- const projectId = this.props.location.query.project;
- return !projectId || typeof projectId !== 'string';
- }
- renderLoading() {
- return (
- <PageContent>
- <LoadingIndicator />
- </PageContent>
- );
- }
- renderProjectsFooterMessage() {
- return (
- <ProjectsFooterMessage>
- <IconInfo size="xs" /> {t('Only projects with this release are visible.')}
- </ProjectsFooterMessage>
- );
- }
- renderBody() {
- const {organization, params, router} = this.props;
- const {releaseMeta} = this.state;
- if (!releaseMeta) {
- return null;
- }
- const {projects} = releaseMeta;
- if (this.isProjectMissingInUrl()) {
- return (
- <PickProjectToContinue
- projects={projects.map(({id, slug}) => ({
- id: String(id),
- slug,
- }))}
- router={router}
- nextPath={{
- pathname: `/organizations/${organization.slug}/releases/${encodeURIComponent(
- params.release
- )}/`,
- }}
- noProjectRedirectPath={`/organizations/${organization.slug}/releases/`}
- />
- );
- }
- return (
- <GlobalSelectionHeader
- lockedMessageSubject={t('release')}
- shouldForceProject={projects.length === 1}
- forceProject={
- projects.length === 1 ? {...projects[0], id: String(projects[0].id)} : undefined
- }
- specificProjectSlugs={projects.map(p => p.slug)}
- disableMultipleProjectSelection
- showProjectSettingsLink
- projectsFooterMessage={this.renderProjectsFooterMessage()}
- showDateSelector={false}
- >
- <ReleasesDetail {...this.props} releaseMeta={releaseMeta} />
- </GlobalSelectionHeader>
- );
- }
- }
- const StyledPageContent = styled(PageContent)`
- padding: 0;
- `;
- const ProjectsFooterMessage = styled('div')`
- display: grid;
- align-items: center;
- grid-template-columns: min-content 1fr;
- grid-gap: ${space(1)};
- `;
- export {ReleaseContext, ReleasesDetailContainer};
- export default withGlobalSelection(withOrganization(ReleasesDetailContainer));
|