hooks.tsx 17 KB

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