abstractIntegrationDetailedView.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import startCase from 'lodash/startCase';
  4. import Access from 'sentry/components/acl/access';
  5. import type {AlertProps} from 'sentry/components/alert';
  6. import {Alert} from 'sentry/components/alert';
  7. import Tag from 'sentry/components/badge/tag';
  8. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  9. import EmptyMessage from 'sentry/components/emptyMessage';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import Panel from 'sentry/components/panels/panel';
  12. import {Tooltip} from 'sentry/components/tooltip';
  13. import {IconClose, IconDocs, IconGeneric, IconGithub, IconProject} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  16. import {space} from 'sentry/styles/space';
  17. import type {
  18. IntegrationFeature,
  19. IntegrationInstallationStatus,
  20. IntegrationType,
  21. } from 'sentry/types/integrations';
  22. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  23. import type {Organization} from 'sentry/types/organization';
  24. import type {
  25. IntegrationAnalyticsKey,
  26. IntegrationEventParameters,
  27. } from 'sentry/utils/analytics/integrations';
  28. import {
  29. getCategories,
  30. getIntegrationFeatureGate,
  31. trackIntegrationAnalytics,
  32. } from 'sentry/utils/integrationUtil';
  33. import marked, {singleLineRenderer} from 'sentry/utils/marked';
  34. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  35. import RequestIntegrationButton from './integrationRequest/RequestIntegrationButton';
  36. import IntegrationStatus from './integrationStatus';
  37. export type Tab = 'overview' | 'configurations' | 'features';
  38. interface AlertType extends AlertProps {
  39. text: string;
  40. }
  41. type State = {
  42. tab: Tab;
  43. } & DeprecatedAsyncComponent['state'];
  44. type Props = {
  45. organization: Organization;
  46. } & RouteComponentProps<{integrationSlug: string}, {}> &
  47. DeprecatedAsyncComponent['props'];
  48. abstract class AbstractIntegrationDetailedView<
  49. P extends Props = Props,
  50. S extends State = State,
  51. > extends DeprecatedAsyncComponent<P, S> {
  52. tabs: Tab[] = ['overview', 'configurations'];
  53. componentDidMount() {
  54. super.componentDidMount();
  55. const {location} = this.props;
  56. const value = location.query.tab === 'configurations' ? 'configurations' : 'overview';
  57. this.setState({tab: value});
  58. }
  59. onLoadAllEndpointsSuccess() {
  60. this.trackIntegrationAnalytics('integrations.integration_viewed', {
  61. integration_tab: this.state.tab,
  62. });
  63. }
  64. /**
  65. * Abstract methods defined below
  66. */
  67. // The analytics type used in analytics which is snake case
  68. get integrationType(): IntegrationType {
  69. // Allow children to implement this
  70. throw new Error('Not implemented');
  71. }
  72. get description(): string {
  73. // Allow children to implement this
  74. throw new Error('Not implemented');
  75. }
  76. get author(): string | undefined {
  77. // Allow children to implement this
  78. throw new Error('Not implemented');
  79. }
  80. get alerts(): AlertType[] {
  81. // default is no alerts
  82. return [];
  83. }
  84. // Returns a list of the resources displayed at the bottom of the overview card
  85. get resourceLinks(): Array<{title: string; url: string}> {
  86. // Allow children to implement this
  87. throw new Error('Not implemented');
  88. }
  89. get installationStatus(): IntegrationInstallationStatus | null {
  90. // Allow children to implement this
  91. throw new Error('Not implemented');
  92. }
  93. get integrationName(): string {
  94. // Allow children to implement this
  95. throw new Error('Not implemented');
  96. }
  97. // Checks to see if integration requires admin access to install, doc integrations don't
  98. get requiresAccess(): boolean {
  99. // default is integration requires access to install
  100. return true;
  101. }
  102. // Returns an array of RawIntegrationFeatures which is used in feature gating
  103. // and displaying what the integration does
  104. get featureData(): IntegrationFeature[] {
  105. // Allow children to implement this
  106. throw new Error('Not implemented');
  107. }
  108. getIcon(title: string) {
  109. switch (title) {
  110. case 'View Source':
  111. return <IconProject />;
  112. case 'Report Issue':
  113. return <IconGithub />;
  114. case 'Documentation':
  115. case 'Splunk Setup Instructions':
  116. case 'Trello Setup Instructions':
  117. return <IconDocs />;
  118. default:
  119. return <IconGeneric />;
  120. }
  121. }
  122. onTabChange = (value: Tab) => {
  123. this.trackIntegrationAnalytics('integrations.integration_tab_clicked', {
  124. integration_tab: value,
  125. });
  126. this.setState({tab: value});
  127. };
  128. // Returns the string that is shown as the title of a tab
  129. getTabDisplay(tab: Tab): string {
  130. // default is return the tab
  131. return tab;
  132. }
  133. // Render the button at the top which is usually just an installation button
  134. renderTopButton(
  135. _disabledFromFeatures: boolean, // from the feature gate
  136. _userHasAccess: boolean // from user permissions
  137. ): React.ReactElement {
  138. // Allow children to implement this
  139. throw new Error('Not implemented');
  140. }
  141. // Returns the permission descriptions, only use by Sentry Apps
  142. renderPermissions(): React.ReactElement | null {
  143. // default is don't render permissions
  144. return null;
  145. }
  146. renderEmptyConfigurations() {
  147. return (
  148. <Panel>
  149. <EmptyMessage
  150. title={t("You haven't set anything up yet")}
  151. description={t(
  152. 'But that doesn’t have to be the case for long! Add an installation to get started.'
  153. )}
  154. action={this.renderAddInstallButton(true)}
  155. />
  156. </Panel>
  157. );
  158. }
  159. // Returns the list of configurations for the integration
  160. abstract renderConfigurations(): React.ReactNode;
  161. /**
  162. * Actually implemented methods below
  163. */
  164. get integrationSlug() {
  165. return this.props.params.integrationSlug;
  166. }
  167. // Wrapper around trackIntegrationAnalytics that automatically provides many fields and the org
  168. trackIntegrationAnalytics = <T extends IntegrationAnalyticsKey>(
  169. eventKey: IntegrationAnalyticsKey,
  170. options?: Partial<IntegrationEventParameters[T]>
  171. ) => {
  172. options = options || {};
  173. // If we use this intermediate type we get type checking on the things we care about
  174. trackIntegrationAnalytics(eventKey, {
  175. view: 'integrations_directory_integration_detail',
  176. integration: this.integrationSlug,
  177. integration_type: this.integrationType,
  178. already_installed: this.installationStatus !== 'Not Installed', // pending counts as installed here
  179. organization: this.props.organization,
  180. ...options,
  181. });
  182. };
  183. // Returns the props as needed by the hooks integrations:feature-gates
  184. get featureProps() {
  185. const {organization} = this.props;
  186. const featureData = this.featureData;
  187. // Prepare the features list
  188. const features = featureData.map(f => ({
  189. featureGate: f.featureGate,
  190. description: (
  191. <FeatureListItem
  192. dangerouslySetInnerHTML={{__html: singleLineRenderer(f.description)}}
  193. />
  194. ),
  195. }));
  196. return {organization, features};
  197. }
  198. cleanTags() {
  199. return getCategories(this.featureData);
  200. }
  201. renderAlert(): React.ReactNode {
  202. return null;
  203. }
  204. renderAdditionalCTA(): React.ReactNode {
  205. return null;
  206. }
  207. renderIntegrationIcon() {
  208. return <PluginIcon pluginId={this.integrationSlug} size={50} />;
  209. }
  210. renderRequestIntegrationButton() {
  211. return (
  212. <RequestIntegrationButton
  213. name={this.integrationName}
  214. slug={this.integrationSlug}
  215. type={this.integrationType}
  216. />
  217. );
  218. }
  219. renderAddInstallButton(hideButtonIfDisabled = false) {
  220. const {IntegrationFeatures} = getIntegrationFeatureGate();
  221. return (
  222. <IntegrationFeatures {...this.featureProps}>
  223. {({disabled, disabledReason}) => (
  224. <DisableWrapper>
  225. <Access access={['org:integrations']}>
  226. {({hasAccess}) => (
  227. <Tooltip
  228. title={t(
  229. 'You must be an organization owner, manager or admin to install this.'
  230. )}
  231. disabled={hasAccess || !this.requiresAccess}
  232. >
  233. {!hideButtonIfDisabled && disabled ? (
  234. <div />
  235. ) : (
  236. this.renderTopButton(disabled, hasAccess)
  237. )}
  238. </Tooltip>
  239. )}
  240. </Access>
  241. {disabled && <DisabledNotice reason={disabledReason} />}
  242. </DisableWrapper>
  243. )}
  244. </IntegrationFeatures>
  245. );
  246. }
  247. // Returns the content shown in the top section of the integration detail
  248. renderTopSection() {
  249. const tags = this.cleanTags();
  250. return (
  251. <TopSectionWrapper>
  252. <Flex>
  253. {this.renderIntegrationIcon()}
  254. <NameContainer>
  255. <Flex>
  256. <Name>{this.integrationName}</Name>
  257. <StatusWrapper>
  258. {this.installationStatus && (
  259. <IntegrationStatus status={this.installationStatus} />
  260. )}
  261. </StatusWrapper>
  262. </Flex>
  263. <Flex>
  264. {tags.map(feature => (
  265. <StyledTag key={feature}>{startCase(feature)}</StyledTag>
  266. ))}
  267. </Flex>
  268. </NameContainer>
  269. </Flex>
  270. <Flex>
  271. {this.renderAddInstallButton()}
  272. {this.renderAdditionalCTA()}
  273. </Flex>
  274. </TopSectionWrapper>
  275. );
  276. }
  277. // Returns the tabs divider with the clickable tabs
  278. renderTabs() {
  279. // TODO: Convert to styled component
  280. return (
  281. <ul className="nav nav-tabs border-bottom" style={{paddingTop: '30px'}}>
  282. {this.tabs.map(tabName => (
  283. <li
  284. key={tabName}
  285. className={this.state.tab === tabName ? 'active' : ''}
  286. onClick={() => this.onTabChange(tabName)}
  287. >
  288. <CapitalizedLink>{this.getTabDisplay(tabName)}</CapitalizedLink>
  289. </li>
  290. ))}
  291. </ul>
  292. );
  293. }
  294. // Returns the information about the integration description and features
  295. renderInformationCard() {
  296. const {FeatureList} = getIntegrationFeatureGate();
  297. return (
  298. <Fragment>
  299. <Flex>
  300. <FlexContainer>
  301. <Description dangerouslySetInnerHTML={{__html: marked(this.description)}} />
  302. <FeatureList
  303. {...this.featureProps}
  304. provider={{key: this.props.params.integrationSlug}}
  305. />
  306. {this.renderPermissions()}
  307. {this.alerts.map((alert, i) => (
  308. <Alert key={i} type={alert.type} showIcon>
  309. <span
  310. dangerouslySetInnerHTML={{__html: singleLineRenderer(alert.text)}}
  311. />
  312. </Alert>
  313. ))}
  314. </FlexContainer>
  315. <Metadata>
  316. {!!this.author && (
  317. <AuthorInfo>
  318. <CreatedContainer>{t('Created By')}</CreatedContainer>
  319. <div>{this.author}</div>
  320. </AuthorInfo>
  321. )}
  322. {this.resourceLinks.map(({title, url}) => (
  323. <ExternalLinkContainer key={url}>
  324. {this.getIcon(title)}
  325. <ExternalLink href={url}>{title}</ExternalLink>
  326. </ExternalLinkContainer>
  327. ))}
  328. </Metadata>
  329. </Flex>
  330. </Fragment>
  331. );
  332. }
  333. renderBody() {
  334. return (
  335. <Fragment>
  336. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  337. {this.renderAlert()}
  338. {this.renderTopSection()}
  339. {this.renderTabs()}
  340. {this.state.tab === 'overview'
  341. ? this.renderInformationCard()
  342. : this.renderConfigurations()}
  343. </Fragment>
  344. );
  345. }
  346. }
  347. const Flex = styled('div')`
  348. display: flex;
  349. align-items: center;
  350. `;
  351. const FlexContainer = styled('div')`
  352. flex: 1;
  353. `;
  354. const CapitalizedLink = styled('a')`
  355. text-transform: capitalize;
  356. `;
  357. const StyledTag = styled(Tag)`
  358. text-transform: none;
  359. &:not(:first-child) {
  360. margin-left: ${space(0.5)};
  361. }
  362. `;
  363. const NameContainer = styled('div')`
  364. display: flex;
  365. align-items: flex-start;
  366. flex-direction: column;
  367. justify-content: center;
  368. padding-left: ${space(2)};
  369. `;
  370. const Name = styled('div')`
  371. font-weight: ${p => p.theme.fontWeightBold};
  372. font-size: 1.4em;
  373. margin-bottom: ${space(0.5)};
  374. `;
  375. const IconCloseCircle = styled(IconClose)`
  376. color: ${p => p.theme.dangerText};
  377. margin-right: ${space(1)};
  378. `;
  379. export const DisabledNotice = styled(({reason, ...p}: {reason: React.ReactNode}) => (
  380. <div
  381. style={{
  382. display: 'flex',
  383. alignItems: 'center',
  384. }}
  385. {...p}
  386. >
  387. <IconCloseCircle isCircled />
  388. <span>{reason}</span>
  389. </div>
  390. ))`
  391. padding-top: ${space(0.5)};
  392. font-size: 0.9em;
  393. `;
  394. const FeatureListItem = styled('span')`
  395. line-height: 24px;
  396. `;
  397. const Description = styled('div')`
  398. li {
  399. margin-bottom: 6px;
  400. }
  401. `;
  402. const Metadata = styled(Flex)`
  403. display: grid;
  404. grid-auto-rows: max-content;
  405. grid-auto-flow: row;
  406. gap: ${space(1)};
  407. font-size: 0.9em;
  408. margin-left: ${space(4)};
  409. margin-right: 100px;
  410. align-self: flex-start;
  411. `;
  412. const AuthorInfo = styled('div')`
  413. margin-bottom: ${space(3)};
  414. `;
  415. const ExternalLinkContainer = styled('div')`
  416. display: grid;
  417. grid-template-columns: max-content 1fr;
  418. gap: ${space(1)};
  419. align-items: center;
  420. `;
  421. const StatusWrapper = styled('div')`
  422. margin-bottom: ${space(0.5)};
  423. padding-left: ${space(2)};
  424. `;
  425. const DisableWrapper = styled('div')`
  426. margin-left: auto;
  427. align-self: center;
  428. display: flex;
  429. flex-direction: column;
  430. align-items: center;
  431. `;
  432. const CreatedContainer = styled('div')`
  433. text-transform: uppercase;
  434. padding-bottom: ${space(1)};
  435. color: ${p => p.theme.gray300};
  436. font-weight: ${p => p.theme.fontWeightBold};
  437. font-size: 12px;
  438. `;
  439. const TopSectionWrapper = styled('div')`
  440. display: flex;
  441. justify-content: space-between;
  442. `;
  443. export default AbstractIntegrationDetailedView;