index.tsx 9.6 KB

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