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} from 'sentry/components/alert';
  7. import AsyncComponent from 'sentry/components/asyncComponent';
  8. import EmptyMessage from 'sentry/components/emptyMessage';
  9. import ExternalLink from 'sentry/components/links/externalLink';
  10. import {Panel} from 'sentry/components/panels';
  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. type Tab = 'overview' | 'configurations';
  37. type AlertType = React.ComponentProps<typeof Alert> & {
  38. text: string;
  39. };
  40. type State = {
  41. tab: Tab;
  42. } & AsyncComponent['state'];
  43. type Props = {
  44. organization: Organization;
  45. } & RouteComponentProps<{integrationSlug: string}, {}> &
  46. AsyncComponent['props'];
  47. class AbstractIntegrationDetailedView<
  48. P extends Props = Props,
  49. S extends State = State
  50. > extends AsyncComponent<P, S> {
  51. tabs: Tab[] = ['overview', 'configurations'];
  52. componentDidMount() {
  53. const {location} = this.props;
  54. const value = location.query.tab === 'configurations' ? 'configurations' : 'overview';
  55. // eslint-disable-next-line react/no-did-mount-set-state
  56. this.setState({tab: value});
  57. }
  58. onLoadAllEndpointsSuccess() {
  59. this.trackIntegrationAnalytics('integrations.integration_viewed', {
  60. integration_tab: this.state.tab,
  61. });
  62. }
  63. /**
  64. * Abstract methods defined below
  65. */
  66. // The analytics type used in analytics which is snake case
  67. get integrationType(): IntegrationType {
  68. // Allow children to implement this
  69. throw new Error('Not implemented');
  70. }
  71. get description(): string {
  72. // Allow children to implement this
  73. throw new Error('Not implemented');
  74. }
  75. get author(): string | undefined {
  76. // Allow children to implement this
  77. throw new Error('Not implemented');
  78. }
  79. get alerts(): AlertType[] {
  80. // default is no alerts
  81. return [];
  82. }
  83. // Returns a list of the resources displayed at the bottom of the overview card
  84. get resourceLinks(): Array<{title: string; url: string}> {
  85. // Allow children to implement this
  86. throw new Error('Not implemented');
  87. }
  88. get installationStatus(): IntegrationInstallationStatus | null {
  89. // Allow children to implement this
  90. throw new Error('Not implemented');
  91. }
  92. get integrationName(): string {
  93. // Allow children to implement this
  94. throw new Error('Not implemented');
  95. }
  96. // Checks to see if integration requires admin access to install, doc integrations don't
  97. get requiresAccess(): boolean {
  98. // default is integration requires access to install
  99. return true;
  100. }
  101. // Returns an array of RawIntegrationFeatures which is used in feature gating
  102. // and displaying what the integration does
  103. get featureData(): IntegrationFeature[] {
  104. // Allow children to implement this
  105. throw new Error('Not implemented');
  106. }
  107. getIcon(title: string) {
  108. switch (title) {
  109. case 'View Source':
  110. return <IconProject />;
  111. case 'Report Issue':
  112. return <IconGithub />;
  113. case 'Documentation':
  114. case 'Splunk Setup Instructions':
  115. case 'Trello Setup Instructions':
  116. return <IconDocs />;
  117. default:
  118. return <IconGeneric />;
  119. }
  120. }
  121. onTabChange = (value: Tab) => {
  122. this.trackIntegrationAnalytics('integrations.integration_tab_clicked', {
  123. integration_tab: value,
  124. });
  125. this.setState({tab: value});
  126. };
  127. // Returns the string that is shown as the title of a tab
  128. getTabDisplay(tab: Tab): string {
  129. // default is return the tab
  130. return tab;
  131. }
  132. // Render the button at the top which is usually just an installation button
  133. renderTopButton(
  134. _disabledFromFeatures: boolean, // from the feature gate
  135. _userHasAccess: boolean // from user permissions
  136. ): React.ReactElement {
  137. // Allow children to implement this
  138. throw new Error('Not implemented');
  139. }
  140. // Returns the permission descriptions, only use by Sentry Apps
  141. renderPermissions(): React.ReactElement | null {
  142. // default is don't render permissions
  143. return null;
  144. }
  145. renderEmptyConfigurations() {
  146. return (
  147. <Panel>
  148. <EmptyMessage
  149. title={t("You haven't set anything up yet")}
  150. description={t(
  151. 'But that doesn’t have to be the case for long! Add an installation to get started.'
  152. )}
  153. action={this.renderAddInstallButton(true)}
  154. />
  155. </Panel>
  156. );
  157. }
  158. // Returns the list of configurations for the integration
  159. renderConfigurations() {
  160. // Allow children to implement this
  161. throw new Error('Not implemented');
  162. }
  163. /**
  164. * Actually implemented methods below
  165. */
  166. get integrationSlug() {
  167. return this.props.params.integrationSlug;
  168. }
  169. // Wrapper around trackIntegrationAnalytics that automatically provides many fields and the org
  170. trackIntegrationAnalytics = <T extends IntegrationAnalyticsKey>(
  171. eventKey: IntegrationAnalyticsKey,
  172. options?: Partial<IntegrationEventParameters[T]>
  173. ) => {
  174. options = options || {};
  175. // If we use this intermediate type we get type checking on the things we care about
  176. trackIntegrationAnalytics(eventKey, {
  177. view: 'integrations_directory_integration_detail',
  178. integration: this.integrationSlug,
  179. integration_type: this.integrationType,
  180. already_installed: this.installationStatus !== 'Not Installed', // pending counts as installed here
  181. organization: this.props.organization,
  182. ...options,
  183. });
  184. };
  185. // Returns the props as needed by the hooks integrations:feature-gates
  186. get featureProps() {
  187. const {organization} = this.props;
  188. const featureData = this.featureData;
  189. // Prepare the features list
  190. const features = featureData.map(f => ({
  191. featureGate: f.featureGate,
  192. description: (
  193. <FeatureListItem
  194. dangerouslySetInnerHTML={{__html: singleLineRenderer(f.description)}}
  195. />
  196. ),
  197. }));
  198. return {organization, features};
  199. }
  200. cleanTags() {
  201. return getCategories(this.featureData);
  202. }
  203. renderAlert(): React.ReactNode {
  204. return null;
  205. }
  206. renderAdditionalCTA(): React.ReactNode {
  207. return null;
  208. }
  209. renderIntegrationIcon() {
  210. return <PluginIcon pluginId={this.integrationSlug} size={50} />;
  211. }
  212. renderRequestIntegrationButton() {
  213. return (
  214. <RequestIntegrationButton
  215. organization={this.props.organization}
  216. name={this.integrationName}
  217. slug={this.integrationSlug}
  218. type={this.integrationType}
  219. />
  220. );
  221. }
  222. renderAddInstallButton(hideButtonIfDisabled = false) {
  223. const {organization} = this.props;
  224. const {IntegrationFeatures} = getIntegrationFeatureGate();
  225. return (
  226. <IntegrationFeatures {...this.featureProps}>
  227. {({disabled, disabledReason}) => (
  228. <DisableWrapper>
  229. <Access organization={organization} 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;