hooks.tsx 14 KB

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