hooks.tsx 19 KB

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