index.tsx 9.4 KB

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