projectEnvironments.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. } & RouteComponentProps<{projectId: string}, {}>;
  27. type State = {
  28. environments: null | Environment[];
  29. isLoading: boolean;
  30. project: null | Project;
  31. };
  32. class ProjectEnvironments extends Component<Props, State> {
  33. state: State = {
  34. project: null,
  35. environments: null,
  36. isLoading: true,
  37. };
  38. componentDidMount() {
  39. this.fetchData();
  40. }
  41. componentDidUpdate(prevProps: Props) {
  42. if (
  43. this.props.location.pathname.endsWith('hidden/') !==
  44. prevProps.location.pathname.endsWith('hidden/')
  45. ) {
  46. this.fetchData();
  47. }
  48. }
  49. fetchData() {
  50. const isHidden = this.props.location.pathname.endsWith('hidden/');
  51. if (!this.state.isLoading) {
  52. this.setState({isLoading: true});
  53. }
  54. const {organization} = this.props;
  55. const {projectId} = this.props.params;
  56. this.props.api.request(`/projects/${organization.slug}/${projectId}/environments/`, {
  57. query: {
  58. visibility: isHidden ? 'hidden' : 'visible',
  59. },
  60. success: environments => {
  61. this.setState({environments, isLoading: false});
  62. },
  63. });
  64. }
  65. fetchProjectDetails() {
  66. const {organization} = this.props;
  67. const {projectId} = this.props.params;
  68. this.props.api.request(`/projects/${organization.slug}/${projectId}/`, {
  69. success: project => {
  70. this.setState({project});
  71. },
  72. });
  73. }
  74. // Toggle visibility of environment
  75. toggleEnv = (env: Environment, shouldHide: boolean) => {
  76. const {organization} = this.props;
  77. const {projectId} = this.props.params;
  78. this.props.api.request(
  79. `/projects/${organization.slug}/${projectId}/environments/${getUrlRoutingName(
  80. env
  81. )}/`,
  82. {
  83. method: 'PUT',
  84. data: {
  85. name: env.name,
  86. isHidden: shouldHide,
  87. },
  88. success: () => {
  89. addSuccessMessage(
  90. tct('Updated [environment]', {
  91. environment: getDisplayName(env),
  92. })
  93. );
  94. },
  95. error: () => {
  96. addErrorMessage(
  97. tct('Unable to update [environment]', {
  98. environment: getDisplayName(env),
  99. })
  100. );
  101. },
  102. complete: this.fetchData.bind(this),
  103. }
  104. );
  105. };
  106. renderEmpty() {
  107. const isHidden = this.props.location.pathname.endsWith('hidden/');
  108. const message = isHidden
  109. ? t("You don't have any hidden environments.")
  110. : t("You don't have any environments yet.");
  111. return <EmptyMessage>{message}</EmptyMessage>;
  112. }
  113. /**
  114. * Renders rows for "system" environments:
  115. * - "All Environments"
  116. * - "No Environment"
  117. *
  118. */
  119. renderAllEnvironmentsSystemRow() {
  120. // Not available in "Hidden" tab
  121. const isHidden = this.props.location.pathname.endsWith('hidden/');
  122. if (isHidden) {
  123. return null;
  124. }
  125. return (
  126. <EnvironmentRow
  127. name={ALL_ENVIRONMENTS_KEY}
  128. environment={{
  129. id: ALL_ENVIRONMENTS_KEY,
  130. name: ALL_ENVIRONMENTS_KEY,
  131. displayName: ALL_ENVIRONMENTS_KEY,
  132. }}
  133. isSystemRow
  134. />
  135. );
  136. }
  137. renderEnvironmentList(envs: Environment[]) {
  138. const isHidden = this.props.location.pathname.endsWith('hidden/');
  139. const buttonText = isHidden ? t('Show') : t('Hide');
  140. return (
  141. <Fragment>
  142. {this.renderAllEnvironmentsSystemRow()}
  143. {envs.map(env => (
  144. <EnvironmentRow
  145. key={env.id}
  146. name={env.name}
  147. environment={env}
  148. isHidden={isHidden}
  149. onHide={this.toggleEnv}
  150. actionText={buttonText}
  151. shouldShowAction
  152. />
  153. ))}
  154. </Fragment>
  155. );
  156. }
  157. renderBody() {
  158. const {environments, isLoading} = this.state;
  159. if (isLoading) {
  160. return <LoadingIndicator />;
  161. }
  162. return (
  163. <PanelBody>
  164. {environments?.length
  165. ? this.renderEnvironmentList(environments)
  166. : this.renderEmpty()}
  167. </PanelBody>
  168. );
  169. }
  170. render() {
  171. const {routes, params, location} = this.props;
  172. const isHidden = location.pathname.endsWith('hidden/');
  173. const baseUrl = recreateRoute('', {routes, params, stepBack: -1});
  174. return (
  175. <div>
  176. <SentryDocumentTitle title={t('Environments')} projectSlug={params.projectId} />
  177. <SettingsPageHeader
  178. title={t('Manage Environments')}
  179. tabs={
  180. <NavTabs underlined>
  181. <ListLink to={baseUrl} index isActive={() => !isHidden}>
  182. {t('Environments')}
  183. </ListLink>
  184. <ListLink to={`${baseUrl}hidden/`} index isActive={() => isHidden}>
  185. {t('Hidden')}
  186. </ListLink>
  187. </NavTabs>
  188. }
  189. />
  190. <PermissionAlert />
  191. <Panel>
  192. <PanelHeader>{isHidden ? t('Hidden') : t('Active Environments')}</PanelHeader>
  193. {this.renderBody()}
  194. </Panel>
  195. </div>
  196. );
  197. }
  198. }
  199. type RowProps = {
  200. environment: Environment;
  201. name: string;
  202. actionText?: string;
  203. isHidden?: boolean;
  204. isSystemRow?: boolean;
  205. onHide?: (env: Environment, isHidden: boolean) => void;
  206. shouldShowAction?: boolean;
  207. };
  208. function EnvironmentRow({
  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']}>
  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);