abstractIntegrationDetailedView.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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; 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. // 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. const params = {
  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. trackIntegrationAnalytics(eventKey, params);
  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 {organization} = this.props;
  225. const {IntegrationFeatures} = getIntegrationFeatureGate();
  226. return (
  227. <IntegrationFeatures {...this.featureProps}>
  228. {({disabled, disabledReason}) => (
  229. <DisableWrapper>
  230. <Access organization={organization} access={['org:integrations']}>
  231. {({hasAccess}) => (
  232. <Tooltip
  233. title={t(
  234. 'You must be an organization owner, manager or admin to install this.'
  235. )}
  236. disabled={hasAccess || !this.requiresAccess}
  237. >
  238. {!hideButtonIfDisabled && disabled ? (
  239. <div />
  240. ) : (
  241. this.renderTopButton(disabled, hasAccess)
  242. )}
  243. </Tooltip>
  244. )}
  245. </Access>
  246. {disabled && <DisabledNotice reason={disabledReason} />}
  247. </DisableWrapper>
  248. )}
  249. </IntegrationFeatures>
  250. );
  251. }
  252. // Returns the content shown in the top section of the integration detail
  253. renderTopSection() {
  254. const tags = this.cleanTags();
  255. return (
  256. <TopSectionWrapper>
  257. <Flex>
  258. {this.renderIntegrationIcon()}
  259. <NameContainer>
  260. <Flex>
  261. <Name>{this.integrationName}</Name>
  262. <StatusWrapper>
  263. {this.installationStatus && (
  264. <IntegrationStatus status={this.installationStatus} />
  265. )}
  266. </StatusWrapper>
  267. </Flex>
  268. <Flex>
  269. {tags.map(feature => (
  270. <StyledTag key={feature}>{startCase(feature)}</StyledTag>
  271. ))}
  272. </Flex>
  273. </NameContainer>
  274. </Flex>
  275. <Flex>
  276. {this.renderAddInstallButton()}
  277. {this.renderAdditionalCTA()}
  278. </Flex>
  279. </TopSectionWrapper>
  280. );
  281. }
  282. // Returns the tabs divider with the clickable tabs
  283. renderTabs() {
  284. // TODO: Convert to styled component
  285. return (
  286. <ul className="nav nav-tabs border-bottom" style={{paddingTop: '30px'}}>
  287. {this.tabs.map(tabName => (
  288. <li
  289. key={tabName}
  290. className={this.state.tab === tabName ? 'active' : ''}
  291. onClick={() => this.onTabChange(tabName)}
  292. >
  293. <CapitalizedLink>{t(this.getTabDisplay(tabName))}</CapitalizedLink>
  294. </li>
  295. ))}
  296. </ul>
  297. );
  298. }
  299. // Returns the information about the integration description and features
  300. renderInformationCard() {
  301. const {FeatureList} = getIntegrationFeatureGate();
  302. return (
  303. <Fragment>
  304. <Flex>
  305. <FlexContainer>
  306. <Description dangerouslySetInnerHTML={{__html: marked(this.description)}} />
  307. <FeatureList
  308. {...this.featureProps}
  309. provider={{key: this.props.params.integrationSlug}}
  310. />
  311. {this.renderPermissions()}
  312. {this.alerts.map((alert, i) => (
  313. <Alert key={i} type={alert.type} showIcon>
  314. <span
  315. dangerouslySetInnerHTML={{__html: singleLineRenderer(alert.text)}}
  316. />
  317. </Alert>
  318. ))}
  319. </FlexContainer>
  320. <Metadata>
  321. {!!this.author && (
  322. <AuthorInfo>
  323. <CreatedContainer>{t('Created By')}</CreatedContainer>
  324. <div>{this.author}</div>
  325. </AuthorInfo>
  326. )}
  327. {this.resourceLinks.map(({title, url}) => (
  328. <ExternalLinkContainer key={url}>
  329. {this.getIcon(title)}
  330. <ExternalLink href={url}>{t(title)}</ExternalLink>
  331. </ExternalLinkContainer>
  332. ))}
  333. </Metadata>
  334. </Flex>
  335. </Fragment>
  336. );
  337. }
  338. renderBody() {
  339. return (
  340. <Fragment>
  341. <BreadcrumbTitle routes={this.props.routes} title={this.integrationName} />
  342. {this.renderAlert()}
  343. {this.renderTopSection()}
  344. {this.renderTabs()}
  345. {this.state.tab === 'overview'
  346. ? this.renderInformationCard()
  347. : this.renderConfigurations()}
  348. </Fragment>
  349. );
  350. }
  351. }
  352. const Flex = styled('div')`
  353. display: flex;
  354. align-items: center;
  355. `;
  356. const FlexContainer = styled('div')`
  357. flex: 1;
  358. `;
  359. const CapitalizedLink = styled('a')`
  360. text-transform: capitalize;
  361. `;
  362. const StyledTag = styled(Tag)`
  363. text-transform: none;
  364. &:not(:first-child) {
  365. margin-left: ${space(0.5)};
  366. }
  367. `;
  368. const NameContainer = styled('div')`
  369. display: flex;
  370. align-items: flex-start;
  371. flex-direction: column;
  372. justify-content: center;
  373. padding-left: ${space(2)};
  374. `;
  375. const Name = styled('div')`
  376. font-weight: bold;
  377. font-size: 1.4em;
  378. margin-bottom: ${space(0.5)};
  379. `;
  380. const IconCloseCircle = styled(IconClose)`
  381. color: ${p => p.theme.red300};
  382. margin-right: ${space(1)};
  383. `;
  384. const DisabledNotice = styled(({reason, ...p}: {reason: React.ReactNode}) => (
  385. <div
  386. style={{
  387. display: 'flex',
  388. alignItems: 'center',
  389. }}
  390. {...p}
  391. >
  392. <IconCloseCircle isCircled />
  393. <span>{reason}</span>
  394. </div>
  395. ))`
  396. padding-top: ${space(0.5)};
  397. font-size: 0.9em;
  398. `;
  399. const FeatureListItem = styled('span')`
  400. line-height: 24px;
  401. `;
  402. const Description = styled('div')`
  403. li {
  404. margin-bottom: 6px;
  405. }
  406. `;
  407. const Metadata = styled(Flex)`
  408. display: grid;
  409. grid-auto-rows: max-content;
  410. grid-auto-flow: row;
  411. gap: ${space(1)};
  412. font-size: 0.9em;
  413. margin-left: ${space(4)};
  414. margin-right: 100px;
  415. align-self: flex-start;
  416. `;
  417. const AuthorInfo = styled('div')`
  418. margin-bottom: ${space(3)};
  419. `;
  420. const ExternalLinkContainer = styled('div')`
  421. display: grid;
  422. grid-template-columns: max-content 1fr;
  423. gap: ${space(1)};
  424. align-items: center;
  425. `;
  426. const StatusWrapper = styled('div')`
  427. margin-bottom: ${space(0.5)};
  428. padding-left: ${space(2)};
  429. `;
  430. const DisableWrapper = styled('div')`
  431. margin-left: auto;
  432. align-self: center;
  433. display: flex;
  434. flex-direction: column;
  435. align-items: center;
  436. `;
  437. const CreatedContainer = styled('div')`
  438. text-transform: uppercase;
  439. padding-bottom: ${space(1)};
  440. color: ${p => p.theme.gray300};
  441. font-weight: 600;
  442. font-size: 12px;
  443. `;
  444. const TopSectionWrapper = styled('div')`
  445. display: flex;
  446. justify-content: space-between;
  447. `;
  448. export default AbstractIntegrationDetailedView;