You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateWebhookSecretField.tsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { useField } from 'formik';
  21. import * as React from 'react';
  22. import InputValidationField from '../../../components/controls/InputValidationField';
  23. import ModalValidationField from '../../../components/controls/ModalValidationField';
  24. import { ButtonLink } from '../../../components/controls/buttons';
  25. import { translate } from '../../../helpers/l10n';
  26. interface Props {
  27. description?: string;
  28. dirty: boolean;
  29. disabled: boolean;
  30. error: string | undefined;
  31. id?: string;
  32. isUpdateForm: boolean;
  33. label?: React.ReactNode;
  34. name: string;
  35. onBlur: (event: React.FocusEvent<HTMLInputElement>) => void;
  36. onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
  37. touched: boolean | undefined;
  38. type?: string;
  39. value?: string;
  40. }
  41. export default function UpdateWebhookSecretField({
  42. isUpdateForm,
  43. description,
  44. dirty,
  45. disabled,
  46. error,
  47. id,
  48. label,
  49. name,
  50. onBlur,
  51. onChange,
  52. touched,
  53. type,
  54. value,
  55. }: Props) {
  56. const [isSecretInputDisplayed, setIsSecretInputDisplayed] = React.useState(false);
  57. const [, , { setValue: setSecretValue }] = useField('secret');
  58. const showSecretInput = () => {
  59. setSecretValue('');
  60. setIsSecretInputDisplayed(true);
  61. };
  62. return !isUpdateForm || isSecretInputDisplayed ? (
  63. <InputValidationField
  64. description={description}
  65. dirty={dirty}
  66. disabled={disabled}
  67. error={error}
  68. id={id}
  69. label={label}
  70. name={name}
  71. onBlur={onBlur}
  72. onChange={onChange}
  73. touched={touched}
  74. type={type}
  75. value={value as string}
  76. />
  77. ) : (
  78. <ModalValidationField
  79. description={description}
  80. dirty={false}
  81. error={undefined}
  82. label={label}
  83. touched={true}
  84. >
  85. {() => (
  86. <div className="sw-mb-5 sw-leading-6 sw-flex sw-items-center">
  87. <span className="sw-mr-1/2">{translate('webhooks.secret.field_mask.description')}</span>
  88. <ButtonLink onClick={showSecretInput}>
  89. {translate('webhooks.secret.field_mask.link')}
  90. </ButtonLink>
  91. </div>
  92. )}
  93. </ModalValidationField>
  94. );
  95. }