abstractIntegrationDetailedView.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import startCase from 'lodash/startCase';
  5. import Access from 'sentry/components/acl/access';
  6. import {Alert, AlertProps} from 'sentry/components/alert';
  7. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import Panel from 'sentry/components/panels/panel';
  11. import Tag from 'sentry/components/tag';
  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 {
  18. IntegrationFeature,
  19. IntegrationInstallationStatus,
  20. IntegrationType,
  21. Organization,
  22. } from 'sentry/types';
  23. import {
  24. IntegrationAnalyticsKey,
  25. IntegrationEventParameters,
  26. } from 'sentry/utils/analytics/integrations';
  27. import {
  28. getCategories,
  29. getIntegrationFeatureGate,
  30. trackIntegrationAnalytics,
  31. } from 'sentry/utils/integrationUtil';
  32. import marked, {singleLineRenderer} from 'sentry/utils/marked';
  33. import BreadcrumbTitle from 'sentry/views/settings/components/settingsBreadcrumb/breadcrumbTitle';
  34. import RequestIntegrationButton from './integrationRequest/RequestIntegrationButton';
  35. import IntegrationStatus from './integrationStatus';
  36. export type Tab = 'overview' | 'configurations' | 'features';
  37. interface AlertType extends AlertProps {
  38. text: string;
  39. }
  40. type State = {
  41. tab: Tab;
  42. } & DeprecatedAsyncComponent['state'];
  43. type Props = {
  44. organization: Organization;
  45. } & RouteComponentProps<{integrationSlug: string}, {}> &
  46. DeprecatedAsyncComponent['props'];
  47. class AbstractIntegrationDetailedView<
  48. P extends Props = Props,
  49. S extends State = State,
  50. > extends DeprecatedAsyncComponent<P, S> {
  51. tabs: Tab[] = ['overview', 'configurations'];
  52. componentDidMount() {
  53. super.componentDidMount();
  54. const {location} = this.props;
  55. const value = location.query.tab === 'configurations' ? 'configurations' : 'overview';
  56. // eslint-disable-next-line react/no-did-mount-set-state
  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. renderConfigurations() {
  161. // Allow children to implement this
  162. throw new Error('Not implemented');
  163. }
  164. /**
  165. * Actually implemented methods below
  166. */
  167. get integrationSlug() {
  168. return this.props.params.integrationSlug;
  169. }
  170. // Wrapper around trackIntegrationAnalytics that automatically provides many fields and the org
  171. trackIntegrationAnalytics = <T extends IntegrationAnalyticsKey>(
  172. eventKey: IntegrationAnalyticsKey,
  173. options?: Partial<IntegrationEventParameters[T]>
  174. ) => {
  175. options = options || {};
  176. // If we use this intermediate type we get type checking on the things we care about
  177. trackIntegrationAnalytics(eventKey, {
  178. view: 'integrations_directory_integration_detail',
  179. integration: this.integrationSlug,
  180. integration_type: this.integrationType,
  181. already_installed: this.installationStatus !== 'Not Installed', // pending counts as installed here
  182. organization: this.props.organization,
  183. ...options,
  184. });
  185. };
  186. // Returns the props as needed by the hooks integrations:feature-gates
  187. get featureProps() {
  188. const {organization} = this.props;
  189. const featureData = this.featureData;
  190. // Prepare the features list
  191. const features = featureData.map(f => ({
  192. featureGate: f.featureGate,
  193. description: (
  194. <FeatureListItem
  195. dangerouslySetInnerHTML={{__html: singleLineRenderer(f.description)}}
  196. />
  197. ),
  198. }));
  199. return {organization, features};
  200. }
  201. cleanTags() {
  202. return getCategories(this.featureData);
  203. }
  204. renderAlert(): React.ReactNode {
  205. return null;
  206. }
  207. renderAdditionalCTA(): React.ReactNode {
  208. return null;
  209. }
  210. renderIntegrationIcon() {
  211. return <PluginIcon pluginId={this.integrationSlug} size={50} />;
  212. }
  213. renderRequestIntegrationButton() {
  214. return (
  215. <RequestIntegrationButton
  216. organization={this.props.organization}
  217. name={this.integrationName}
  218. slug={this.integrationSlug}
  219. type={this.integrationType}
  220. />
  221. );
  222. }
  223. renderAddInstallButton(hideButtonIfDisabled = false) {
  224. const {IntegrationFeatures} = getIntegrationFeatureGate();
  225. return (
  226. <IntegrationFeatures {...this.featureProps}>
  227. {({disabled, disabledReason}) => (
  228. <DisableWrapper>
  229. <Access access={['org:integrations']}>
  230. {({hasAccess}) => (
  231. <Tooltip
  232. title={t(
  233. 'You must be an organization owner, manager or admin to install this.'
  234. )}
  235. disabled={hasAccess || !this.requiresAccess}
  236. >
  237. {!hideButtonIfDisabled && disabled ? (
  238. <div />
  239. ) : (
  240. this.renderTopButton(disabled, hasAccess)
  241. )}
  242. </Tooltip>
  243. )}
  244. </Access>
  245. {disabled && <DisabledNotice reason={disabledReason} />}
  246. </DisableWrapper>
  247. )}
  248. </IntegrationFeatures>
  249. );
  250. }
  251. // Returns the content shown in the top section of the integration detail
  252. renderTopSection() {
  253. const tags = this.cleanTags();
  254. return (
  255. <TopSectionWrapper>
  256. <Flex>
  257. {this.renderIntegrationIcon()}
  258. <NameContainer>
  259. <Flex>
  260. <Name>{this.integrationName}</Name>
  261. <StatusWrapper>
  262. {this.installationStatus && (
  263. <IntegrationStatus status={this.installationStatus} />
  264. )}
  265. </StatusWrapper>
  266. </Flex>
  267. <Flex>
  268. {tags.map(feature => (
  269. <StyledTag key={feature}>{startCase(feature)}</StyledTag>
  270. ))}
  271. </Flex>
  272. </NameContainer>
  273. </Flex>
  274. <Flex>
  275. {this.renderAddInstallButton()}
  276. {this.renderAdditionalCTA()}
  277. </Flex>
  278. </TopSectionWrapper>
  279. );
  280. }
  281. // Returns the tabs divider with the clickable tabs
  282. renderTabs() {
  283. // TODO: Convert to styled component
  284. return (
  285. <ul className="nav nav-tabs border-bottom" style={{paddingTop: '30px'}}>
  286. {this.tabs.map(tabName => (
  287. <li
  288. key={tabName}
  289. className={this.state.tab === tabName ? 'active' : ''}
  290. onClick={() => this.onTabChange(tabName)}
  291. >
  292. <CapitalizedLink>{this.getTabDisplay(tabName)}</CapitalizedLink>
  293. </li>
  294. ))}
  295. </ul>
  296. );
  297. }
  298. // Returns the information about the integration description and features
  299. renderInformationCard() {
  300. const {FeatureList} = getIntegrationFeatureGate();
  301. return (
  302. <Fragment>
  303. <Flex>
  304. <FlexContainer>
  305. <Description dangerouslySetInnerHTML={{__html: marked(this.description)}} />
  306. <FeatureList
  307. {...this.featureProps}
  308. provider={{key: this.props.params.integrationSlug}}
  309. />
  310. {this.renderPermissions()}
  311. {this.alerts.map((alert, i) => (
  312. <Alert key={i} type={alert.type} showIcon>
  313. <span
  314. dangerouslySetInnerHTML={{__html: singleLineRenderer(alert.text)}}
  315. />
  316. </Alert>
  317. ))}
  318. </FlexContainer>
  319. <Metadata>
  320. {!!this.author && (
  321. <AuthorInfo>
  322. <CreatedContainer>{t('Created By')}</CreatedContainer>
  323. <div>{this.author}</div>
  324. </AuthorInfo>
  325. )}
  326. {this.resourceLinks.map(({title, url}) => (
  327. <ExternalLinkContainer key={url}>
  328. {this.getIcon(title)}
  329. <ExternalLink href={url}>{title}</ExternalLink>
  330. </ExternalLinkContainer>
  331. ))}
  332. </Metadata>
  333. </Flex>
  334. </Fragment>
  335. );
  336. }
  337. renderBody() {
  338. return (
  339. <Fragment>
  340. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  341. {this.renderAlert()}
  342. {this.renderTopSection()}
  343. {this.renderTabs()}
  344. {this.state.tab === 'overview'
  345. ? this.renderInformationCard()
  346. : this.renderConfigurations()}
  347. </Fragment>
  348. );
  349. }
  350. }
  351. const Flex = styled('div')`
  352. display: flex;
  353. align-items: center;
  354. `;
  355. const FlexContainer = styled('div')`
  356. flex: 1;
  357. `;
  358. const CapitalizedLink = styled('a')`
  359. text-transform: capitalize;
  360. `;
  361. const StyledTag = styled(Tag)`
  362. text-transform: none;
  363. &:not(:first-child) {
  364. margin-left: ${space(0.5)};
  365. }
  366. `;
  367. const NameContainer = styled('div')`
  368. display: flex;
  369. align-items: flex-start;
  370. flex-direction: column;
  371. justify-content: center;
  372. padding-left: ${space(2)};
  373. `;
  374. const Name = styled('div')`
  375. font-weight: bold;
  376. font-size: 1.4em;
  377. margin-bottom: ${space(0.5)};
  378. `;
  379. const IconCloseCircle = styled(IconClose)`
  380. color: ${p => p.theme.dangerText};
  381. margin-right: ${space(1)};
  382. `;
  383. const DisabledNotice = styled(({reason, ...p}: {reason: React.ReactNode}) => (
  384. <div
  385. style={{
  386. display: 'flex',
  387. alignItems: 'center',
  388. }}
  389. {...p}
  390. >
  391. <IconCloseCircle isCircled />
  392. <span>{reason}</span>
  393. </div>
  394. ))`
  395. padding-top: ${space(0.5)};
  396. font-size: 0.9em;
  397. `;
  398. const FeatureListItem = styled('span')`
  399. line-height: 24px;
  400. `;
  401. const Description = styled('div')`
  402. li {
  403. margin-bottom: 6px;
  404. }
  405. `;
  406. const Metadata = styled(Flex)`
  407. display: grid;
  408. grid-auto-rows: max-content;
  409. grid-auto-flow: row;
  410. gap: ${space(1)};
  411. font-size: 0.9em;
  412. margin-left: ${space(4)};
  413. margin-right: 100px;
  414. align-self: flex-start;
  415. `;
  416. const AuthorInfo = styled('div')`
  417. margin-bottom: ${space(3)};
  418. `;
  419. const ExternalLinkContainer = styled('div')`
  420. display: grid;
  421. grid-template-columns: max-content 1fr;
  422. gap: ${space(1)};
  423. align-items: center;
  424. `;
  425. const StatusWrapper = styled('div')`
  426. margin-bottom: ${space(0.5)};
  427. padding-left: ${space(2)};
  428. `;
  429. const DisableWrapper = styled('div')`
  430. margin-left: auto;
  431. align-self: center;
  432. display: flex;
  433. flex-direction: column;
  434. align-items: center;
  435. `;
  436. const CreatedContainer = styled('div')`
  437. text-transform: uppercase;
  438. padding-bottom: ${space(1)};
  439. color: ${p => p.theme.gray300};
  440. font-weight: 600;
  441. font-size: 12px;
  442. `;
  443. const TopSectionWrapper = styled('div')`
  444. display: flex;
  445. justify-content: space-between;
  446. `;
  447. export default AbstractIntegrationDetailedView;