hooks.tsx 19 KB

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