hooks.tsx 13 KB

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