projectEnvironments.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 ListLink from 'sentry/components/links/listLink';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import NavTabs from 'sentry/components/navTabs';
  11. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  12. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  13. import {ALL_ENVIRONMENTS_KEY} from 'sentry/constants';
  14. import {t, tct} from 'sentry/locale';
  15. import space from 'sentry/styles/space';
  16. import {Environment, Project} from 'sentry/types';
  17. import {getDisplayName, getUrlRoutingName} from 'sentry/utils/environment';
  18. import recreateRoute from 'sentry/utils/recreateRoute';
  19. import withApi from 'sentry/utils/withApi';
  20. import EmptyMessage from 'sentry/views/settings/components/emptyMessage';
  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. } & RouteComponentProps<{orgId: string; projectId: string}, {}>;
  26. type State = {
  27. environments: null | Environment[];
  28. isLoading: boolean;
  29. project: null | Project;
  30. };
  31. class ProjectEnvironments extends Component<Props, State> {
  32. state: State = {
  33. project: null,
  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 {orgId, projectId} = this.props.params;
  54. this.props.api.request(`/projects/${orgId}/${projectId}/environments/`, {
  55. query: {
  56. visibility: isHidden ? 'hidden' : 'visible',
  57. },
  58. success: environments => {
  59. this.setState({environments, isLoading: false});
  60. },
  61. });
  62. }
  63. fetchProjectDetails() {
  64. const {orgId, projectId} = this.props.params;
  65. this.props.api.request(`/projects/${orgId}/${projectId}/`, {
  66. success: project => {
  67. this.setState({project});
  68. },
  69. });
  70. }
  71. // Toggle visibility of environment
  72. toggleEnv = (env: Environment, shouldHide: boolean) => {
  73. const {orgId, projectId} = this.props.params;
  74. this.props.api.request(
  75. `/projects/${orgId}/${projectId}/environments/${getUrlRoutingName(env)}/`,
  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. return (
  120. <EnvironmentRow
  121. name={ALL_ENVIRONMENTS_KEY}
  122. environment={{
  123. id: ALL_ENVIRONMENTS_KEY,
  124. name: ALL_ENVIRONMENTS_KEY,
  125. displayName: ALL_ENVIRONMENTS_KEY,
  126. }}
  127. isSystemRow
  128. />
  129. );
  130. }
  131. renderEnvironmentList(envs: Environment[]) {
  132. const isHidden = this.props.location.pathname.endsWith('hidden/');
  133. const buttonText = isHidden ? t('Show') : t('Hide');
  134. return (
  135. <Fragment>
  136. {this.renderAllEnvironmentsSystemRow()}
  137. {envs.map(env => (
  138. <EnvironmentRow
  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} = 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 />
  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. actionText?: string;
  197. isHidden?: boolean;
  198. isSystemRow?: boolean;
  199. onHide?: (env: Environment, isHidden: boolean) => void;
  200. shouldShowAction?: boolean;
  201. };
  202. function EnvironmentRow({
  203. environment,
  204. name,
  205. onHide,
  206. shouldShowAction = false,
  207. isSystemRow = false,
  208. isHidden = false,
  209. actionText = '',
  210. }: RowProps) {
  211. return (
  212. <EnvironmentItem>
  213. <Name>{isSystemRow ? t('All Environments') : name}</Name>
  214. <Access access={['project:write']}>
  215. {({hasAccess}) => (
  216. <Fragment>
  217. {shouldShowAction && onHide && (
  218. <EnvironmentButton
  219. size="xs"
  220. disabled={!hasAccess}
  221. onClick={() => onHide(environment, !isHidden)}
  222. >
  223. {actionText}
  224. </EnvironmentButton>
  225. )}
  226. </Fragment>
  227. )}
  228. </Access>
  229. </EnvironmentItem>
  230. );
  231. }
  232. const EnvironmentItem = styled(PanelItem)`
  233. align-items: center;
  234. justify-content: space-between;
  235. `;
  236. const Name = styled('div')`
  237. display: flex;
  238. align-items: center;
  239. `;
  240. const EnvironmentButton = styled(Button)`
  241. margin-left: ${space(0.5)};
  242. `;
  243. export {ProjectEnvironments};
  244. export default withApi(ProjectEnvironments);