hooks.tsx 18 KB

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