hooks.tsx 18 KB

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