abstractIntegrationDetailedView.tsx 14 KB

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