awsLambdaFunctionSelect.tsx 6.5 KB

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