integrationServerlessRow.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {
  4. addErrorMessage,
  5. addLoadingMessage,
  6. addSuccessMessage,
  7. } from 'sentry/actionCreators/indicator';
  8. import type {Client} from 'sentry/api';
  9. import {Button} from 'sentry/components/button';
  10. import Switch from 'sentry/components/switchButton';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {
  14. Organization,
  15. OrganizationIntegration,
  16. ServerlessFunction,
  17. } from 'sentry/types';
  18. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  19. import withApi from 'sentry/utils/withApi';
  20. type Props = {
  21. api: Client;
  22. integration: OrganizationIntegration;
  23. onUpdateFunction: (serverlessFunctionUpdate: Partial<ServerlessFunction>) => void;
  24. organization: Organization;
  25. serverlessFunction: ServerlessFunction;
  26. };
  27. type State = {
  28. submitting: boolean;
  29. };
  30. class IntegrationServerlessRow extends Component<Props, State> {
  31. state: State = {
  32. submitting: false,
  33. };
  34. get enabled() {
  35. return this.props.serverlessFunction.enabled;
  36. }
  37. get endpoint() {
  38. const orgSlug = this.props.organization.slug;
  39. return `/organizations/${orgSlug}/integrations/${this.props.integration.id}/serverless-functions/`;
  40. }
  41. recordAction = (action: 'enable' | 'disable' | 'updateVersion') => {
  42. trackIntegrationAnalytics('integrations.serverless_function_action', {
  43. integration: this.props.integration.provider.key,
  44. integration_type: 'first_party',
  45. action,
  46. organization: this.props.organization,
  47. });
  48. };
  49. toggleEnable = async () => {
  50. const {serverlessFunction} = this.props;
  51. const action = this.enabled ? 'disable' : 'enable';
  52. const data = {
  53. action,
  54. target: serverlessFunction.name,
  55. };
  56. try {
  57. addLoadingMessage();
  58. this.setState({submitting: true});
  59. // optimistically update enable state
  60. this.props.onUpdateFunction({enabled: !this.enabled});
  61. this.recordAction(action);
  62. const resp = await this.props.api.requestPromise(this.endpoint, {
  63. method: 'POST',
  64. data,
  65. });
  66. // update remaining after response
  67. this.props.onUpdateFunction(resp);
  68. addSuccessMessage(t('Success'));
  69. } catch (err) {
  70. // restore original on failure
  71. this.props.onUpdateFunction(serverlessFunction);
  72. addErrorMessage(err.responseJSON?.detail ?? t('Error occurred'));
  73. }
  74. this.setState({submitting: false});
  75. };
  76. updateVersion = async () => {
  77. const {serverlessFunction} = this.props;
  78. const data = {
  79. action: 'updateVersion',
  80. target: serverlessFunction.name,
  81. };
  82. try {
  83. this.setState({submitting: true});
  84. // don't know the latest version but at least optimistically remove the update button
  85. this.props.onUpdateFunction({outOfDate: false});
  86. addLoadingMessage();
  87. this.recordAction('updateVersion');
  88. const resp = await this.props.api.requestPromise(this.endpoint, {
  89. method: 'POST',
  90. data,
  91. });
  92. // update remaining after response
  93. this.props.onUpdateFunction(resp);
  94. addSuccessMessage(t('Success'));
  95. } catch (err) {
  96. // restore original on failure
  97. this.props.onUpdateFunction(serverlessFunction);
  98. addErrorMessage(err.responseJSON?.detail ?? t('Error occurred'));
  99. }
  100. this.setState({submitting: false});
  101. };
  102. renderLayerStatus() {
  103. const {serverlessFunction} = this.props;
  104. if (!serverlessFunction.outOfDate) {
  105. return this.enabled ? t('Latest') : t('Disabled');
  106. }
  107. return (
  108. <UpdateButton size="sm" priority="primary" onClick={this.updateVersion}>
  109. {t('Update')}
  110. </UpdateButton>
  111. );
  112. }
  113. render() {
  114. const {serverlessFunction} = this.props;
  115. const {version} = serverlessFunction;
  116. // during optimistic update, we might be enabled without a version
  117. const versionText =
  118. this.enabled && version > 0 ? <Fragment>&nbsp;|&nbsp;v{version}</Fragment> : null;
  119. return (
  120. <Item>
  121. <NameWrapper>
  122. <NameRuntimeVersionWrapper>
  123. <Name>{serverlessFunction.name}</Name>
  124. <RuntimeAndVersion>
  125. <DetailWrapper>{serverlessFunction.runtime}</DetailWrapper>
  126. <DetailWrapper>{versionText}</DetailWrapper>
  127. </RuntimeAndVersion>
  128. </NameRuntimeVersionWrapper>
  129. </NameWrapper>
  130. <LayerStatusWrapper>{this.renderLayerStatus()}</LayerStatusWrapper>
  131. <StyledSwitch
  132. isActive={this.enabled}
  133. isDisabled={this.state.submitting}
  134. size="sm"
  135. toggle={this.toggleEnable}
  136. />
  137. </Item>
  138. );
  139. }
  140. }
  141. export default withApi(IntegrationServerlessRow);
  142. const Item = styled('div')`
  143. padding: ${space(2)};
  144. &:not(:last-child) {
  145. border-bottom: 1px solid ${p => p.theme.innerBorder};
  146. }
  147. display: grid;
  148. grid-column-gap: ${space(1)};
  149. align-items: center;
  150. grid-template-columns: 2fr 1fr 0.5fr;
  151. grid-template-areas: 'function-name layer-status enable-switch';
  152. `;
  153. const ItemWrapper = styled('span')`
  154. height: 32px;
  155. vertical-align: middle;
  156. display: flex;
  157. align-items: center;
  158. `;
  159. const NameWrapper = styled(ItemWrapper)`
  160. grid-area: function-name;
  161. `;
  162. const LayerStatusWrapper = styled(ItemWrapper)`
  163. grid-area: layer-status;
  164. `;
  165. const StyledSwitch = styled(Switch)`
  166. grid-area: enable-switch;
  167. `;
  168. const UpdateButton = styled(Button)``;
  169. const NameRuntimeVersionWrapper = styled('div')`
  170. display: flex;
  171. flex-direction: column;
  172. `;
  173. const Name = styled(`span`)`
  174. padding-bottom: ${space(1)};
  175. `;
  176. const RuntimeAndVersion = styled('div')`
  177. display: flex;
  178. flex-direction: row;
  179. color: ${p => p.theme.gray300};
  180. `;
  181. const DetailWrapper = styled('div')`
  182. line-height: 1.2;
  183. `;