abstractIntegrationDetailedView.tsx 14 KB

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