hooks.tsx 19 KB

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