builtInRepositories.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import styled from '@emotion/styled';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {Client} from 'sentry/api';
  4. import Access from 'sentry/components/acl/access';
  5. import SelectField from 'sentry/components/forms/fields/selectField';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  8. import {t} from 'sentry/locale';
  9. import ProjectsStore from 'sentry/stores/projectsStore';
  10. import {Organization, Project} from 'sentry/types';
  11. import {BuiltinSymbolSource} from 'sentry/types/debugFiles';
  12. const SECTION_TITLE = t('Built-in Repositories');
  13. type Props = {
  14. api: Client;
  15. builtinSymbolSourceOptions: BuiltinSymbolSource[];
  16. builtinSymbolSources: string[];
  17. isLoading: boolean;
  18. organization: Organization;
  19. projSlug: Project['slug'];
  20. };
  21. function BuiltInRepositories({
  22. api,
  23. organization,
  24. builtinSymbolSourceOptions,
  25. builtinSymbolSources,
  26. projSlug,
  27. isLoading,
  28. }: Props) {
  29. // If the project details object has an unknown built-in source, this will be filtered here.
  30. // This prevents the UI from showing the wrong feedback message when updating the field
  31. const validBuiltInSymbolSources = builtinSymbolSources.filter(builtinSymbolSource =>
  32. builtinSymbolSourceOptions.find(({sentry_key}) => sentry_key === builtinSymbolSource)
  33. );
  34. function getRequestMessages(builtinSymbolSourcesQuantity: number) {
  35. if (builtinSymbolSourcesQuantity === 0) {
  36. return {
  37. errorMessage: t('This field requires at least one built-in repository'),
  38. };
  39. }
  40. if (builtinSymbolSourcesQuantity > validBuiltInSymbolSources.length) {
  41. return {
  42. successMessage: t('Successfully added built-in repository'),
  43. errorMessage: t('An error occurred while adding new built-in repository'),
  44. };
  45. }
  46. return {
  47. successMessage: t('Successfully removed built-in repository'),
  48. errorMessage: t('An error occurred while removing built-in repository'),
  49. };
  50. }
  51. async function handleChange(value: null | string[]) {
  52. const {successMessage, errorMessage} = getRequestMessages((value ?? []).length);
  53. try {
  54. const updatedProjectDetails: Project = await api.requestPromise(
  55. `/projects/${organization.slug}/${projSlug}/`,
  56. {
  57. method: 'PUT',
  58. data: {
  59. builtinSymbolSources: value,
  60. },
  61. }
  62. );
  63. ProjectsStore.onUpdateSuccess(updatedProjectDetails);
  64. addSuccessMessage(successMessage);
  65. } catch {
  66. addErrorMessage(errorMessage);
  67. }
  68. }
  69. return (
  70. <Panel>
  71. <PanelHeader>{SECTION_TITLE}</PanelHeader>
  72. <PanelBody>
  73. {isLoading ? (
  74. <LoadingIndicator />
  75. ) : (
  76. <Access access={['project:write']}>
  77. {({hasAccess}) => (
  78. <StyledSelectField
  79. disabledReason={
  80. !hasAccess
  81. ? t(
  82. 'You do not have permission to edit built-in repositories configurations.'
  83. )
  84. : undefined
  85. }
  86. disabled={!hasAccess}
  87. name="builtinSymbolSources"
  88. label={SECTION_TITLE}
  89. help={t(
  90. 'Configures which built-in repositories Sentry should use to resolve debug files.'
  91. )}
  92. placeholder={t('Select built-in repository')}
  93. value={validBuiltInSymbolSources}
  94. onChange={handleChange}
  95. options={builtinSymbolSourceOptions
  96. .filter(source => !source.hidden)
  97. .map(source => ({
  98. value: source.sentry_key,
  99. label: source.name,
  100. }))}
  101. getValue={value => (value === null ? [] : value)}
  102. flexibleControlStateSize
  103. multiple
  104. />
  105. )}
  106. </Access>
  107. )}
  108. </PanelBody>
  109. </Panel>
  110. );
  111. }
  112. export default BuiltInRepositories;
  113. const StyledSelectField = styled(SelectField)`
  114. ${p => p.disabled && `cursor: not-allowed`}
  115. `;