hooks.tsx 20 KB

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