sentryAppDetailedView.tsx 9.5 KB

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