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 type DateRange from 'sentry/components/organizations/timeRangeSelector/dateRange';
  5. import type SelectorItems from 'sentry/components/organizations/timeRangeSelector/selectorItems';
  6. import type SidebarItem from 'sentry/components/sidebar/sidebarItem';
  7. import {RouteAnalyticsContext} from 'sentry/views/routeAnalyticsContextProvider';
  8. import type {NavigationItem, NavigationSection} from 'sentry/views/settings/types';
  9. import type {ExperimentKey} from './experiments';
  10. import type {Integration, IntegrationProvider} from './integrations';
  11. import type {Member, Organization} from './organization';
  12. import type {Project} from './project';
  13. import type {User} from './user';
  14. // XXX(epurkhiser): A Note about `_`.
  15. //
  16. // We add the `_: any` type int our hooks list to stop
  17. // typescript from doing too much type tightening. We should absolutely revisit
  18. // this in the future because all callbacks _should_ be allowed to be
  19. // functions, but doing so causes some unexpected issues and makes typescript
  20. // not happy. We still get a huge advantage of typing just by having each hook
  21. // type here however.
  22. /**
  23. * The Hooks type mapping is the master interface for all external Hooks into
  24. * the sentry frontend application.
  25. */
  26. export interface Hooks
  27. extends RouteHooks,
  28. ComponentHooks,
  29. CustomizationHooks,
  30. AnalyticsHooks,
  31. FeatureDisabledHooks,
  32. InterfaceChromeHooks,
  33. OnboardingHooks,
  34. SettingsHooks,
  35. FeatureSpecificHooks,
  36. ReactHooks,
  37. CallbackHooks {
  38. _: any;
  39. }
  40. export type HookName = keyof Hooks;
  41. /**
  42. * Route hooks.
  43. */
  44. export type RouteHooks = {
  45. 'routes:api': RoutesHook;
  46. 'routes:organization': RoutesHook;
  47. };
  48. /**
  49. * Component specific hooks for DateRange and SelectorItems
  50. * These components have plan specific overrides in getsentry
  51. */
  52. type DateRangeProps = React.ComponentProps<typeof DateRange>;
  53. type SelectorItemsProps = React.ComponentProps<typeof SelectorItems>;
  54. type DisabledMemberViewProps = RouteComponentProps<{orgId: string}, {}>;
  55. type MemberListHeaderProps = {
  56. members: Member[];
  57. organization: Organization;
  58. };
  59. type DisabledAppStoreConnectMultiple = {organization: Organization};
  60. type DisabledCustomSymbolSources = {organization: Organization};
  61. type DisabledMemberTooltipProps = {children: React.ReactNode};
  62. type DashboardHeadersProps = {organization: Organization};
  63. type FirstPartyIntegrationAlertProps = {
  64. integrations: Integration[];
  65. hideCTA?: boolean;
  66. wrapWithContainer?: boolean;
  67. };
  68. type FirstPartyIntegrationAdditionalCTAProps = {
  69. integrations: Integration[];
  70. };
  71. type GuideUpdateCallback = (nextGuide: Guide | null, opts: {dismissed?: boolean}) => void;
  72. /**
  73. * Component wrapping hooks
  74. */
  75. export type ComponentHooks = {
  76. 'component:dashboards-header': () => React.ComponentType<DashboardHeadersProps>;
  77. 'component:disabled-app-store-connect-multiple': () => React.ComponentType<DisabledAppStoreConnectMultiple>;
  78. 'component:disabled-custom-symbol-sources': () => React.ComponentType<DisabledCustomSymbolSources>;
  79. 'component:disabled-member': () => React.ComponentType<DisabledMemberViewProps>;
  80. 'component:disabled-member-tooltip': () => React.ComponentType<DisabledMemberTooltipProps>;
  81. 'component:dynamic-sampling-limited-availability-program-ending': () => React.ComponentType<{}>;
  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:extra-chrome': GenericComponentHook;
  173. 'onboarding:show-sidebar': (organization: Organization) => boolean;
  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. };
  203. /**
  204. * Callback hooks.
  205. * These hooks just call a function that has no return value
  206. * and perform some sort of callback logic
  207. */
  208. type CallbackHooks = {
  209. 'callback:on-guide-update': GuideUpdateCallback;
  210. };
  211. /**
  212. * Renders a React node with no props
  213. */
  214. type GenericComponentHook = () => React.ReactNode;
  215. /**
  216. * A route hook provides an injection point for a list of routes.
  217. */
  218. type RoutesHook = () => Route[];
  219. /**
  220. * Receives an organization object and should return a React node.
  221. */
  222. type GenericOrganizationComponentHook = (opts: {
  223. organization: Organization;
  224. }) => React.ReactNode;
  225. /**
  226. * Receives a project object and should return a React node.
  227. */
  228. type GenericProjectComponentHook = (opts: {project: Project}) => React.ReactNode;
  229. // TODO(ts): We should correct the organization header hook to conform to the
  230. // GenericOrganizationComponentHook, passing org as a prop object, not direct
  231. // as the only argument.
  232. type OrganizationHeaderComponentHook = (org: Organization) => React.ReactNode;
  233. /**
  234. * A FeatureDisabledHook returns a react element when a feature is not enabled.
  235. */
  236. type FeatureDisabledHook = (opts: {
  237. /**
  238. * Children can either be a node, or a function that accepts a renderDisabled prop containing
  239. * a function/component to render when the feature is not enabled.
  240. */
  241. children: React.ReactNode | ChildrenRenderFn;
  242. /**
  243. * The list of features that are controlled by this hook.
  244. */
  245. features: string[];
  246. /**
  247. * Weather the feature is or is not enabled.
  248. */
  249. hasFeature: boolean;
  250. /**
  251. * The organization that is associated to this feature.
  252. */
  253. organization: Organization;
  254. /**
  255. * The project that is associated to this feature.
  256. */
  257. project?: Project;
  258. }) => React.ReactNode;
  259. /**
  260. * Called when the app is mounted.
  261. */
  262. type AnalyticsInitUser = (user: User) => void;
  263. /**
  264. * Trigger analytics tracking in the hook store.
  265. */
  266. type AnalyticsTrackEvent = (opts: {
  267. /**
  268. * Arbitrary data to track
  269. */
  270. [key: string]: any;
  271. /**
  272. * The key used to identify the event.
  273. */
  274. eventKey: string;
  275. /**
  276. * The English string used as the name of the event.
  277. */
  278. eventName: string;
  279. organization_id: string | number | null;
  280. }) => void;
  281. /**
  282. * Trigger analytics tracking in the hook store.
  283. */
  284. type AnalyticsTrackEventV2 = (
  285. data: {
  286. /**
  287. * Arbitrary data to track
  288. */
  289. [key: string]: any;
  290. /**
  291. * The Reload event key.
  292. */
  293. eventKey: string;
  294. /**
  295. * The Amplitude event name. Set to null if event should not go to Amplitude.
  296. */
  297. eventName: string | null;
  298. /**
  299. * Organization to pass in. If full org object not available, pass in just the Id.
  300. * If no org, pass in null.
  301. */
  302. organization: Organization | string | null;
  303. },
  304. options?: {
  305. /**
  306. * An arbitrary function to map the parameters to new parameters
  307. */
  308. mapValuesFn?: (params: Record<string, any>) => Record<string, any>;
  309. /**
  310. * If true, starts an analytics session. This session can be used
  311. * to construct funnels. The start of the funnel should have
  312. * startSession set to true.
  313. */
  314. startSession?: boolean;
  315. /**
  316. * Optional unix timestamp
  317. */
  318. time?: number;
  319. }
  320. ) => void;
  321. /**
  322. * Trigger ad hoc analytics tracking in the hook store.
  323. */
  324. type AnalyticsTrackAdhocEvent = (opts: {
  325. /**
  326. * Arbitrary data to track
  327. */
  328. [key: string]: any;
  329. /**
  330. * The key used to identify the event.
  331. */
  332. eventKey: string;
  333. }) => void;
  334. /**
  335. * Trigger experiment observed logging.
  336. */
  337. type AnalyticsLogExperiment = (opts: {
  338. /**
  339. * The experiment key
  340. */
  341. key: ExperimentKey;
  342. /**
  343. * The organization. Must be provided for organization experiments.
  344. */
  345. organization?: Organization;
  346. }) => void;
  347. /**
  348. * Trigger analytics tracking in the hook store.
  349. *
  350. * Prefer using `analytics:track-event` or `analytics:track-adhock-event`.
  351. *
  352. * @deprecated This is the legacy interface.
  353. */
  354. type LegacyAnalyticsEvent = (
  355. /**
  356. * The key used to identify the event.
  357. */
  358. name: string,
  359. /**
  360. * Arbitrary data to track
  361. */
  362. data: {[key: string]: any}
  363. ) => void;
  364. /**
  365. * Trigger recording a metric in the hook store.
  366. */
  367. type MetricsEvent = (
  368. /**
  369. * Metric name
  370. */
  371. name: string,
  372. /**
  373. * Value to record for this metric
  374. */
  375. value: number,
  376. /**
  377. * An additional tags object
  378. */
  379. tags?: object
  380. ) => void;
  381. /**
  382. * Provides additional navigation components
  383. */
  384. type OrganizationSettingsHook = (organization: Organization) => React.ReactElement;
  385. /**
  386. * Provides additional setting configurations
  387. */
  388. type SettingsConfigHook = (organization: Organization) => NavigationSection;
  389. /**
  390. * Provides additional setting navigation items
  391. */
  392. type SettingsItemsHook = (organization?: Organization) => NavigationItem[];
  393. /**
  394. * Each sidebar label is wrapped with this hook, to allow sidebar item
  395. * augmentation.
  396. */
  397. type SidebarItemLabelHook = () => React.ComponentType<{
  398. /**
  399. * The item label being wrapped
  400. */
  401. children: React.ReactNode;
  402. /**
  403. * The key of the item label currently being rendered. If no id is provided
  404. * the hook will have no effect.
  405. */
  406. id?: string;
  407. }>;
  408. type SidebarItemOverrideHook = () => React.ComponentType<{
  409. /**
  410. * The item label being wrapped
  411. */
  412. children: (props: Partial<React.ComponentProps<typeof SidebarItem>>) => React.ReactNode;
  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. }>;