hooks.tsx 16 KB

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