projectEnvironments.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import {Component, Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import {Client} from 'sentry/api';
  6. import Access from 'sentry/components/acl/access';
  7. import {Button} from 'sentry/components/button';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import ListLink from 'sentry/components/links/listLink';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import NavTabs from 'sentry/components/navTabs';
  12. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import {ALL_ENVIRONMENTS_KEY} from 'sentry/constants';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {Environment, Organization, Project} from 'sentry/types';
  18. import {getDisplayName, getUrlRoutingName} from 'sentry/utils/environment';
  19. import recreateRoute from 'sentry/utils/recreateRoute';
  20. import withApi from 'sentry/utils/withApi';
  21. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  22. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  23. type Props = {
  24. api: Client;
  25. organization: Organization;
  26. project: Project;
  27. } & RouteComponentProps<{projectId: string}, {}>;
  28. type State = {
  29. environments: null | Environment[];
  30. isLoading: boolean;
  31. };
  32. class ProjectEnvironments extends Component<Props, State> {
  33. state: State = {
  34. environments: null,
  35. isLoading: true,
  36. };
  37. componentDidMount() {
  38. this.fetchData();
  39. }
  40. componentDidUpdate(prevProps: Props) {
  41. if (
  42. this.props.location.pathname.endsWith('hidden/') !==
  43. prevProps.location.pathname.endsWith('hidden/')
  44. ) {
  45. this.fetchData();
  46. }
  47. }
  48. fetchData() {
  49. const isHidden = this.props.location.pathname.endsWith('hidden/');
  50. if (!this.state.isLoading) {
  51. this.setState({isLoading: true});
  52. }
  53. const {organization} = this.props;
  54. const {projectId} = this.props.params;
  55. this.props.api.request(`/projects/${organization.slug}/${projectId}/environments/`, {
  56. query: {
  57. visibility: isHidden ? 'hidden' : 'visible',
  58. },
  59. success: environments => {
  60. this.setState({environments, isLoading: false});
  61. },
  62. });
  63. }
  64. // Toggle visibility of environment
  65. toggleEnv = (env: Environment, shouldHide: boolean) => {
  66. const {organization} = this.props;
  67. const {projectId} = this.props.params;
  68. this.props.api.request(
  69. `/projects/${organization.slug}/${projectId}/environments/${getUrlRoutingName(
  70. env
  71. )}/`,
  72. {
  73. method: 'PUT',
  74. data: {
  75. name: env.name,
  76. isHidden: shouldHide,
  77. },
  78. success: () => {
  79. addSuccessMessage(
  80. tct('Updated [environment]', {
  81. environment: getDisplayName(env),
  82. })
  83. );
  84. },
  85. error: () => {
  86. addErrorMessage(
  87. tct('Unable to update [environment]', {
  88. environment: getDisplayName(env),
  89. })
  90. );
  91. },
  92. complete: this.fetchData.bind(this),
  93. }
  94. );
  95. };
  96. renderEmpty() {
  97. const isHidden = this.props.location.pathname.endsWith('hidden/');
  98. const message = isHidden
  99. ? t("You don't have any hidden environments.")
  100. : t("You don't have any environments yet.");
  101. return <EmptyMessage>{message}</EmptyMessage>;
  102. }
  103. /**
  104. * Renders rows for "system" environments:
  105. * - "All Environments"
  106. * - "No Environment"
  107. *
  108. */
  109. renderAllEnvironmentsSystemRow() {
  110. // Not available in "Hidden" tab
  111. const isHidden = this.props.location.pathname.endsWith('hidden/');
  112. if (isHidden) {
  113. return null;
  114. }
  115. const {project} = this.props;
  116. return (
  117. <EnvironmentRow
  118. project={project}
  119. name={ALL_ENVIRONMENTS_KEY}
  120. environment={{
  121. id: ALL_ENVIRONMENTS_KEY,
  122. name: ALL_ENVIRONMENTS_KEY,
  123. displayName: ALL_ENVIRONMENTS_KEY,
  124. }}
  125. isSystemRow
  126. />
  127. );
  128. }
  129. renderEnvironmentList(envs: Environment[]) {
  130. const {project} = this.props;
  131. const isHidden = this.props.location.pathname.endsWith('hidden/');
  132. const buttonText = isHidden ? t('Show') : t('Hide');
  133. return (
  134. <Fragment>
  135. {this.renderAllEnvironmentsSystemRow()}
  136. {envs.map(env => (
  137. <EnvironmentRow
  138. project={project}
  139. key={env.id}
  140. name={env.name}
  141. environment={env}
  142. isHidden={isHidden}
  143. onHide={this.toggleEnv}
  144. actionText={buttonText}
  145. shouldShowAction
  146. />
  147. ))}
  148. </Fragment>
  149. );
  150. }
  151. renderBody() {
  152. const {environments, isLoading} = this.state;
  153. if (isLoading) {
  154. return <LoadingIndicator />;
  155. }
  156. return (
  157. <PanelBody>
  158. {environments?.length
  159. ? this.renderEnvironmentList(environments)
  160. : this.renderEmpty()}
  161. </PanelBody>
  162. );
  163. }
  164. render() {
  165. const {routes, params, location, project} = this.props;
  166. const isHidden = location.pathname.endsWith('hidden/');
  167. const baseUrl = recreateRoute('', {routes, params, stepBack: -1});
  168. return (
  169. <div>
  170. <SentryDocumentTitle title={t('Environments')} projectSlug={params.projectId} />
  171. <SettingsPageHeader
  172. title={t('Manage Environments')}
  173. tabs={
  174. <NavTabs underlined>
  175. <ListLink to={baseUrl} index isActive={() => !isHidden}>
  176. {t('Environments')}
  177. </ListLink>
  178. <ListLink to={`${baseUrl}hidden/`} index isActive={() => isHidden}>
  179. {t('Hidden')}
  180. </ListLink>
  181. </NavTabs>
  182. }
  183. />
  184. <PermissionAlert project={project} />
  185. <Panel>
  186. <PanelHeader>{isHidden ? t('Hidden') : t('Active Environments')}</PanelHeader>
  187. {this.renderBody()}
  188. </Panel>
  189. </div>
  190. );
  191. }
  192. }
  193. type RowProps = {
  194. environment: Environment;
  195. name: string;
  196. project: Project;
  197. actionText?: string;
  198. isHidden?: boolean;
  199. isSystemRow?: boolean;
  200. onHide?: (env: Environment, isHidden: boolean) => void;
  201. shouldShowAction?: boolean;
  202. };
  203. function EnvironmentRow({
  204. project,
  205. environment,
  206. name,
  207. onHide,
  208. shouldShowAction = false,
  209. isSystemRow = false,
  210. isHidden = false,
  211. actionText = '',
  212. }: RowProps) {
  213. return (
  214. <EnvironmentItem>
  215. <Name>{isSystemRow ? t('All Environments') : name}</Name>
  216. <Access access={['project:write']} project={project}>
  217. {({hasAccess}) => (
  218. <Fragment>
  219. {shouldShowAction && onHide && (
  220. <EnvironmentButton
  221. size="xs"
  222. disabled={!hasAccess}
  223. onClick={() => onHide(environment, !isHidden)}
  224. >
  225. {actionText}
  226. </EnvironmentButton>
  227. )}
  228. </Fragment>
  229. )}
  230. </Access>
  231. </EnvironmentItem>
  232. );
  233. }
  234. const EnvironmentItem = styled(PanelItem)`
  235. align-items: center;
  236. justify-content: space-between;
  237. `;
  238. const Name = styled('div')`
  239. display: flex;
  240. align-items: center;
  241. `;
  242. const EnvironmentButton = styled(Button)`
  243. margin-left: ${space(0.5)};
  244. `;
  245. export {ProjectEnvironments};
  246. export default withApi(ProjectEnvironments);