index.tsx 7.6 KB

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