hooks.tsx 17 KB

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