NotificationChannelOptions.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import React, { FC } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { Button, Checkbox, Field, Input } from '@grafana/ui';
  4. import { NotificationChannelDTO, NotificationChannelOption, NotificationChannelSecureFields } from '../../../types';
  5. import { NotificationSettingsProps } from './NotificationChannelForm';
  6. import { OptionElement } from './OptionElement';
  7. interface Props extends NotificationSettingsProps {
  8. selectedChannelOptions: NotificationChannelOption[];
  9. currentFormValues: NotificationChannelDTO;
  10. secureFields: NotificationChannelSecureFields;
  11. onResetSecureField: (key: string) => void;
  12. }
  13. export const NotificationChannelOptions: FC<Props> = ({
  14. control,
  15. currentFormValues,
  16. errors,
  17. selectedChannelOptions,
  18. register,
  19. onResetSecureField,
  20. secureFields,
  21. }) => {
  22. return (
  23. <>
  24. {selectedChannelOptions.map((option: NotificationChannelOption, index: number) => {
  25. const key = `${option.label}-${index}`;
  26. // Some options can be dependent on other options, this determines what is selected in the dependency options
  27. // I think this needs more thought.
  28. const selectedOptionValue =
  29. currentFormValues[`settings.${option.showWhen.field}`] &&
  30. (currentFormValues[`settings.${option.showWhen.field}`] as SelectableValue<string>).value;
  31. if (option.showWhen.field && selectedOptionValue !== option.showWhen.is) {
  32. return null;
  33. }
  34. if (option.element === 'checkbox') {
  35. return (
  36. <Field key={key}>
  37. <Checkbox
  38. {...register(
  39. option.secure ? `secureSettings.${option.propertyName}` : `settings.${option.propertyName}`
  40. )}
  41. label={option.label}
  42. description={option.description}
  43. />
  44. </Field>
  45. );
  46. }
  47. return (
  48. <Field
  49. key={key}
  50. label={option.label}
  51. description={option.description}
  52. invalid={errors.settings && !!errors.settings[option.propertyName]}
  53. error={errors.settings && errors.settings[option.propertyName]?.message}
  54. >
  55. {secureFields && secureFields[option.propertyName] ? (
  56. <Input
  57. readOnly={true}
  58. value="Configured"
  59. suffix={
  60. <Button onClick={() => onResetSecureField(option.propertyName)} fill="text" type="button" size="sm">
  61. Clear
  62. </Button>
  63. }
  64. />
  65. ) : (
  66. <OptionElement option={option} register={register} control={control} />
  67. )}
  68. </Field>
  69. );
  70. })}
  71. </>
  72. );
  73. };