hooks.tsx 19 KB

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