index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import {useCallback, useContext, useEffect} from 'react';
  2. import {InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  6. import {openDebugFileSourceModal} from 'sentry/actionCreators/modal';
  7. import {Client} from 'sentry/api';
  8. import Access from 'sentry/components/acl/access';
  9. import Feature from 'sentry/components/acl/feature';
  10. import DropdownAutoComplete from 'sentry/components/dropdownAutoComplete';
  11. import DropdownButton from 'sentry/components/dropdownButton';
  12. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  13. import LoadingIndicator from 'sentry/components/loadingIndicator';
  14. import MenuItem from 'sentry/components/menuItem';
  15. import Panel from 'sentry/components/panels/panel';
  16. import PanelBody from 'sentry/components/panels/panelBody';
  17. import PanelHeader from 'sentry/components/panels/panelHeader';
  18. import AppStoreConnectContext from 'sentry/components/projects/appStoreConnectContext';
  19. import {Tooltip} from 'sentry/components/tooltip';
  20. import {t} from 'sentry/locale';
  21. import ProjectsStore from 'sentry/stores/projectsStore';
  22. import {Organization, Project} from 'sentry/types';
  23. import {CustomRepo, CustomRepoType} from 'sentry/types/debugFiles';
  24. import {defined} from 'sentry/utils';
  25. import {handleXhrErrorResponse} from 'sentry/utils/handleXhrErrorResponse';
  26. import Repository from './repository';
  27. import {dropDownItems, expandKeys, getRequestMessages} from './utils';
  28. const SECTION_TITLE = t('Custom Repositories');
  29. type Props = {
  30. api: Client;
  31. customRepositories: CustomRepo[];
  32. isLoading: boolean;
  33. location: Location;
  34. organization: Organization;
  35. project: Project;
  36. router: InjectedRouter;
  37. };
  38. function CustomRepositories({
  39. api,
  40. organization,
  41. customRepositories: repositories,
  42. project,
  43. router,
  44. location,
  45. isLoading,
  46. }: Props) {
  47. const appStoreConnectContext = useContext(AppStoreConnectContext);
  48. const orgSlug = organization.slug;
  49. const appStoreConnectSourcesQuantity = repositories.filter(
  50. repository => repository.type === CustomRepoType.APP_STORE_CONNECT
  51. ).length;
  52. const persistData = useCallback(
  53. ({
  54. updatedItems,
  55. updatedItem,
  56. index,
  57. refresh,
  58. }: {
  59. index?: number;
  60. refresh?: boolean;
  61. updatedItem?: CustomRepo;
  62. updatedItems?: CustomRepo[];
  63. }) => {
  64. let items = updatedItems ?? [];
  65. if (updatedItem && defined(index)) {
  66. items = [...repositories];
  67. items.splice(index, 1, updatedItem);
  68. }
  69. const {successMessage, errorMessage} = getRequestMessages(
  70. items.length,
  71. repositories.length
  72. );
  73. const symbolSources = JSON.stringify(items.map(expandKeys));
  74. const promise: Promise<any> = api.requestPromise(
  75. `/projects/${orgSlug}/${project.slug}/`,
  76. {
  77. method: 'PUT',
  78. data: {symbolSources},
  79. }
  80. );
  81. promise.catch(() => {
  82. addErrorMessage(errorMessage);
  83. });
  84. promise.then(result => {
  85. ProjectsStore.onUpdateSuccess(result);
  86. addSuccessMessage(successMessage);
  87. if (refresh) {
  88. window.location.reload();
  89. }
  90. });
  91. return promise;
  92. },
  93. [api, orgSlug, project.slug, repositories]
  94. );
  95. const handleCloseModal = useCallback(() => {
  96. router.push({
  97. ...location,
  98. query: {
  99. ...location.query,
  100. customRepository: undefined,
  101. },
  102. });
  103. }, [location, router]);
  104. const openDebugFileSourceDialog = useCallback(() => {
  105. const {customRepository} = location.query;
  106. if (!customRepository) {
  107. return;
  108. }
  109. const itemIndex = repositories.findIndex(
  110. repository => repository.id === customRepository
  111. );
  112. const item = repositories[itemIndex];
  113. if (!item) {
  114. return;
  115. }
  116. openDebugFileSourceModal({
  117. organization,
  118. sourceConfig: item,
  119. sourceType: item.type,
  120. appStoreConnectSourcesQuantity,
  121. appStoreConnectStatusData: appStoreConnectContext?.[item.id],
  122. onSave: updatedItem =>
  123. persistData({updatedItem: updatedItem as CustomRepo, index: itemIndex}),
  124. onClose: handleCloseModal,
  125. });
  126. }, [
  127. appStoreConnectContext,
  128. appStoreConnectSourcesQuantity,
  129. handleCloseModal,
  130. location.query,
  131. organization,
  132. persistData,
  133. repositories,
  134. ]);
  135. useEffect(() => {
  136. openDebugFileSourceDialog();
  137. }, [location.query, appStoreConnectContext, openDebugFileSourceDialog]);
  138. function handleAddRepository(repoType: CustomRepoType) {
  139. openDebugFileSourceModal({
  140. organization,
  141. appStoreConnectSourcesQuantity,
  142. sourceType: repoType,
  143. onSave: updatedData =>
  144. persistData({updatedItems: [...repositories, updatedData] as CustomRepo[]}),
  145. });
  146. }
  147. function handleDeleteRepository(repoId: CustomRepo['id']) {
  148. const newRepositories = [...repositories];
  149. const index = newRepositories.findIndex(item => item.id === repoId);
  150. newRepositories.splice(index, 1);
  151. persistData({
  152. updatedItems: newRepositories as CustomRepo[],
  153. refresh: repositories[index].type === CustomRepoType.APP_STORE_CONNECT,
  154. });
  155. }
  156. function handleEditRepository(repoId: CustomRepo['id']) {
  157. router.push({
  158. ...location,
  159. query: {
  160. ...location.query,
  161. customRepository: repoId,
  162. },
  163. });
  164. }
  165. async function handleSyncRepositoryNow(repoId: CustomRepo['id']) {
  166. try {
  167. await api.requestPromise(
  168. `/projects/${orgSlug}/${project.slug}/appstoreconnect/${repoId}/refresh/`,
  169. {
  170. method: 'POST',
  171. }
  172. );
  173. addSuccessMessage(t('Repository sync started.'));
  174. } catch (error) {
  175. const errorMessage = t(
  176. 'Rate limit for refreshing repository exceeded. Try again in a few minutes.'
  177. );
  178. addErrorMessage(errorMessage);
  179. handleXhrErrorResponse(errorMessage, error);
  180. }
  181. }
  182. return (
  183. <Feature features={['custom-symbol-sources']} organization={organization}>
  184. {({hasFeature}) => (
  185. <Access access={['project:write']} project={project}>
  186. {({hasAccess}) => {
  187. const addRepositoryButtonDisabled = !hasAccess || isLoading;
  188. return (
  189. <Panel>
  190. <PanelHeader hasButtons>
  191. {SECTION_TITLE}
  192. <Tooltip
  193. title={
  194. !hasAccess
  195. ? t('You do not have permission to add custom repositories.')
  196. : undefined
  197. }
  198. >
  199. <DropdownAutoComplete
  200. alignMenu="right"
  201. disabled={addRepositoryButtonDisabled}
  202. onSelect={item => handleAddRepository(item.value)}
  203. items={dropDownItems.map(dropDownItem => ({
  204. ...dropDownItem,
  205. label: (
  206. <DropDownLabel
  207. aria-label={t(
  208. 'Open %s custom repository modal',
  209. dropDownItem.label
  210. )}
  211. >
  212. {dropDownItem.label}
  213. </DropDownLabel>
  214. ),
  215. }))}
  216. >
  217. {({isOpen}) => (
  218. <DropdownButton
  219. isOpen={isOpen}
  220. disabled={addRepositoryButtonDisabled}
  221. size="xs"
  222. aria-label={t('Add Repository')}
  223. >
  224. {t('Add Repository')}
  225. </DropdownButton>
  226. )}
  227. </DropdownAutoComplete>
  228. </Tooltip>
  229. </PanelHeader>
  230. <PanelBody>
  231. {isLoading ? (
  232. <LoadingIndicator />
  233. ) : !repositories.length ? (
  234. <EmptyStateWarning>
  235. <p>{t('No custom repositories configured')}</p>
  236. </EmptyStateWarning>
  237. ) : (
  238. repositories.map((repository, index) => (
  239. <Repository
  240. key={index}
  241. repository={
  242. repository.type === CustomRepoType.APP_STORE_CONNECT
  243. ? {
  244. ...repository,
  245. details: appStoreConnectContext?.[repository.id],
  246. }
  247. : repository
  248. }
  249. hasFeature={
  250. repository.type === CustomRepoType.APP_STORE_CONNECT
  251. ? hasFeature || appStoreConnectSourcesQuantity === 1
  252. : hasFeature
  253. }
  254. hasAccess={hasAccess}
  255. onDelete={handleDeleteRepository}
  256. onEdit={handleEditRepository}
  257. onSyncNow={handleSyncRepositoryNow}
  258. />
  259. ))
  260. )}
  261. </PanelBody>
  262. </Panel>
  263. );
  264. }}
  265. </Access>
  266. )}
  267. </Feature>
  268. );
  269. }
  270. export default CustomRepositories;
  271. const DropDownLabel = styled(MenuItem)`
  272. color: ${p => p.theme.textColor};
  273. font-size: ${p => p.theme.fontSizeMedium};
  274. font-weight: 400;
  275. text-transform: none;
  276. span {
  277. padding: 0;
  278. }
  279. `;