index.tsx 9.1 KB

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