sentryAppDetailedView.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import styled from '@emotion/styled';
  2. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  3. import {openModal} from 'sentry/actionCreators/modal';
  4. import {
  5. installSentryApp,
  6. uninstallSentryApp,
  7. } from 'sentry/actionCreators/sentryAppInstallations';
  8. import {Button} from 'sentry/components/button';
  9. import CircleIndicator from 'sentry/components/circleIndicator';
  10. import Confirm from 'sentry/components/confirm';
  11. import type DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  12. import SentryAppIcon from 'sentry/components/sentryAppIcon';
  13. import {IconSubtract} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {IntegrationFeature, SentryApp, SentryAppInstallation} from 'sentry/types';
  17. import {toPermissions} from 'sentry/utils/consolidatedScopes';
  18. import {getSentryAppInstallStatus} from 'sentry/utils/integrationUtil';
  19. import {addQueryParamsToExistingUrl} from 'sentry/utils/queryString';
  20. import {recordInteraction} from 'sentry/utils/recordSentryAppInteraction';
  21. import withOrganization from 'sentry/utils/withOrganization';
  22. import AbstractIntegrationDetailedView from './abstractIntegrationDetailedView';
  23. import {SplitInstallationIdModal} from './SplitInstallationIdModal';
  24. type State = {
  25. appInstalls: SentryAppInstallation[];
  26. featureData: IntegrationFeature[];
  27. sentryApp: SentryApp;
  28. };
  29. type Tab = AbstractIntegrationDetailedView['state']['tab'];
  30. class SentryAppDetailedView extends AbstractIntegrationDetailedView<
  31. AbstractIntegrationDetailedView['props'],
  32. State & AbstractIntegrationDetailedView['state']
  33. > {
  34. tabs: Tab[] = ['overview'];
  35. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  36. const {
  37. organization,
  38. params: {integrationSlug},
  39. } = this.props;
  40. return [
  41. ['sentryApp', `/sentry-apps/${integrationSlug}/`],
  42. ['featureData', `/sentry-apps/${integrationSlug}/features/`],
  43. ['appInstalls', `/organizations/${organization.slug}/sentry-app-installations/`],
  44. ];
  45. }
  46. onLoadAllEndpointsSuccess() {
  47. const {
  48. organization,
  49. params: {integrationSlug},
  50. router,
  51. } = this.props;
  52. // redirect for internal integrations
  53. if (this.sentryApp.status === 'internal') {
  54. router.push(
  55. `/settings/${organization.slug}/developer-settings/${integrationSlug}/`
  56. );
  57. return;
  58. }
  59. super.onLoadAllEndpointsSuccess();
  60. recordInteraction(integrationSlug, 'sentry_app_viewed');
  61. }
  62. get integrationType() {
  63. return 'sentry_app' as const;
  64. }
  65. get sentryApp() {
  66. return this.state.sentryApp;
  67. }
  68. get description() {
  69. return this.state.sentryApp.overview || '';
  70. }
  71. get author() {
  72. return this.sentryApp.author;
  73. }
  74. get resourceLinks() {
  75. // only show links for published sentry apps
  76. if (this.sentryApp.status !== 'published') {
  77. return [];
  78. }
  79. return [
  80. {
  81. title: 'Documentation',
  82. url: `https://docs.sentry.io/product/integrations/${this.integrationSlug}/`,
  83. },
  84. ];
  85. }
  86. get permissions() {
  87. return toPermissions(this.sentryApp.scopes);
  88. }
  89. get installationStatus() {
  90. return getSentryAppInstallStatus(this.install);
  91. }
  92. get integrationName() {
  93. return this.sentryApp.name;
  94. }
  95. get featureData() {
  96. return this.state.featureData;
  97. }
  98. get install() {
  99. return this.state.appInstalls.find(i => i.app.slug === this.sentryApp.slug);
  100. }
  101. redirectUser = (install: SentryAppInstallation) => {
  102. const {organization} = this.props;
  103. const {sentryApp} = this.state;
  104. const queryParams = {
  105. installationId: install.uuid,
  106. code: install.code,
  107. orgSlug: organization.slug,
  108. };
  109. if (sentryApp.redirectUrl) {
  110. const redirectUrl = addQueryParamsToExistingUrl(sentryApp.redirectUrl, queryParams);
  111. window.location.assign(redirectUrl);
  112. }
  113. };
  114. handleInstall = async () => {
  115. const {organization} = this.props;
  116. const {sentryApp} = this.state;
  117. this.trackIntegrationAnalytics('integrations.installation_start', {
  118. integration_status: sentryApp.status,
  119. });
  120. // installSentryApp adds a message on failure
  121. const install = await installSentryApp(this.api, organization.slug, sentryApp);
  122. // installation is complete if the status is installed
  123. if (install.status === 'installed') {
  124. this.trackIntegrationAnalytics('integrations.installation_complete', {
  125. integration_status: sentryApp.status,
  126. });
  127. }
  128. if (!sentryApp.redirectUrl) {
  129. addSuccessMessage(t('%s successfully installed.', sentryApp.slug));
  130. this.setState({appInstalls: [install, ...this.state.appInstalls]});
  131. // hack for split so we can show the install ID to users for them to copy
  132. // Will remove once the proper fix is in place
  133. if (['split', 'split-dev', 'split-testing'].includes(sentryApp.slug)) {
  134. openModal(({closeModal}) => (
  135. <SplitInstallationIdModal
  136. installationId={install.uuid}
  137. closeModal={closeModal}
  138. />
  139. ));
  140. }
  141. } else {
  142. this.redirectUser(install);
  143. }
  144. };
  145. handleUninstall = async (install: SentryAppInstallation) => {
  146. try {
  147. await uninstallSentryApp(this.api, install);
  148. this.trackIntegrationAnalytics('integrations.uninstall_completed', {
  149. integration_status: this.sentryApp.status,
  150. });
  151. const appInstalls = this.state.appInstalls.filter(
  152. i => i.app.slug !== this.sentryApp.slug
  153. );
  154. return this.setState({appInstalls});
  155. } catch (error) {
  156. return addErrorMessage(t('Unable to uninstall %s', this.sentryApp.name));
  157. }
  158. };
  159. recordUninstallClicked = () => {
  160. const sentryApp = this.sentryApp;
  161. this.trackIntegrationAnalytics('integrations.uninstall_clicked', {
  162. integration_status: sentryApp.status,
  163. });
  164. };
  165. renderPermissions() {
  166. const permissions = this.permissions;
  167. if (!Object.keys(permissions).some(scope => permissions[scope].length > 0)) {
  168. return null;
  169. }
  170. return (
  171. <PermissionWrapper>
  172. <Title>Permissions</Title>
  173. {permissions.read.length > 0 && (
  174. <Permission>
  175. <Indicator />
  176. <Text key="read">
  177. {tct('[read] access to [resources] resources', {
  178. read: <strong>Read</strong>,
  179. resources: permissions.read.join(', '),
  180. })}
  181. </Text>
  182. </Permission>
  183. )}
  184. {permissions.write.length > 0 && (
  185. <Permission>
  186. <Indicator />
  187. <Text key="write">
  188. {tct('[read] and [write] access to [resources] resources', {
  189. read: <strong>Read</strong>,
  190. write: <strong>Write</strong>,
  191. resources: permissions.write.join(', '),
  192. })}
  193. </Text>
  194. </Permission>
  195. )}
  196. {permissions.admin.length > 0 && (
  197. <Permission>
  198. <Indicator />
  199. <Text key="admin">
  200. {tct('[admin] access to [resources] resources', {
  201. admin: <strong>Admin</strong>,
  202. resources: permissions.admin.join(', '),
  203. })}
  204. </Text>
  205. </Permission>
  206. )}
  207. </PermissionWrapper>
  208. );
  209. }
  210. renderTopButton(disabledFromFeatures: boolean, userHasAccess: boolean) {
  211. const install = this.install;
  212. const capitalizedSlug =
  213. this.integrationSlug.charAt(0).toUpperCase() + this.integrationSlug.slice(1);
  214. if (install) {
  215. return (
  216. <Confirm
  217. disabled={!userHasAccess}
  218. message={tct('Are you sure you want to uninstall the [slug] installation?', {
  219. slug: capitalizedSlug,
  220. })}
  221. onConfirm={() => this.handleUninstall(install)} // called when the user confirms the action
  222. onConfirming={this.recordUninstallClicked} // called when the confirm modal opens
  223. priority="danger"
  224. >
  225. <StyledUninstallButton size="sm" data-test-id="sentry-app-uninstall">
  226. <IconSubtract isCircled style={{marginRight: space(0.75)}} />
  227. {t('Uninstall')}
  228. </StyledUninstallButton>
  229. </Confirm>
  230. );
  231. }
  232. if (userHasAccess) {
  233. return (
  234. <InstallButton
  235. data-test-id="install-button"
  236. disabled={disabledFromFeatures}
  237. onClick={() => this.handleInstall()}
  238. priority="primary"
  239. size="sm"
  240. style={{marginLeft: space(1)}}
  241. >
  242. {t('Accept & Install')}
  243. </InstallButton>
  244. );
  245. }
  246. return this.renderRequestIntegrationButton();
  247. }
  248. // no configurations for sentry apps
  249. renderConfigurations() {
  250. return null;
  251. }
  252. renderIntegrationIcon() {
  253. return <SentryAppIcon sentryApp={this.sentryApp} size={50} />;
  254. }
  255. }
  256. const Text = styled('p')`
  257. margin: 0px 6px;
  258. `;
  259. const Permission = styled('div')`
  260. display: flex;
  261. `;
  262. const PermissionWrapper = styled('div')`
  263. padding-bottom: ${space(2)};
  264. `;
  265. const Title = styled('p')`
  266. margin-bottom: ${space(1)};
  267. font-weight: bold;
  268. `;
  269. const Indicator = styled(p => <CircleIndicator size={7} {...p} />)`
  270. align-self: center;
  271. color: ${p => p.theme.success};
  272. `;
  273. const InstallButton = styled(Button)`
  274. margin-left: ${space(1)};
  275. `;
  276. const StyledUninstallButton = styled(Button)`
  277. color: ${p => p.theme.gray300};
  278. background: ${p => p.theme.background};
  279. border: ${p => `1px solid ${p.theme.gray300}`};
  280. box-sizing: border-box;
  281. box-shadow: 0px 2px 1px rgba(0, 0, 0, 0.08);
  282. border-radius: 4px;
  283. `;
  284. export default withOrganization(SentryAppDetailedView);