sentryFunctionsEnvironmentVariables.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import styled from '@emotion/styled';
  2. import {Button} from 'sentry/components/button';
  3. import TextField from 'sentry/components/forms/fields/textField';
  4. import PanelBody from 'sentry/components/panels/panelBody';
  5. import PanelHeader from 'sentry/components/panels/panelHeader';
  6. import {IconAdd, IconDelete} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. type Props = {
  10. envVariables: {
  11. name: string;
  12. value: string;
  13. }[];
  14. setEnvVariables: (envVariables) => void;
  15. };
  16. function SentryFunctionEnvironmentVariables(props: Props) {
  17. const {envVariables, setEnvVariables} = props;
  18. const addEnvVar = () => {
  19. setEnvVariables([...envVariables, {name: '', value: ''}]);
  20. };
  21. const handleNameChange = (value: string, pos: number) => {
  22. const newEnvVariables = [...envVariables];
  23. while (newEnvVariables.length <= pos) {
  24. newEnvVariables.push({name: '', value: ''});
  25. }
  26. newEnvVariables[pos] = {...newEnvVariables[pos], name: value};
  27. setEnvVariables(newEnvVariables);
  28. };
  29. const handleValueChange = (value: string, pos: number) => {
  30. const newEnvVariables = [...envVariables];
  31. while (newEnvVariables.length <= pos) {
  32. newEnvVariables.push({name: '', value: ''});
  33. }
  34. newEnvVariables[pos] = {...newEnvVariables[pos], value};
  35. setEnvVariables(newEnvVariables);
  36. };
  37. const removeEnvVar = (pos: number) => {
  38. const newEnvVariables = [...envVariables];
  39. newEnvVariables.splice(pos, 1);
  40. setEnvVariables(newEnvVariables);
  41. };
  42. return (
  43. <div>
  44. <PanelHeader>
  45. {t('Environment Variables')}
  46. <StyledAddButton
  47. size="sm"
  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. aria-label={t('Remove Environment Variable %s', i)}
  84. onClick={() => removeEnvVar(i)}
  85. />
  86. </ButtonHolder>
  87. </EnvironmentVariableWrapper>
  88. );
  89. })}
  90. </StyledPanelBody>
  91. </div>
  92. );
  93. }
  94. export default SentryFunctionEnvironmentVariables;
  95. const EnvironmentVariableWrapper = styled('div')`
  96. display: grid;
  97. grid-template-columns: 1fr 1.5fr min-content;
  98. `;
  99. const StyledAddButton = styled(Button)`
  100. float: right;
  101. `;
  102. const EnvHeader = styled('div')`
  103. text-align: left;
  104. margin-top: ${space(2)};
  105. margin-bottom: ${space(1)};
  106. color: ${p => p.theme.gray400};
  107. `;
  108. const EnvHeaderRight = styled(EnvHeader)`
  109. margin-left: -${space(2)};
  110. `;
  111. const ButtonHolder = styled('div')`
  112. align-items: center;
  113. display: flex;
  114. margin-bottom: ${space(2)};
  115. `;
  116. const StyledPanelBody = styled(PanelBody)`
  117. padding: ${space(2)};
  118. `;