hooks.tsx 18 KB

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