projectEnvironments.tsx 7.5 KB

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