index.tsx 7.9 KB

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