actionTargetSelector.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import SelectControl from 'sentry/components/forms/controls/selectControl';
  2. import Input from 'sentry/components/input';
  3. import SelectMembers from 'sentry/components/selectMembers';
  4. import TeamSelector from 'sentry/components/teamSelector';
  5. import {Organization, Project, SelectValue} from 'sentry/types';
  6. import {
  7. Action,
  8. ActionType,
  9. MetricActionTemplate,
  10. TargetType,
  11. } from 'sentry/views/alerts/rules/metric/types';
  12. const getPlaceholderForType = (type: ActionType) => {
  13. switch (type) {
  14. case ActionType.SLACK:
  15. return '@username or #channel';
  16. case ActionType.MSTEAMS:
  17. // no prefixes for msteams
  18. return 'username or channel';
  19. case ActionType.DISCORD:
  20. return 'Discord channel ID';
  21. case ActionType.PAGERDUTY:
  22. return 'service';
  23. case ActionType.OPSGENIE:
  24. return 'team';
  25. default:
  26. throw Error('Not implemented');
  27. }
  28. };
  29. type Props = {
  30. action: Action;
  31. disabled: boolean;
  32. loading: boolean;
  33. onChange: (value: string) => void;
  34. organization: Organization;
  35. availableAction?: MetricActionTemplate;
  36. project?: Project;
  37. };
  38. export default function ActionTargetSelector(props: Props) {
  39. const {action, availableAction, disabled, loading, onChange, organization, project} =
  40. props;
  41. const handleChangeTargetIdentifier = (value: SelectValue<string>) => {
  42. onChange(value.value);
  43. };
  44. const handleChangeSpecificTargetIdentifier = (
  45. e: React.ChangeEvent<HTMLInputElement>
  46. ) => {
  47. onChange(e.target.value);
  48. };
  49. switch (action.targetType) {
  50. case TargetType.TEAM:
  51. case TargetType.USER:
  52. const isTeam = action.targetType === TargetType.TEAM;
  53. return isTeam ? (
  54. <TeamSelector
  55. disabled={disabled}
  56. key="team"
  57. project={project}
  58. value={action.targetIdentifier}
  59. onChange={handleChangeTargetIdentifier}
  60. useId
  61. />
  62. ) : (
  63. <SelectMembers
  64. disabled={disabled}
  65. key="member"
  66. project={project}
  67. organization={organization}
  68. value={action.targetIdentifier}
  69. onChange={handleChangeTargetIdentifier}
  70. />
  71. );
  72. case TargetType.SPECIFIC:
  73. return availableAction?.options ? (
  74. <SelectControl
  75. isDisabled={disabled || loading}
  76. value={action.targetIdentifier}
  77. options={availableAction.options}
  78. onChange={handleChangeTargetIdentifier}
  79. />
  80. ) : (
  81. <Input
  82. type="text"
  83. autoComplete="off"
  84. disabled={disabled}
  85. required={action.type === 'discord'} // Only required for discord channel ID
  86. key={action.type}
  87. value={action.targetIdentifier || ''}
  88. onChange={handleChangeSpecificTargetIdentifier}
  89. placeholder={getPlaceholderForType(action.type)}
  90. />
  91. );
  92. default:
  93. return null;
  94. }
  95. }