hooks.tsx 18 KB

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