hooks.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import React from 'react';
  2. import {Route, RouteComponentProps} from 'react-router';
  3. import {ChildrenRenderFn} from 'app/components/acl/feature';
  4. import DateRange from 'app/components/organizations/timeRangeSelector/dateRange';
  5. import SelectorItems from 'app/components/organizations/timeRangeSelector/dateRange/selectorItems';
  6. import SidebarItem from 'app/components/sidebar/sidebarItem';
  7. import {
  8. IntegrationProvider,
  9. LightWeightOrganization,
  10. Member,
  11. Organization,
  12. Project,
  13. User,
  14. } from 'app/types';
  15. import {ExperimentKey} from 'app/types/experiments';
  16. import {NavigationItem, NavigationSection} from 'app/views/settings/types';
  17. // XXX(epurkhiser): A Note about `_`.
  18. //
  19. // We add the `_: any` type int our hooks list to stop
  20. // typescript from doing too much type tightening. We should absolutely revisit
  21. // this in the future because all callbacks _should_ be allowed to be
  22. // functions, but doing so causes some unexpected issues and makes typescript
  23. // not happy. We still get a huge advantage of typing just by having each hook
  24. // type here however.
  25. /**
  26. * The Hooks type mapping is the master interface for all external Hooks into
  27. * the sentry frontend application.
  28. */
  29. export type Hooks = {_: any} & RouteHooks &
  30. ComponentHooks &
  31. CustomizationHooks &
  32. AnalyticsHooks &
  33. FeatureDisabledHooks &
  34. InterfaceChromeHooks &
  35. OnboardingHooks &
  36. SettingsHooks;
  37. export type HookName = keyof Hooks;
  38. /**
  39. * Route hooks.
  40. */
  41. export type RouteHooks = {
  42. routes: RoutesHook;
  43. 'routes:admin': RoutesHook;
  44. 'routes:api': RoutesHook;
  45. 'routes:organization': RoutesHook;
  46. 'routes:organization-root': RoutesHook;
  47. };
  48. /**
  49. * Component specific hooks for DateRange and SelectorItems
  50. * These components have plan specific overrides in getsentry
  51. */
  52. type DateRangeProps = React.ComponentProps<typeof DateRange>;
  53. type SelectorItemsProps = React.ComponentProps<typeof SelectorItems>;
  54. type GlobalNotificationProps = {className: string; organization?: Organization};
  55. type DisabledMemberViewProps = RouteComponentProps<{orgId: string}, {}>;
  56. type MemberListHeaderProps = {
  57. members: Member[];
  58. organization: Organization;
  59. };
  60. type DisabledAppStoreConnectItem = {
  61. disabled: boolean;
  62. onTrialStarted: () => void;
  63. children: React.ReactElement;
  64. };
  65. type DisabledMemberTooltipProps = {children: React.ReactNode};
  66. type DashboardHeadersProps = {organization: Organization};
  67. type CodeOwnersHeaderProps = {
  68. addCodeOwner: () => void;
  69. handleRequest: () => void;
  70. };
  71. /**
  72. * Component wrapping hooks
  73. */
  74. export type ComponentHooks = {
  75. 'component:header-date-range': () => React.ComponentType<DateRangeProps>;
  76. 'component:header-selector-items': () => React.ComponentType<SelectorItemsProps>;
  77. 'component:global-notifications': () => React.ComponentType<GlobalNotificationProps>;
  78. 'component:disabled-member': () => React.ComponentType<DisabledMemberViewProps>;
  79. 'component:member-list-header': () => React.ComponentType<MemberListHeaderProps>;
  80. 'component:codeowners-header': () => React.ComponentType<CodeOwnersHeaderProps>;
  81. 'component:disabled-member-tooltip': () => React.ComponentType<DisabledMemberTooltipProps>;
  82. 'component:disabled-app-store-connect-item': () => React.ComponentType<DisabledAppStoreConnectItem>;
  83. 'component:dashboards-header': () => React.ComponentType<DashboardHeadersProps>;
  84. };
  85. /**
  86. * Customization hooks are advanced hooks that return render-prop style
  87. * components the allow for specific customizations of components.
  88. *
  89. * These are very similar to the component wrapping hooks
  90. */
  91. export type CustomizationHooks = {
  92. 'integrations:feature-gates': IntegrationsFeatureGatesHook;
  93. 'member-invite-modal:customization': InviteModalCustomizationHook;
  94. };
  95. /**
  96. * Analytics / tracking / and operational metrics backend hooks.
  97. */
  98. export type AnalyticsHooks = {
  99. 'analytics:init-user': AnalyticsInitUser;
  100. 'analytics:track-event': AnalyticsTrackEvent;
  101. 'analytics:track-event-v2': AnalyticsTrackEventV2;
  102. 'analytics:track-adhoc-event': AnalyticsTrackAdhocEvent;
  103. 'analytics:log-experiment': AnalyticsLogExperiment;
  104. 'metrics:event': MetricsEvent;
  105. // TODO(epurkhiser): This is deprecated and should be replaced
  106. 'analytics:event': LegacyAnalyticsEvent;
  107. };
  108. /**
  109. * feature-disabled:<feature-flag> hooks return components that will be
  110. * rendered in place for Feature components when the feature is not enabled.
  111. */
  112. export type FeatureDisabledHooks = {
  113. 'feature-disabled:alerts-page': FeatureDisabledHook;
  114. 'feature-disabled:configure-distributed-tracing': FeatureDisabledHook;
  115. 'feature-disabled:custom-inbound-filters': FeatureDisabledHook;
  116. 'feature-disabled:custom-symbol-sources': FeatureDisabledHook;
  117. 'feature-disabled:data-forwarding': FeatureDisabledHook;
  118. 'feature-disabled:discard-groups': FeatureDisabledHook;
  119. 'feature-disabled:discover-page': FeatureDisabledHook;
  120. 'feature-disabled:discover-saved-query-create': FeatureDisabledHook;
  121. 'feature-disabled:discover-sidebar-item': FeatureDisabledHook;
  122. 'feature-disabled:discover2-page': FeatureDisabledHook;
  123. 'feature-disabled:discover2-sidebar-item': FeatureDisabledHook;
  124. 'feature-disabled:open-in-discover': FeatureDisabledHook;
  125. 'feature-disabled:events-page': FeatureDisabledHook;
  126. 'feature-disabled:events-sidebar-item': FeatureDisabledHook;
  127. 'feature-disabled:grid-editable-actions': FeatureDisabledHook;
  128. 'feature-disabled:open-discover': FeatureDisabledHook;
  129. 'feature-disabled:dashboards-edit': FeatureDisabledHook;
  130. 'feature-disabled:dashboards-page': FeatureDisabledHook;
  131. 'feature-disabled:dashboards-sidebar-item': FeatureDisabledHook;
  132. 'feature-disabled:incidents-sidebar-item': FeatureDisabledHook;
  133. 'feature-disabled:performance-new-project': FeatureDisabledHook;
  134. 'feature-disabled:performance-page': FeatureDisabledHook;
  135. 'feature-disabled:performance-quick-trace': FeatureDisabledHook;
  136. 'feature-disabled:performance-sidebar-item': FeatureDisabledHook;
  137. 'feature-disabled:project-performance-score-card': FeatureDisabledHook;
  138. 'feature-disabled:project-selector-checkbox': FeatureDisabledHook;
  139. 'feature-disabled:rate-limits': FeatureDisabledHook;
  140. 'feature-disabled:relay': FeatureDisabledHook;
  141. 'feature-disabled:sso-basic': FeatureDisabledHook;
  142. 'feature-disabled:sso-rippling': FeatureDisabledHook;
  143. 'feature-disabled:sso-saml2': FeatureDisabledHook;
  144. 'feature-disabled:trace-view-link': FeatureDisabledHook;
  145. 'feature-disabled:alert-wizard-performance': FeatureDisabledHook;
  146. };
  147. /**
  148. * Interface chrome hooks.
  149. */
  150. export type InterfaceChromeHooks = {
  151. footer: GenericComponentHook;
  152. 'organization:header': OrganizationHeaderComponentHook;
  153. 'sidebar:help-menu': GenericOrganizationComponentHook;
  154. 'sidebar:organization-dropdown-menu': GenericOrganizationComponentHook;
  155. 'sidebar:organization-dropdown-menu-bottom': GenericOrganizationComponentHook;
  156. 'sidebar:bottom-items': SidebarBottomItemsHook;
  157. 'sidebar:item-label': SidebarItemLabelHook;
  158. 'sidebar:item-override': SidebarItemOverrideHook;
  159. 'help-modal:footer': HelpModalFooterHook;
  160. };
  161. /**
  162. * Onboarding experience hooks
  163. */
  164. export type OnboardingHooks = {
  165. 'onboarding:extra-chrome': GenericComponentHook;
  166. 'onboarding-wizard:skip-help': GenericOrganizationComponentHook;
  167. };
  168. /**
  169. * Settings navigation hooks.
  170. */
  171. export type SettingsHooks = {
  172. 'settings:api-navigation-config': SettingsItemsHook;
  173. 'settings:organization-navigation': OrganizationSettingsHook;
  174. 'settings:organization-navigation-config': SettingsConfigHook;
  175. };
  176. /**
  177. * Renders a React node with no props
  178. */
  179. type GenericComponentHook = () => React.ReactNode;
  180. /**
  181. * A route hook provides an injection point for a list of routes.
  182. */
  183. type RoutesHook = () => Route[];
  184. /**
  185. * Receives an organization object and should return a React node.
  186. */
  187. type GenericOrganizationComponentHook = (opts: {
  188. organization: Organization;
  189. }) => React.ReactNode;
  190. // TODO(ts): We should correct the organization header hook to conform to the
  191. // GenericOrganizationComponentHook, passing org as a prop object, not direct
  192. // as the only argument.
  193. type OrganizationHeaderComponentHook = (org: Organization) => React.ReactNode;
  194. /**
  195. * A FeatureDisabledHook returns a react element when a feature is not enabled.
  196. */
  197. type FeatureDisabledHook = (opts: {
  198. /**
  199. * The organization that is associated to this feature.
  200. */
  201. organization: Organization;
  202. /**
  203. * The list of features that are controlled by this hook.
  204. */
  205. features: string[];
  206. /**
  207. * Weather the feature is or is not enabled.
  208. */
  209. hasFeature: boolean;
  210. /**
  211. * Children can either be a node, or a function that accepts a renderDisabled prop containing
  212. * a function/component to render when the feature is not enabled.
  213. */
  214. children: React.ReactNode | ChildrenRenderFn;
  215. /**
  216. * The project that is associated to this feature.
  217. */
  218. project?: Project;
  219. }) => React.ReactNode;
  220. /**
  221. * Called when the app is mounted.
  222. */
  223. type AnalyticsInitUser = (user: User) => void;
  224. /**
  225. * Trigger analytics tracking in the hook store.
  226. */
  227. type AnalyticsTrackEvent = (opts: {
  228. /**
  229. * The key used to identify the event.
  230. */
  231. eventKey: string;
  232. /**
  233. * The English string used as the name of the event.
  234. */
  235. eventName: string;
  236. organization_id: string | number | null;
  237. /**
  238. * Arbitrary data to track
  239. */
  240. [key: string]: any;
  241. }) => void;
  242. /**
  243. * Trigger analytics tracking in the hook store.
  244. */
  245. type AnalyticsTrackEventV2 = (
  246. data: {
  247. /**
  248. * The Reload event key.
  249. */
  250. eventKey: string;
  251. /**
  252. * The Amplitude event name. Set to null if event should not go to Amplitude.
  253. */
  254. eventName: string | null;
  255. organization: LightWeightOrganization | null;
  256. /**
  257. * Arbitrary data to track
  258. */
  259. [key: string]: any;
  260. },
  261. options?: {
  262. /**
  263. * If true, starts an analytics session. This session can be used
  264. * to construct funnels. The start of the funnel should have
  265. * startSession set to true.
  266. */
  267. startSession?: boolean;
  268. /**
  269. * An arbitrary function to map the parameters to new parameters
  270. */
  271. mapValuesFn?: (params: Record<string, any>) => Record<string, any>;
  272. }
  273. ) => void;
  274. /**
  275. * Trigger ad hoc analytics tracking in the hook store.
  276. */
  277. type AnalyticsTrackAdhocEvent = (opts: {
  278. /**
  279. * The key used to identify the event.
  280. */
  281. eventKey: string;
  282. /**
  283. * Arbitrary data to track
  284. */
  285. [key: string]: any;
  286. }) => void;
  287. /**
  288. * Trigger experiment observed logging.
  289. */
  290. type AnalyticsLogExperiment = (opts: {
  291. /**
  292. * The organization. Must be provided for organization experiments.
  293. */
  294. organization?: Organization;
  295. /**
  296. * The experiment key
  297. */
  298. key: ExperimentKey;
  299. }) => void;
  300. /**
  301. * Trigger analytics tracking in the hook store.
  302. *
  303. * Prefer using `analytics:track-event` or `analytics:track-adhock-event`.
  304. *
  305. * @deprecated This is the legacy interface.
  306. */
  307. type LegacyAnalyticsEvent = (
  308. /**
  309. * The key used to identify the event.
  310. */
  311. name: string,
  312. /**
  313. * Arbitrary data to track
  314. */
  315. data: {[key: string]: any}
  316. ) => void;
  317. /**
  318. * Trigger recording a metric in the hook store.
  319. */
  320. type MetricsEvent = (
  321. /**
  322. * Metric name
  323. */
  324. name: string,
  325. /**
  326. * Value to record for this metric
  327. */
  328. value: number,
  329. /**
  330. * An additional tags object
  331. */
  332. tags?: object
  333. ) => void;
  334. /**
  335. * Provides additional navigation components
  336. */
  337. type OrganizationSettingsHook = (organization: Organization) => React.ReactElement;
  338. /**
  339. * Provides additional setting configurations
  340. */
  341. type SettingsConfigHook = (organization: Organization) => NavigationSection;
  342. /**
  343. * Provides additional setting navigation items
  344. */
  345. type SettingsItemsHook = (organization?: Organization) => NavigationItem[];
  346. /**
  347. * Each sidebar label is wrapped with this hook, to allow sidebar item
  348. * augmentation.
  349. */
  350. type SidebarItemLabelHook = () => React.ComponentType<{
  351. /**
  352. * The item label being wrapped
  353. */
  354. children: React.ReactNode;
  355. /**
  356. * The key of the item label currently being rendered. If no id is provided
  357. * the hook will have no effect.
  358. */
  359. id?: string;
  360. }>;
  361. type SidebarItemOverrideHook = () => React.ComponentType<{
  362. /**
  363. * The item label being wrapped
  364. */
  365. children: (props: Partial<React.ComponentProps<typeof SidebarItem>>) => React.ReactNode;
  366. id?: string;
  367. }>;
  368. type SidebarProps = Pick<
  369. React.ComponentProps<typeof SidebarItem>,
  370. 'orientation' | 'collapsed' | 'hasPanel'
  371. >;
  372. /**
  373. * Returns an additional list of sidebar items.
  374. */
  375. type SidebarBottomItemsHook = (
  376. opts: SidebarProps & {organization: Organization}
  377. ) => React.ReactNode;
  378. /**
  379. * Provides augmentation of the help modal footer
  380. */
  381. type HelpModalFooterHook = (opts: {
  382. closeModal: () => void;
  383. organization: Organization;
  384. }) => React.ReactNode;
  385. /**
  386. * The DecoratedIntegrationFeature differs from the IntegrationFeature as it is
  387. * expected to have been transformed into marked up content.
  388. */
  389. type DecoratedIntegrationFeature = {
  390. /**
  391. * Marked up description
  392. */
  393. description: React.ReactNode;
  394. featureGate: string;
  395. };
  396. type IntegrationFeatureGroup = {
  397. /**
  398. * The list of features within this group
  399. */
  400. features: DecoratedIntegrationFeature[];
  401. /**
  402. * Weather the group has all of the features enabled within this group
  403. * or not.
  404. */
  405. hasFeatures: boolean;
  406. };
  407. type FeatureGateSharedProps = {
  408. /**
  409. * Organization of the integration we're querying feature gate details for.
  410. */
  411. organization: Organization;
  412. /**
  413. * The list of features, typically this is provided by the backend.
  414. */
  415. features: DecoratedIntegrationFeature[];
  416. };
  417. type IntegrationFeaturesProps = FeatureGateSharedProps & {
  418. /**
  419. * The children function which will be provided with gating details.
  420. */
  421. children: (opts: {
  422. /**
  423. * This is the list of features which have *not* been gated in any way.
  424. */
  425. ungatedFeatures: DecoratedIntegrationFeature[];
  426. /**
  427. * Features grouped based on specific gating criteria (for example, in
  428. * sentry.io this is features grouped by plans).
  429. */
  430. gatedFeatureGroups: IntegrationFeatureGroup[];
  431. /**
  432. * Is the integration disabled for installation because of feature gating?
  433. */
  434. disabled: boolean;
  435. /**
  436. * The translated reason that the integration is disabled.
  437. */
  438. disabledReason: React.ReactNode;
  439. }) => React.ReactElement;
  440. };
  441. type IntegrationFeatureListProps = FeatureGateSharedProps & {
  442. provider: Pick<IntegrationProvider, 'key'>;
  443. };
  444. /**
  445. * The integration features gate hook provides components to customize
  446. * integration feature lists.
  447. */
  448. type IntegrationsFeatureGatesHook = () => {
  449. /**
  450. * This is a render-prop style component that given a set of integration
  451. * features will call the children function with gating details about the
  452. * features.
  453. */
  454. IntegrationFeatures: React.ComponentType<IntegrationFeaturesProps>;
  455. IntegrationDirectoryFeatures: React.ComponentType<IntegrationFeaturesProps>;
  456. /**
  457. * This component renders the list of integration features.
  458. */
  459. FeatureList: React.ComponentType<IntegrationFeatureListProps>;
  460. IntegrationDirectoryFeatureList: React.ComponentType<IntegrationFeatureListProps>;
  461. };
  462. /**
  463. * Invite Modal customization allows for a render-prop component to add
  464. * additional react elements into the modal, and add invite-send middleware.
  465. */
  466. type InviteModalCustomizationHook = () => React.ComponentType<{
  467. /**
  468. * The organization that members will be invited to.
  469. */
  470. organization: Organization;
  471. /**
  472. * Indicates if clicking 'send invites' will immediately send invites, or
  473. * would just create invite requests.
  474. */
  475. willInvite: boolean;
  476. /**
  477. * When the children's sendInvites renderProp is called, this will also be
  478. * triggered.
  479. */
  480. onSendInvites: () => void;
  481. children: (opts: {
  482. /**
  483. * Additional react elements to render in the header of the modal, just
  484. * under the description.
  485. */
  486. headerInfo?: React.ReactNode;
  487. /**
  488. * Indicates that the modal's send invites button should be enabled and
  489. * invites may currently be sent.
  490. */
  491. canSend: boolean;
  492. /**
  493. * Trigger sending invites
  494. */
  495. sendInvites: () => void;
  496. }) => React.ReactElement;
  497. }>;