builtInRepositories.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import styled from '@emotion/styled';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import type {Client} from 'sentry/api';
  4. import Access from 'sentry/components/acl/access';
  5. import SelectField from 'sentry/components/forms/fields/selectField';
  6. import Panel from 'sentry/components/panels/panel';
  7. import PanelBody from 'sentry/components/panels/panelBody';
  8. import PanelHeader from 'sentry/components/panels/panelHeader';
  9. import {t} from 'sentry/locale';
  10. import ProjectsStore from 'sentry/stores/projectsStore';
  11. import type {BuiltinSymbolSource} from 'sentry/types/debugFiles';
  12. import type {Organization} from 'sentry/types/organization';
  13. import type {Project} from 'sentry/types/project';
  14. const SECTION_TITLE = t('Built-in Repositories');
  15. type Props = {
  16. api: Client;
  17. builtinSymbolSourceOptions: BuiltinSymbolSource[];
  18. builtinSymbolSources: string[];
  19. organization: Organization;
  20. project: Project;
  21. };
  22. function BuiltInRepositories({
  23. api,
  24. organization,
  25. builtinSymbolSourceOptions,
  26. builtinSymbolSources,
  27. project,
  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}/${project.slug}/`,
  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. <Access access={['project:write']} project={project}>
  74. {({hasAccess}) => (
  75. <StyledSelectField
  76. disabledReason={
  77. !hasAccess
  78. ? t(
  79. 'You do not have permission to edit built-in repositories configurations.'
  80. )
  81. : undefined
  82. }
  83. disabled={!hasAccess}
  84. name="builtinSymbolSources"
  85. label={SECTION_TITLE}
  86. help={t(
  87. 'Configures which built-in repositories Sentry should use to resolve debug files.'
  88. )}
  89. placeholder={t('Select built-in repository')}
  90. value={validBuiltInSymbolSources}
  91. onChange={handleChange}
  92. options={builtinSymbolSourceOptions
  93. .filter(source => !source.hidden)
  94. .map(source => ({
  95. value: source.sentry_key,
  96. label: source.name,
  97. }))}
  98. getValue={value => (value === null ? [] : value)}
  99. flexibleControlStateSize
  100. multiple
  101. />
  102. )}
  103. </Access>
  104. </PanelBody>
  105. </Panel>
  106. );
  107. }
  108. export default BuiltInRepositories;
  109. const StyledSelectField = styled(SelectField)`
  110. ${p => p.disabled && `cursor: not-allowed`}
  111. `;