actionTargetSelector.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.PAGERDUTY:
  20. return 'service';
  21. default:
  22. throw Error('Not implemented');
  23. }
  24. };
  25. type Props = {
  26. action: Action;
  27. disabled: boolean;
  28. loading: boolean;
  29. onChange: (value: string) => void;
  30. organization: Organization;
  31. availableAction?: MetricActionTemplate;
  32. project?: Project;
  33. };
  34. export default function ActionTargetSelector(props: Props) {
  35. const {action, availableAction, disabled, loading, onChange, organization, project} =
  36. props;
  37. const handleChangeTargetIdentifier = (value: SelectValue<string>) => {
  38. onChange(value.value);
  39. };
  40. const handleChangeSpecificTargetIdentifier = (
  41. e: React.ChangeEvent<HTMLInputElement>
  42. ) => {
  43. onChange(e.target.value);
  44. };
  45. switch (action.targetType) {
  46. case TargetType.TEAM:
  47. case TargetType.USER:
  48. const isTeam = action.targetType === TargetType.TEAM;
  49. return isTeam ? (
  50. <TeamSelector
  51. disabled={disabled}
  52. key="team"
  53. project={project}
  54. value={action.targetIdentifier}
  55. onChange={handleChangeTargetIdentifier}
  56. useId
  57. />
  58. ) : (
  59. <SelectMembers
  60. disabled={disabled}
  61. key="member"
  62. project={project}
  63. organization={organization}
  64. value={action.targetIdentifier}
  65. onChange={handleChangeTargetIdentifier}
  66. />
  67. );
  68. case TargetType.SPECIFIC:
  69. return availableAction?.options ? (
  70. <SelectControl
  71. isDisabled={disabled || loading}
  72. value={action.targetIdentifier}
  73. options={availableAction.options}
  74. onChange={handleChangeTargetIdentifier}
  75. />
  76. ) : (
  77. <Input
  78. type="text"
  79. autoComplete="off"
  80. disabled={disabled}
  81. key={action.type}
  82. value={action.targetIdentifier || ''}
  83. onChange={handleChangeSpecificTargetIdentifier}
  84. placeholder={getPlaceholderForType(action.type)}
  85. />
  86. );
  87. default:
  88. return null;
  89. }
  90. }