hooks.tsx 18 KB

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