index.tsx 8.9 KB

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