index.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {removeSentryApp} from 'sentry/actionCreators/sentryApps';
  4. import EmptyMessage from 'sentry/components/emptyMessage';
  5. import ExternalLink from 'sentry/components/links/externalLink';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import NavTabs from 'sentry/components/navTabs';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelBody from 'sentry/components/panels/panelBody';
  11. import PanelHeader from 'sentry/components/panels/panelHeader';
  12. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {SentryApp} from 'sentry/types/integrations';
  16. import {
  17. platformEventLinkMap,
  18. PlatformEvents,
  19. } from 'sentry/utils/analytics/integrations/platformAnalyticsEvents';
  20. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  21. import {useApiQuery} from 'sentry/utils/queryClient';
  22. import useApi from 'sentry/utils/useApi';
  23. import {useLocation} from 'sentry/utils/useLocation';
  24. import useOrganization from 'sentry/utils/useOrganization';
  25. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  26. import SentryApplicationRow from 'sentry/views/settings/organizationDeveloperSettings/sentryApplicationRow';
  27. import CreateIntegrationButton from 'sentry/views/settings/organizationIntegrations/createIntegrationButton';
  28. import ExampleIntegrationButton from 'sentry/views/settings/organizationIntegrations/exampleIntegrationButton';
  29. type Tab = 'public' | 'internal';
  30. function OrganizationDeveloperSettings() {
  31. const location = useLocation();
  32. const organization = useOrganization();
  33. const api = useApi({persistInFlight: true});
  34. const value =
  35. ['public', 'internal'].find(tab => tab === location?.query?.type) || 'internal';
  36. const analyticsView = 'developer_settings';
  37. const tabs: Array<[id: Tab, label: string]> = [
  38. ['internal', t('Internal Integration')],
  39. ['public', t('Public Integration')],
  40. ];
  41. const [tab, setTab] = useState<Tab>(value as Tab);
  42. const [applicationsState, setApplicationsState] = useState<SentryApp[] | undefined>(
  43. undefined
  44. );
  45. const {
  46. data: fetchedApplications,
  47. isPending,
  48. isError,
  49. refetch,
  50. } = useApiQuery<SentryApp[]>([`/organizations/${organization.slug}/sentry-apps/`], {
  51. staleTime: 0,
  52. });
  53. if (isPending) {
  54. return <LoadingIndicator />;
  55. }
  56. if (isError) {
  57. return <LoadingError onRetry={refetch} />;
  58. }
  59. const applications = applicationsState ?? fetchedApplications;
  60. const removeApp = (app: SentryApp) => {
  61. const apps = applications.filter(a => a.slug !== app.slug);
  62. removeSentryApp(api, app).then(
  63. () => setApplicationsState(apps),
  64. () => {}
  65. );
  66. };
  67. const renderApplicationRow = (app: SentryApp) => {
  68. return (
  69. <SentryApplicationRow
  70. key={app.uuid}
  71. app={app}
  72. organization={organization}
  73. onRemoveApp={removeApp}
  74. />
  75. );
  76. };
  77. const renderInternalIntegrations = () => {
  78. const integrations = applications.filter(
  79. (app: SentryApp) => app.status === 'internal'
  80. );
  81. const isEmpty = integrations.length === 0;
  82. return (
  83. <Panel>
  84. <PanelHeader>{t('Internal Integrations')}</PanelHeader>
  85. <PanelBody>
  86. {!isEmpty ? (
  87. integrations.map(renderApplicationRow)
  88. ) : (
  89. <EmptyMessage>
  90. {t('No internal integrations have been created yet.')}
  91. </EmptyMessage>
  92. )}
  93. </PanelBody>
  94. </Panel>
  95. );
  96. };
  97. const renderPublicIntegrations = () => {
  98. const integrations = applications.filter(app => app.status !== 'internal');
  99. const isEmpty = integrations.length === 0;
  100. return (
  101. <Panel>
  102. <PanelHeader>{t('Public Integrations')}</PanelHeader>
  103. <PanelBody>
  104. {!isEmpty ? (
  105. integrations.map(renderApplicationRow)
  106. ) : (
  107. <EmptyMessage>
  108. {t('No public integrations have been created yet.')}
  109. </EmptyMessage>
  110. )}
  111. </PanelBody>
  112. </Panel>
  113. );
  114. };
  115. const renderTabContent = () => {
  116. switch (tab) {
  117. case 'internal':
  118. return renderInternalIntegrations();
  119. case 'public':
  120. default:
  121. return renderPublicIntegrations();
  122. }
  123. };
  124. return (
  125. <div>
  126. <SentryDocumentTitle title={t('Custom Integrations')} orgSlug={organization.slug} />
  127. <SettingsPageHeader
  128. title={t('Custom Integrations')}
  129. body={
  130. <Fragment>
  131. {t(
  132. 'Create integrations that interact with Sentry using the REST API and webhooks. '
  133. )}
  134. <br />
  135. {tct('For more information [link: see our docs].', {
  136. link: (
  137. <ExternalLink
  138. href={platformEventLinkMap[PlatformEvents.DOCS]}
  139. onClick={() => {
  140. trackIntegrationAnalytics(PlatformEvents.DOCS, {
  141. organization,
  142. view: analyticsView,
  143. });
  144. }}
  145. />
  146. ),
  147. })}
  148. </Fragment>
  149. }
  150. action={
  151. <ActionContainer>
  152. <ExampleIntegrationButton
  153. analyticsView={analyticsView}
  154. style={{marginRight: space(1)}}
  155. />
  156. <CreateIntegrationButton analyticsView={analyticsView} />
  157. </ActionContainer>
  158. }
  159. />
  160. <NavTabs underlined>
  161. {tabs.map(([type, label]) => (
  162. <li
  163. key={type}
  164. className={tab === type ? 'active' : ''}
  165. onClick={() => setTab(type)}
  166. >
  167. <a>{label}</a>
  168. </li>
  169. ))}
  170. </NavTabs>
  171. {renderTabContent()}
  172. </div>
  173. );
  174. }
  175. const ActionContainer = styled('div')`
  176. display: flex;
  177. `;
  178. export default OrganizationDeveloperSettings;