index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {removeSentryApp} from 'sentry/actionCreators/sentryApps';
  5. import {removeSentryFunction} from 'sentry/actionCreators/sentryFunctions';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import NavTabs from 'sentry/components/navTabs';
  9. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  10. import {t, tct} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Organization, SentryApp, SentryFunction} from 'sentry/types';
  13. import {
  14. platformEventLinkMap,
  15. PlatformEvents,
  16. } from 'sentry/utils/analytics/integrations/platformAnalyticsEvents';
  17. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  18. import routeTitleGen from 'sentry/utils/routeTitle';
  19. import withOrganization from 'sentry/utils/withOrganization';
  20. import AsyncView from 'sentry/views/asyncView';
  21. import CreateIntegrationButton from 'sentry/views/organizationIntegrations/createIntegrationButton';
  22. import ExampleIntegrationButton from 'sentry/views/organizationIntegrations/exampleIntegrationButton';
  23. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  24. import SentryApplicationRow from 'sentry/views/settings/organizationDeveloperSettings/sentryApplicationRow';
  25. import SentryFunctionRow from './sentryFunctionRow';
  26. type Props = Omit<AsyncView['props'], 'params'> & {
  27. organization: Organization;
  28. } & RouteComponentProps<{orgId: string}, {}>;
  29. type Tab = 'public' | 'internal' | 'sentryfx';
  30. type State = AsyncView['state'] & {
  31. applications: SentryApp[];
  32. tab: Tab;
  33. };
  34. class OrganizationDeveloperSettings extends AsyncView<Props, State> {
  35. analyticsView = 'developer_settings' as const;
  36. getDefaultState(): State {
  37. const {location} = this.props;
  38. const value =
  39. (['public', 'internal', 'sentryfx'] as const).find(
  40. tab => tab === location?.query?.type
  41. ) || 'internal';
  42. return {
  43. ...super.getDefaultState(),
  44. applications: [],
  45. sentryFunctions: [],
  46. tab: value,
  47. };
  48. }
  49. get tab() {
  50. return this.state.tab;
  51. }
  52. getTitle() {
  53. const {orgId} = this.props.params;
  54. return routeTitleGen(t('Developer Settings'), orgId, false);
  55. }
  56. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  57. const {orgId} = this.props.params;
  58. const {organization} = this.props;
  59. const returnValue: [string, string, any?, any?][] = [
  60. ['applications', `/organizations/${orgId}/sentry-apps/`],
  61. ];
  62. if (organization.features.includes('sentry-functions')) {
  63. returnValue.push(['sentryFunctions', `/organizations/${orgId}/functions/`]);
  64. }
  65. return returnValue;
  66. }
  67. removeApp = (app: SentryApp) => {
  68. const apps = this.state.applications.filter(a => a.slug !== app.slug);
  69. removeSentryApp(this.api, app).then(
  70. () => {
  71. this.setState({applications: apps});
  72. },
  73. () => {}
  74. );
  75. };
  76. removeFunction = (organization: Organization, sentryFunction: SentryFunction) => {
  77. const functionsToKeep = this.state.sentryFunctions?.filter(
  78. fn => fn.name !== sentryFunction.name
  79. );
  80. if (!functionsToKeep) {
  81. return;
  82. }
  83. removeSentryFunction(this.api, organization, sentryFunction).then(
  84. isSuccess => {
  85. if (isSuccess) {
  86. this.setState({sentryFunctions: functionsToKeep});
  87. }
  88. },
  89. () => {}
  90. );
  91. };
  92. onTabChange = (value: Tab) => {
  93. this.setState({tab: value});
  94. };
  95. renderSentryFunction = (sentryFunction: SentryFunction) => {
  96. const {organization} = this.props;
  97. return (
  98. <SentryFunctionRow
  99. key={organization.slug + sentryFunction.name}
  100. organization={organization}
  101. sentryFunction={sentryFunction}
  102. onRemoveFunction={this.removeFunction}
  103. />
  104. );
  105. };
  106. renderSentryFunctions() {
  107. const {sentryFunctions} = this.state;
  108. return (
  109. <Panel>
  110. <PanelHeader>{t('Sentry Functions')}</PanelHeader>
  111. <PanelBody>
  112. {sentryFunctions?.length ? (
  113. sentryFunctions.map(this.renderSentryFunction)
  114. ) : (
  115. <EmptyMessage>{t('No Sentry Functions have been created yet.')}</EmptyMessage>
  116. )}
  117. </PanelBody>
  118. </Panel>
  119. );
  120. }
  121. renderApplicationRow = (app: SentryApp) => {
  122. const {organization} = this.props;
  123. return (
  124. <SentryApplicationRow
  125. key={app.uuid}
  126. app={app}
  127. organization={organization}
  128. onRemoveApp={this.removeApp}
  129. />
  130. );
  131. };
  132. renderInternalIntegrations() {
  133. const integrations = this.state.applications.filter(
  134. (app: SentryApp) => app.status === 'internal'
  135. );
  136. const isEmpty = integrations.length === 0;
  137. return (
  138. <Panel>
  139. <PanelHeader>{t('Internal Integrations')}</PanelHeader>
  140. <PanelBody>
  141. {!isEmpty ? (
  142. integrations.map(this.renderApplicationRow)
  143. ) : (
  144. <EmptyMessage>
  145. {t('No internal integrations have been created yet.')}
  146. </EmptyMessage>
  147. )}
  148. </PanelBody>
  149. </Panel>
  150. );
  151. }
  152. renderPublicIntegrations() {
  153. const integrations = this.state.applications.filter(app => app.status !== 'internal');
  154. const isEmpty = integrations.length === 0;
  155. return (
  156. <Panel>
  157. <PanelHeader>{t('Public Integrations')}</PanelHeader>
  158. <PanelBody>
  159. {!isEmpty ? (
  160. integrations.map(this.renderApplicationRow)
  161. ) : (
  162. <EmptyMessage>
  163. {t('No public integrations have been created yet.')}
  164. </EmptyMessage>
  165. )}
  166. </PanelBody>
  167. </Panel>
  168. );
  169. }
  170. renderTabContent(tab: Tab) {
  171. switch (tab) {
  172. case 'sentryfx':
  173. return this.renderSentryFunctions();
  174. case 'internal':
  175. return this.renderInternalIntegrations();
  176. case 'public':
  177. default:
  178. return this.renderPublicIntegrations();
  179. }
  180. }
  181. renderBody() {
  182. const {organization} = this.props;
  183. const tabs: [id: Tab, label: string][] = [
  184. ['internal', t('Internal Integration')],
  185. ['public', t('Public Integration')],
  186. ];
  187. if (organization.features.includes('sentry-functions')) {
  188. tabs.push(['sentryfx', t('Sentry Function')]);
  189. }
  190. return (
  191. <div>
  192. <SettingsPageHeader
  193. title={t('Developer Settings')}
  194. body={
  195. <Fragment>
  196. {t(
  197. 'Create integrations that interact with Sentry using the REST API and webhooks. '
  198. )}
  199. <br />
  200. {tct('For more information [link: see our docs].', {
  201. link: (
  202. <ExternalLink
  203. href={platformEventLinkMap[PlatformEvents.DOCS]}
  204. onClick={() => {
  205. trackIntegrationAnalytics(PlatformEvents.DOCS, {
  206. organization,
  207. view: this.analyticsView,
  208. });
  209. }}
  210. />
  211. ),
  212. })}
  213. </Fragment>
  214. }
  215. action={
  216. <ActionContainer>
  217. <ExampleIntegrationButton
  218. analyticsView={this.analyticsView}
  219. style={{marginRight: space(1)}}
  220. />
  221. <CreateIntegrationButton analyticsView={this.analyticsView} />
  222. </ActionContainer>
  223. }
  224. />
  225. <NavTabs underlined>
  226. {tabs.map(([type, label]) => (
  227. <li
  228. key={type}
  229. className={this.tab === type ? 'active' : ''}
  230. onClick={() => this.onTabChange(type)}
  231. >
  232. <a>{label}</a>
  233. </li>
  234. ))}
  235. </NavTabs>
  236. {this.renderTabContent(this.tab)}
  237. </div>
  238. );
  239. }
  240. }
  241. const ActionContainer = styled('div')`
  242. display: flex;
  243. `;
  244. export default withOrganization(OrganizationDeveloperSettings);