memberTeamFields.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import SelectControl from 'sentry/components/forms/controls/selectControl';
  4. import PanelItem from 'sentry/components/panels/panelItem';
  5. import SelectMembers from 'sentry/components/selectMembers';
  6. import TeamSelector from 'sentry/components/teamSelector';
  7. import {space} from 'sentry/styles/space';
  8. import type {IssueAlertRuleAction, IssueAlertRuleCondition} from 'sentry/types/alerts';
  9. import type {Organization} from 'sentry/types/organization';
  10. import type {Project} from 'sentry/types/project';
  11. interface OptionRecord {
  12. label: string;
  13. value: string;
  14. }
  15. type Props = {
  16. disabled: boolean;
  17. loading: boolean;
  18. memberValue: string | number;
  19. onChange: (action: IssueAlertRuleAction) => void;
  20. options: OptionRecord[];
  21. organization: Organization;
  22. project: Project;
  23. ruleData: IssueAlertRuleAction | IssueAlertRuleCondition;
  24. teamValue: string | number;
  25. };
  26. class MemberTeamFields extends Component<Props> {
  27. handleChange = (attribute: 'targetType' | 'targetIdentifier', newValue: string) => {
  28. const {onChange, ruleData} = this.props;
  29. if (newValue === ruleData[attribute]) {
  30. return;
  31. }
  32. const newData = {
  33. ...ruleData,
  34. [attribute]: newValue,
  35. };
  36. // TargetIdentifiers between the targetTypes are not unique, and may
  37. // wrongly map to something that has not been selected. E.g. A member and
  38. // project can both have the `targetIdentifier`, `'2'`. Hence we clear the
  39. // identifier.
  40. if (attribute === 'targetType') {
  41. newData.targetIdentifier = '';
  42. }
  43. onChange(newData);
  44. };
  45. handleChangeActorType = (optionRecord: OptionRecord) => {
  46. this.handleChange('targetType', optionRecord.value);
  47. };
  48. handleChangeActorId = (optionRecord: OptionRecord & {[key: string]: any}) => {
  49. this.handleChange('targetIdentifier', optionRecord.value);
  50. };
  51. render(): React.ReactElement {
  52. const {
  53. disabled,
  54. loading,
  55. project,
  56. organization,
  57. ruleData,
  58. memberValue,
  59. teamValue,
  60. options,
  61. } = this.props;
  62. const teamSelected = ruleData.targetType === teamValue;
  63. const memberSelected = ruleData.targetType === memberValue;
  64. const selectControlStyles = {
  65. control: provided => ({
  66. ...provided,
  67. minHeight: '28px',
  68. height: '28px',
  69. }),
  70. };
  71. return (
  72. <PanelItemGrid>
  73. <SelectWrapper>
  74. <SelectControl
  75. isClearable={false}
  76. isDisabled={disabled || loading}
  77. value={ruleData.targetType}
  78. styles={selectControlStyles}
  79. options={options}
  80. onChange={this.handleChangeActorType}
  81. />
  82. </SelectWrapper>
  83. {(teamSelected || memberSelected) && (
  84. <SelectWrapper>
  85. {teamSelected ? (
  86. <TeamSelector
  87. disabled={disabled}
  88. key={teamValue}
  89. project={project}
  90. // The value from the endpoint is of type `number`, `SelectMembers` require value to be of type `string`
  91. value={`${ruleData.targetIdentifier}`}
  92. styles={selectControlStyles}
  93. onChange={this.handleChangeActorId}
  94. useId
  95. />
  96. ) : memberSelected ? (
  97. <SelectMembers
  98. disabled={disabled}
  99. key={teamSelected ? teamValue : memberValue}
  100. organization={organization}
  101. // The value from the endpoint is of type `number`, `SelectMembers` require value to be of type `string`
  102. value={`${ruleData.targetIdentifier}`}
  103. styles={selectControlStyles}
  104. onChange={this.handleChangeActorId}
  105. />
  106. ) : null}
  107. </SelectWrapper>
  108. )}
  109. </PanelItemGrid>
  110. );
  111. }
  112. }
  113. const PanelItemGrid = styled(PanelItem)`
  114. display: flex;
  115. align-items: center;
  116. padding: 0;
  117. gap: ${space(2)};
  118. `;
  119. const SelectWrapper = styled('div')`
  120. width: 200px;
  121. `;
  122. export default MemberTeamFields;