awsLambdaFunctionSelect.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import reduce from 'lodash/reduce';
  4. import {computed, makeObservable} from 'mobx';
  5. import {Observer} from 'mobx-react';
  6. import type {FormProps} from 'sentry/components/forms/form';
  7. import Form from 'sentry/components/forms/form';
  8. import JsonForm from 'sentry/components/forms/jsonForm';
  9. import FormModel from 'sentry/components/forms/model';
  10. import type {JsonFormObject} from 'sentry/components/forms/types';
  11. import List from 'sentry/components/list';
  12. import ListItem from 'sentry/components/list/listItem';
  13. import LoadingIndicator from 'sentry/components/loadingIndicator';
  14. import PanelHeader from 'sentry/components/panels/panelHeader';
  15. import Switch from 'sentry/components/switchButton';
  16. import {Tooltip} from 'sentry/components/tooltip';
  17. import {t, tn} from 'sentry/locale';
  18. import FooterWithButtons from './components/footerWithButtons';
  19. import HeaderWithHelp from './components/headerWithHelp';
  20. const LAMBDA_COUNT_THRESHOLD = 10;
  21. type LambdaFunction = {FunctionName: string; Runtime: string};
  22. type Props = {
  23. initialStepNumber: number;
  24. lambdaFunctions: LambdaFunction[];
  25. };
  26. type State = {
  27. submitting: boolean;
  28. };
  29. const getLabel = (func: LambdaFunction) => func.FunctionName;
  30. export default class AwsLambdaFunctionSelect extends Component<Props, State> {
  31. constructor(props: Props) {
  32. super(props);
  33. makeObservable(this, {allStatesToggled: computed});
  34. }
  35. state: State = {
  36. submitting: false,
  37. };
  38. model = new FormModel();
  39. get initialData() {
  40. const {lambdaFunctions} = this.props;
  41. const initialData = lambdaFunctions.reduce((accum, func) => {
  42. accum[func.FunctionName] = true;
  43. return accum;
  44. }, {});
  45. return initialData;
  46. }
  47. get lambdaFunctions() {
  48. return this.props.lambdaFunctions.sort((a, b) =>
  49. getLabel(a).toLowerCase() < getLabel(b).toLowerCase() ? -1 : 1
  50. );
  51. }
  52. get enabledCount() {
  53. const data = this.model.getTransformedData();
  54. return reduce(data, (acc: number, val: boolean) => (val ? acc + 1 : acc), 0);
  55. }
  56. get allStatesToggled() {
  57. // check if any of the lambda functions have a falsy value
  58. // no falsy values means everything is enabled
  59. return Object.values(this.model.getData()).every(val => val);
  60. }
  61. get formFields() {
  62. const data = this.model.getTransformedData();
  63. return Object.entries(data).map(([name, value]) => ({name, value}));
  64. }
  65. handleSubmit = () => {
  66. this.setState({submitting: true});
  67. };
  68. handleToggle = () => {
  69. const newState = !this.allStatesToggled;
  70. this.lambdaFunctions.forEach(lambda => {
  71. this.model.setValue(lambda.FunctionName, newState, {quiet: true});
  72. });
  73. };
  74. renderWhatWeFound = () => {
  75. const count = this.lambdaFunctions.length;
  76. return (
  77. <h4>
  78. {tn(
  79. 'We found %s function with a Node or Python runtime',
  80. 'We found %s functions with Node or Python runtimes',
  81. count
  82. )}
  83. </h4>
  84. );
  85. };
  86. renderLoadingScreen = () => {
  87. const count = this.enabledCount;
  88. const text =
  89. count > LAMBDA_COUNT_THRESHOLD
  90. ? t('This might take a while\u2026', count)
  91. : t('This might take a sec\u2026');
  92. return (
  93. <LoadingWrapper>
  94. <StyledLoadingIndicator />
  95. <h4>{t('Adding Sentry to %s functions', count)}</h4>
  96. {text}
  97. </LoadingWrapper>
  98. );
  99. };
  100. renderCore = () => {
  101. const {initialStepNumber} = this.props;
  102. const FormHeader = (
  103. <StyledPanelHeader>
  104. {t('Lambda Functions')}
  105. <SwitchHolder>
  106. <Observer>
  107. {() => (
  108. <Tooltip
  109. title={this.allStatesToggled ? t('Disable All') : t('Enable All')}
  110. position="left"
  111. >
  112. <StyledSwitch
  113. size="lg"
  114. name="toggleAll"
  115. toggle={this.handleToggle}
  116. isActive={this.allStatesToggled}
  117. />
  118. </Tooltip>
  119. )}
  120. </Observer>
  121. </SwitchHolder>
  122. </StyledPanelHeader>
  123. );
  124. const formFields: JsonFormObject = {
  125. fields: this.lambdaFunctions.map(func => ({
  126. name: func.FunctionName,
  127. type: 'boolean',
  128. required: false,
  129. label: getLabel(func),
  130. alignRight: true,
  131. })),
  132. };
  133. return (
  134. <List symbol="colored-numeric" initialCounterValue={initialStepNumber}>
  135. <ListItem>
  136. <Header>{this.renderWhatWeFound()}</Header>
  137. {t('Decide which functions you would like to enable for Sentry monitoring')}
  138. <StyledForm
  139. initialData={this.initialData}
  140. model={this.model}
  141. apiEndpoint="/extensions/aws_lambda/setup/"
  142. hideFooter
  143. preventFormResetOnUnmount
  144. >
  145. <JsonForm renderHeader={() => FormHeader} forms={[formFields]} />
  146. </StyledForm>
  147. </ListItem>
  148. <Fragment />
  149. </List>
  150. );
  151. };
  152. render() {
  153. return (
  154. <Fragment>
  155. <HeaderWithHelp docsUrl="https://docs.sentry.io/product/integrations/cloud-monitoring/aws-lambda/" />
  156. <Wrapper>
  157. {this.state.submitting ? this.renderLoadingScreen() : this.renderCore()}
  158. </Wrapper>
  159. <Observer>
  160. {() => (
  161. <FooterWithButtons
  162. formProps={{
  163. action: '/extensions/aws_lambda/setup/',
  164. method: 'post',
  165. onSubmit: this.handleSubmit,
  166. }}
  167. formFields={this.formFields}
  168. buttonText={t('Finish Setup')}
  169. disabled={
  170. this.model.isError || this.model.isSaving || this.state.submitting
  171. }
  172. />
  173. )}
  174. </Observer>
  175. </Fragment>
  176. );
  177. }
  178. }
  179. const Wrapper = styled('div')`
  180. padding: 100px 50px 50px 50px;
  181. `;
  182. // TODO(ts): Understand why styled is not correctly inheriting props here
  183. const StyledForm = styled(Form)<FormProps>`
  184. margin-top: 10px;
  185. `;
  186. const Header = styled('div')`
  187. text-align: left;
  188. margin-bottom: 10px;
  189. `;
  190. const LoadingWrapper = styled('div')`
  191. padding: 50px;
  192. text-align: center;
  193. `;
  194. const StyledLoadingIndicator = styled(LoadingIndicator)`
  195. margin: 0;
  196. `;
  197. const SwitchHolder = styled('div')`
  198. display: flex;
  199. `;
  200. const StyledSwitch = styled(Switch)`
  201. margin: auto;
  202. `;
  203. // padding is based on fom control width
  204. const StyledPanelHeader = styled(PanelHeader)`
  205. padding-right: 36px;
  206. `;