memberTeamFields.tsx 3.7 KB

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