hooks.tsx 18 KB

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