hooks.tsx 19 KB

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