abstractIntegrationDetailedView.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 ExternalLink from 'sentry/components/links/externalLink';
  9. import {Panel} from 'sentry/components/panels';
  10. import Tag from 'sentry/components/tag';
  11. import Tooltip from 'sentry/components/tooltip';
  12. import {IconClose, IconDocs, IconGeneric, IconGithub, IconProject} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  15. import space from 'sentry/styles/space';
  16. import {
  17. IntegrationFeature,
  18. IntegrationInstallationStatus,
  19. IntegrationType,
  20. Organization,
  21. } from 'sentry/types';
  22. import {
  23. IntegrationAnalyticsKey,
  24. IntegrationEventParameters,
  25. } from 'sentry/utils/analytics/integrations';
  26. import {
  27. getCategories,
  28. getIntegrationFeatureGate,
  29. trackIntegrationAnalytics,
  30. } from 'sentry/utils/integrationUtil';
  31. import marked, {singleLineRenderer} from 'sentry/utils/marked';
  32. import EmptyMessage from 'sentry/views/settings/components/emptyMessage';
  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; orgId: 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. // Returns an array of RawIntegrationFeatures which is used in feature gating
  97. // and displaying what the integration does
  98. get featureData(): IntegrationFeature[] {
  99. // Allow children to implement this
  100. throw new Error('Not implemented');
  101. }
  102. getIcon(title: string) {
  103. switch (title) {
  104. case 'View Source':
  105. return <IconProject />;
  106. case 'Report Issue':
  107. return <IconGithub />;
  108. case 'Documentation':
  109. case 'Splunk Setup Instructions':
  110. case 'Trello Setup Instructions':
  111. return <IconDocs />;
  112. default:
  113. return <IconGeneric />;
  114. }
  115. }
  116. onTabChange = (value: Tab) => {
  117. this.trackIntegrationAnalytics('integrations.integration_tab_clicked', {
  118. integration_tab: value,
  119. });
  120. this.setState({tab: value});
  121. };
  122. // Returns the string that is shown as the title of a tab
  123. getTabDisplay(tab: Tab): string {
  124. // default is return the tab
  125. return tab;
  126. }
  127. // Render the button at the top which is usually just an installation button
  128. renderTopButton(
  129. _disabledFromFeatures: boolean, // from the feature gate
  130. _userHasAccess: boolean // from user permissions
  131. ): React.ReactElement {
  132. // Allow children to implement this
  133. throw new Error('Not implemented');
  134. }
  135. // Returns the permission descriptions, only use by Sentry Apps
  136. renderPermissions(): React.ReactElement | null {
  137. // default is don't render permissions
  138. return null;
  139. }
  140. renderEmptyConfigurations() {
  141. return (
  142. <Panel>
  143. <EmptyMessage
  144. title={t("You haven't set anything up yet")}
  145. description={t(
  146. 'But that doesn’t have to be the case for long! Add an installation to get started.'
  147. )}
  148. action={this.renderAddInstallButton(true)}
  149. />
  150. </Panel>
  151. );
  152. }
  153. // Returns the list of configurations for the integration
  154. renderConfigurations() {
  155. // Allow children to implement this
  156. throw new Error('Not implemented');
  157. }
  158. /**
  159. * Actually implemented methods below
  160. */
  161. get integrationSlug() {
  162. return this.props.params.integrationSlug;
  163. }
  164. // Wrapper around trackIntegrationAnalytics that automatically provides many fields and the org
  165. trackIntegrationAnalytics = <T extends IntegrationAnalyticsKey>(
  166. eventKey: IntegrationAnalyticsKey,
  167. options?: Partial<IntegrationEventParameters[T]>
  168. ) => {
  169. options = options || {};
  170. // If we use this intermediate type we get type checking on the things we care about
  171. const params = {
  172. view: 'integrations_directory_integration_detail',
  173. integration: this.integrationSlug,
  174. integration_type: this.integrationType,
  175. already_installed: this.installationStatus !== 'Not Installed', // pending counts as installed here
  176. organization: this.props.organization,
  177. ...options,
  178. };
  179. trackIntegrationAnalytics(eventKey, params);
  180. };
  181. // Returns the props as needed by the hooks integrations:feature-gates
  182. get featureProps() {
  183. const {organization} = this.props;
  184. const featureData = this.featureData;
  185. // Prepare the features list
  186. const features = featureData.map(f => ({
  187. featureGate: f.featureGate,
  188. description: (
  189. <FeatureListItem
  190. dangerouslySetInnerHTML={{__html: singleLineRenderer(f.description)}}
  191. />
  192. ),
  193. }));
  194. return {organization, features};
  195. }
  196. cleanTags() {
  197. return getCategories(this.featureData);
  198. }
  199. renderAlert(): React.ReactNode {
  200. return null;
  201. }
  202. renderAdditionalCTA(): React.ReactNode {
  203. return null;
  204. }
  205. renderIntegrationIcon() {
  206. return <PluginIcon pluginId={this.integrationSlug} size={50} />;
  207. }
  208. renderRequestIntegrationButton() {
  209. return (
  210. <RequestIntegrationButton
  211. organization={this.props.organization}
  212. name={this.integrationName}
  213. slug={this.integrationSlug}
  214. type={this.integrationType}
  215. />
  216. );
  217. }
  218. renderAddInstallButton(hideButtonIfDisabled = false) {
  219. const {organization} = this.props;
  220. const {IntegrationFeatures} = getIntegrationFeatureGate();
  221. return (
  222. <IntegrationFeatures {...this.featureProps}>
  223. {({disabled, disabledReason}) => (
  224. <DisableWrapper>
  225. <Access organization={organization} 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}
  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>{t(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}>{t(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: bold;
  372. font-size: 1.4em;
  373. margin-bottom: ${space(0.5)};
  374. `;
  375. const IconCloseCircle = styled(IconClose)`
  376. color: ${p => p.theme.red300};
  377. margin-right: ${space(1)};
  378. `;
  379. 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: 600;
  437. font-size: 12px;
  438. `;
  439. const TopSectionWrapper = styled('div')`
  440. display: flex;
  441. justify-content: space-between;
  442. `;
  443. export default AbstractIntegrationDetailedView;