sentryFunctionsEnvironmentVariables.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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} 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. icon={<IconAdd isCircled />}
  48. aria-label={t('Add Environment Variable')}
  49. onClick={addEnvVar}
  50. />
  51. </PanelHeader>
  52. <StyledPanelBody>
  53. <EnvironmentVariableWrapper>
  54. <EnvHeader>{t('Name')}</EnvHeader>
  55. <EnvHeaderRight>{t('Value')}</EnvHeaderRight>
  56. </EnvironmentVariableWrapper>
  57. {envVariables.map((envVariable, i) => {
  58. return (
  59. <EnvironmentVariableWrapper key={i}>
  60. <TextField
  61. name={`env-variable-name-${i}`}
  62. required={false}
  63. inline={false}
  64. defaultValue={envVariable.name}
  65. value={envVariable.name}
  66. stacked
  67. onChange={e => handleNameChange(e, i)}
  68. />
  69. <TextField
  70. name={`env-variable-value-${i}`}
  71. required={false}
  72. inline={false}
  73. defaultValue={envVariable.value}
  74. value={envVariable.value}
  75. stacked
  76. onChange={e => handleValueChange(e, i)}
  77. />
  78. <ButtonHolder>
  79. <StyledAddButton
  80. size="sm"
  81. icon={<IconDelete />}
  82. aria-label={t('Remove Environment Variable %s', i)}
  83. onClick={() => removeEnvVar(i)}
  84. />
  85. </ButtonHolder>
  86. </EnvironmentVariableWrapper>
  87. );
  88. })}
  89. </StyledPanelBody>
  90. </div>
  91. );
  92. }
  93. export default SentryFunctionEnvironmentVariables;
  94. const EnvironmentVariableWrapper = styled('div')`
  95. display: grid;
  96. grid-template-columns: 1fr 1.5fr min-content;
  97. `;
  98. const StyledAddButton = styled(Button)`
  99. float: right;
  100. `;
  101. const EnvHeader = styled('div')`
  102. text-align: left;
  103. margin-top: ${space(2)};
  104. margin-bottom: ${space(1)};
  105. color: ${p => p.theme.gray400};
  106. `;
  107. const EnvHeaderRight = styled(EnvHeader)`
  108. margin-left: -${space(2)};
  109. `;
  110. const ButtonHolder = styled('div')`
  111. align-items: center;
  112. display: flex;
  113. margin-bottom: ${space(2)};
  114. `;
  115. const StyledPanelBody = styled(PanelBody)`
  116. padding: ${space(2)};
  117. `;