hooks.tsx 19 KB

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