sentryFunctionsEnvironmentVariables.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import styled from '@emotion/styled';
  2. import Button from 'sentry/components/button';
  3. import {TextField} from 'sentry/components/forms';
  4. import {PanelBody, PanelHeader} from 'sentry/components/panels';
  5. import {IconAdd, IconDelete} from 'sentry/icons';
  6. import {t, tct} from 'sentry/locale';
  7. import space from 'sentry/styles/space';
  8. type Props = {
  9. envVariables: {
  10. name: string;
  11. value: string;
  12. }[];
  13. setEnvVariables: (envVariables) => void;
  14. };
  15. function SentryFunctionEnvironmentVariables(props: Props) {
  16. const {envVariables, setEnvVariables} = props;
  17. const addEnvVar = () => {
  18. setEnvVariables([...envVariables, {name: '', value: ''}]);
  19. };
  20. const handleNameChange = (value: string, pos: number) => {
  21. const newEnvVariables = [...envVariables];
  22. while (newEnvVariables.length <= pos) {
  23. newEnvVariables.push({name: '', value: ''});
  24. }
  25. newEnvVariables[pos] = {...newEnvVariables[pos], name: value};
  26. setEnvVariables(newEnvVariables);
  27. };
  28. const handleValueChange = (value: string, pos: number) => {
  29. const newEnvVariables = [...envVariables];
  30. while (newEnvVariables.length <= pos) {
  31. newEnvVariables.push({name: '', value: ''});
  32. }
  33. newEnvVariables[pos] = {...newEnvVariables[pos], value};
  34. setEnvVariables(newEnvVariables);
  35. };
  36. const removeEnvVar = (pos: number) => {
  37. const newEnvVariables = [...envVariables];
  38. newEnvVariables.splice(pos, 1);
  39. setEnvVariables(newEnvVariables);
  40. };
  41. return (
  42. <div>
  43. <PanelHeader>
  44. {t('Environment Variables')}
  45. <StyledAddButton
  46. size="sm"
  47. type="button"
  48. icon={<IconAdd isCircled />}
  49. aria-label={t('Add Environment Variable')}
  50. onClick={addEnvVar}
  51. />
  52. </PanelHeader>
  53. <StyledPanelBody>
  54. <EnvironmentVariableWrapper>
  55. <EnvHeader>{t('Name')}</EnvHeader>
  56. <EnvHeaderRight>{t('Value')}</EnvHeaderRight>
  57. </EnvironmentVariableWrapper>
  58. {envVariables.map((envVariable, i) => {
  59. return (
  60. <EnvironmentVariableWrapper key={i}>
  61. <TextField
  62. name={`env-variable-name-${i}`}
  63. required={false}
  64. inline={false}
  65. defaultValue={envVariable.name}
  66. value={envVariable.name}
  67. stacked
  68. onChange={e => handleNameChange(e, i)}
  69. />
  70. <TextField
  71. name={`env-variable-value-${i}`}
  72. required={false}
  73. inline={false}
  74. defaultValue={envVariable.value}
  75. value={envVariable.value}
  76. stacked
  77. onChange={e => handleValueChange(e, i)}
  78. />
  79. <ButtonHolder>
  80. <StyledAddButton
  81. size="sm"
  82. icon={<IconDelete />}
  83. type="button"
  84. aria-label={tct('Remove Environment Variable [i]', {i})}
  85. onClick={() => removeEnvVar(i)}
  86. />
  87. </ButtonHolder>
  88. </EnvironmentVariableWrapper>
  89. );
  90. })}
  91. </StyledPanelBody>
  92. </div>
  93. );
  94. }
  95. export default SentryFunctionEnvironmentVariables;
  96. const EnvironmentVariableWrapper = styled('div')`
  97. display: grid;
  98. grid-template-columns: 1fr 1.5fr min-content;
  99. `;
  100. const StyledAddButton = styled(Button)`
  101. float: right;
  102. `;
  103. const EnvHeader = styled('div')`
  104. text-align: left;
  105. margin-top: ${space(2)};
  106. margin-bottom: ${space(1)};
  107. color: ${p => p.theme.gray400};
  108. `;
  109. const EnvHeaderRight = styled(EnvHeader)`
  110. margin-left: -${space(2)};
  111. `;
  112. const ButtonHolder = styled('div')`
  113. align-items: center;
  114. display: flex;
  115. margin-bottom: ${space(2)};
  116. `;
  117. const StyledPanelBody = styled(PanelBody)`
  118. padding: ${space(2)};
  119. `;