repositoryRow.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {cancelDeleteRepository, deleteRepository} from 'app/actionCreators/integrations';
  4. import {openModal} from 'app/actionCreators/modal';
  5. import {Client} from 'app/api';
  6. import Access from 'app/components/acl/access';
  7. import Button from 'app/components/button';
  8. import Confirm from 'app/components/confirm';
  9. import ExternalLink from 'app/components/links/externalLink';
  10. import {PanelItem} from 'app/components/panels';
  11. import RepositoryEditForm from 'app/components/repositoryEditForm';
  12. import Tooltip from 'app/components/tooltip';
  13. import {IconDelete, IconEdit} from 'app/icons';
  14. import {t} from 'app/locale';
  15. import space from 'app/styles/space';
  16. import {Organization, Repository, RepositoryStatus} from 'app/types';
  17. import withOrganization from 'app/utils/withOrganization';
  18. type DefaultProps = {
  19. showProvider?: boolean;
  20. };
  21. type Props = DefaultProps & {
  22. organization: Organization;
  23. repository: Repository;
  24. api: Client;
  25. orgId: string;
  26. onRepositoryChange?: (data: {id: string; status: RepositoryStatus}) => void;
  27. };
  28. class RepositoryRow extends Component<Props> {
  29. static defaultProps: DefaultProps = {
  30. showProvider: false,
  31. };
  32. getStatusLabel(repo: Repository) {
  33. switch (repo.status) {
  34. case RepositoryStatus.PENDING_DELETION:
  35. return 'Deletion Queued';
  36. case RepositoryStatus.DELETION_IN_PROGRESS:
  37. return 'Deletion in Progress';
  38. case RepositoryStatus.DISABLED:
  39. return 'Disabled';
  40. case RepositoryStatus.HIDDEN:
  41. return 'Disabled';
  42. default:
  43. return null;
  44. }
  45. }
  46. cancelDelete = () => {
  47. const {api, orgId, repository, onRepositoryChange} = this.props;
  48. cancelDeleteRepository(api, orgId, repository.id).then(
  49. data => {
  50. if (onRepositoryChange) {
  51. onRepositoryChange(data);
  52. }
  53. },
  54. () => {}
  55. );
  56. };
  57. deleteRepo = () => {
  58. const {api, orgId, repository, onRepositoryChange} = this.props;
  59. deleteRepository(api, orgId, repository.id).then(
  60. data => {
  61. if (onRepositoryChange) {
  62. onRepositoryChange(data);
  63. }
  64. },
  65. () => {}
  66. );
  67. };
  68. handleEditRepo = (data: Repository) => {
  69. const {onRepositoryChange} = this.props;
  70. if (onRepositoryChange) {
  71. onRepositoryChange(data);
  72. }
  73. };
  74. get isActive() {
  75. return this.props.repository.status === RepositoryStatus.ACTIVE;
  76. }
  77. renderDeleteButton(hasAccess) {
  78. const {repository} = this.props;
  79. const isActive = this.isActive;
  80. return (
  81. <Tooltip
  82. title={t(
  83. 'You must be an organization owner, manager or admin to remove a repository.'
  84. )}
  85. disabled={hasAccess}
  86. >
  87. <Confirm
  88. disabled={
  89. !hasAccess || (!isActive && repository.status !== RepositoryStatus.DISABLED)
  90. }
  91. onConfirm={this.deleteRepo}
  92. message={t(
  93. 'Are you sure you want to remove this repository? All associated commit data will be removed in addition to the repository.'
  94. )}
  95. >
  96. <StyledButton
  97. size="xsmall"
  98. icon={<IconDelete size="xs" />}
  99. label={t('delete')}
  100. disabled={!hasAccess}
  101. />
  102. </Confirm>
  103. </Tooltip>
  104. );
  105. }
  106. openModal = () => {
  107. const {repository, orgId} = this.props;
  108. openModal(({Body, Header, closeModal}) => (
  109. <Fragment>
  110. <Header closeButton>{t('Edit Repository')}</Header>
  111. <Body>
  112. <RepositoryEditForm
  113. orgSlug={orgId}
  114. repository={repository}
  115. onSubmitSuccess={this.handleEditRepo}
  116. closeModal={closeModal}
  117. onCancel={closeModal}
  118. />
  119. </Body>
  120. </Fragment>
  121. ));
  122. };
  123. render() {
  124. const {repository, showProvider, organization} = this.props;
  125. const isActive = this.isActive;
  126. const isCustomRepo =
  127. organization.features.includes('integrations-custom-scm') &&
  128. repository.provider.id === 'integrations:custom_scm';
  129. return (
  130. <Access access={['org:integrations']}>
  131. {({hasAccess}) => (
  132. <StyledPanelItem status={repository.status}>
  133. <RepositoryTitleAndUrl>
  134. <RepositoryTitle>
  135. <strong>{repository.name}</strong>
  136. {!isActive && <small> &mdash; {this.getStatusLabel(repository)}</small>}
  137. {repository.status === RepositoryStatus.PENDING_DELETION && (
  138. <StyledButton
  139. size="xsmall"
  140. onClick={this.cancelDelete}
  141. disabled={!hasAccess}
  142. data-test-id="repo-cancel"
  143. >
  144. {t('Cancel')}
  145. </StyledButton>
  146. )}
  147. </RepositoryTitle>
  148. <div>
  149. {showProvider && <small>{repository.provider.name}</small>}
  150. {showProvider && repository.url && <span>&nbsp;&mdash;&nbsp;</span>}
  151. {repository.url && (
  152. <small>
  153. <ExternalLink href={repository.url}>
  154. {repository.url.replace('https://', '')}
  155. </ExternalLink>
  156. </small>
  157. )}
  158. </div>
  159. </RepositoryTitleAndUrl>
  160. {isCustomRepo ? (
  161. <EditAndDelete>
  162. <StyledButton
  163. size="xsmall"
  164. icon={<IconEdit size="xs" />}
  165. label={t('edit')}
  166. disabled={
  167. !hasAccess ||
  168. (!isActive && repository.status !== RepositoryStatus.DISABLED)
  169. }
  170. onClick={() => this.openModal()}
  171. />
  172. {this.renderDeleteButton(hasAccess)}
  173. </EditAndDelete>
  174. ) : (
  175. this.renderDeleteButton(hasAccess)
  176. )}
  177. </StyledPanelItem>
  178. )}
  179. </Access>
  180. );
  181. }
  182. }
  183. const StyledPanelItem = styled(PanelItem)<{status: RepositoryStatus}>`
  184. /* shorter top padding because of title lineheight */
  185. padding: ${space(1)} ${space(2)} ${space(2)};
  186. justify-content: space-between;
  187. align-items: center;
  188. flex: 1;
  189. ${p =>
  190. p.status === RepositoryStatus.DISABLED &&
  191. `
  192. filter: grayscale(1);
  193. opacity: 0.4;
  194. `};
  195. &:last-child {
  196. border-bottom: none;
  197. }
  198. `;
  199. const StyledButton = styled(Button)`
  200. margin-left: ${space(1)};
  201. `;
  202. const RepositoryTitleAndUrl = styled('div')`
  203. display: flex;
  204. flex-direction: column;
  205. `;
  206. const EditAndDelete = styled('div')`
  207. display: flex;
  208. margin-left: ${space(1)};
  209. `;
  210. const RepositoryTitle = styled('div')`
  211. margin-bottom: ${space(1)};
  212. /* accommodate cancel button height */
  213. line-height: 26px;
  214. `;
  215. export default withOrganization(RepositoryRow);