hooks.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. import {Route, RouteComponentProps} from 'react-router';
  2. import {ChildrenRenderFn} from 'sentry/components/acl/feature';
  3. import DateRange from 'sentry/components/organizations/timeRangeSelector/dateRange';
  4. import SelectorItems from 'sentry/components/organizations/timeRangeSelector/dateRange/selectorItems';
  5. import SidebarItem from 'sentry/components/sidebar/sidebarItem';
  6. import {
  7. Integration,
  8. IntegrationProvider,
  9. Member,
  10. Organization,
  11. Project,
  12. User,
  13. } from 'sentry/types';
  14. import {ExperimentKey} from 'sentry/types/experiments';
  15. import {NavigationItem, NavigationSection} from 'sentry/views/settings/types';
  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 type Hooks = {_: any} & RouteHooks &
  29. ComponentHooks &
  30. CustomizationHooks &
  31. AnalyticsHooks &
  32. FeatureDisabledHooks &
  33. InterfaceChromeHooks &
  34. OnboardingHooks &
  35. SettingsHooks;
  36. export type HookName = keyof Hooks;
  37. /**
  38. * Route hooks.
  39. */
  40. export type RouteHooks = {
  41. routes: RoutesHook;
  42. 'routes:admin': RoutesHook;
  43. 'routes:api': RoutesHook;
  44. 'routes:organization': RoutesHook;
  45. };
  46. /**
  47. * Component specific hooks for DateRange and SelectorItems
  48. * These components have plan specific overrides in getsentry
  49. */
  50. type DateRangeProps = React.ComponentProps<typeof DateRange>;
  51. type SelectorItemsProps = React.ComponentProps<typeof SelectorItems>;
  52. type GlobalNotificationProps = {className: string; organization?: Organization};
  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 CodeOwnersHeaderProps = {
  63. addCodeOwner: () => void;
  64. handleRequest: () => void;
  65. };
  66. type FirstPartyIntegrationAlertProps = {
  67. integrations: Integration[];
  68. hideCTA?: boolean;
  69. wrapWithContainer?: boolean;
  70. };
  71. type FirstPartyIntegrationAdditionalCTAProps = {
  72. integrations: Integration[];
  73. };
  74. /**
  75. * Component wrapping hooks
  76. */
  77. export type ComponentHooks = {
  78. 'component:codeowners-header': () => React.ComponentType<CodeOwnersHeaderProps>;
  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:global-notifications': () => React.ComponentType<GlobalNotificationProps>;
  87. 'component:header-date-range': () => React.ComponentType<DateRangeProps>;
  88. 'component:header-selector-items': () => React.ComponentType<SelectorItemsProps>;
  89. 'component:member-list-header': () => React.ComponentType<MemberListHeaderProps>;
  90. 'component:org-stats-banner': () => React.ComponentType<DashboardHeadersProps>;
  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-modal:customization': InviteModalCustomizationHook;
  101. };
  102. /**
  103. * Analytics / tracking / and operational metrics backend hooks.
  104. */
  105. export type AnalyticsHooks = {
  106. // TODO(scefali): Below are deprecated and should be replaced
  107. 'analytics:event': LegacyAnalyticsEvent;
  108. 'analytics:init-user': AnalyticsInitUser;
  109. 'analytics:log-experiment': AnalyticsLogExperiment;
  110. 'analytics:track-adhoc-event': AnalyticsTrackAdhocEvent;
  111. 'analytics:track-event': AnalyticsTrackEvent;
  112. 'analytics:track-event-v2': AnalyticsTrackEventV2;
  113. 'metrics:event': MetricsEvent;
  114. };
  115. /**
  116. * feature-disabled:<feature-flag> hooks return components that will be
  117. * rendered in place for Feature components when the feature is not enabled.
  118. */
  119. export type FeatureDisabledHooks = {
  120. 'feature-disabled:alert-wizard-performance': FeatureDisabledHook;
  121. 'feature-disabled:alerts-page': FeatureDisabledHook;
  122. 'feature-disabled:configure-distributed-tracing': 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:extra-chrome': GenericComponentHook;
  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. * Renders a React node with no props
  186. */
  187. type GenericComponentHook = () => React.ReactNode;
  188. /**
  189. * A route hook provides an injection point for a list of routes.
  190. */
  191. type RoutesHook = () => Route[];
  192. /**
  193. * Receives an organization object and should return a React node.
  194. */
  195. type GenericOrganizationComponentHook = (opts: {
  196. organization: Organization;
  197. }) => React.ReactNode;
  198. // TODO(ts): We should correct the organization header hook to conform to the
  199. // GenericOrganizationComponentHook, passing org as a prop object, not direct
  200. // as the only argument.
  201. type OrganizationHeaderComponentHook = (org: Organization) => React.ReactNode;
  202. /**
  203. * A FeatureDisabledHook returns a react element when a feature is not enabled.
  204. */
  205. type FeatureDisabledHook = (opts: {
  206. /**
  207. * Children can either be a node, or a function that accepts a renderDisabled prop containing
  208. * a function/component to render when the feature is not enabled.
  209. */
  210. children: React.ReactNode | ChildrenRenderFn;
  211. /**
  212. * The list of features that are controlled by this hook.
  213. */
  214. features: string[];
  215. /**
  216. * Weather the feature is or is not enabled.
  217. */
  218. hasFeature: boolean;
  219. /**
  220. * The organization that is associated to this feature.
  221. */
  222. organization: Organization;
  223. /**
  224. * The project that is associated to this feature.
  225. */
  226. project?: Project;
  227. }) => React.ReactNode;
  228. /**
  229. * Called when the app is mounted.
  230. */
  231. type AnalyticsInitUser = (user: User) => void;
  232. /**
  233. * Trigger analytics tracking in the hook store.
  234. */
  235. type AnalyticsTrackEvent = (opts: {
  236. /**
  237. * Arbitrary data to track
  238. */
  239. [key: string]: any;
  240. /**
  241. * The key used to identify the event.
  242. */
  243. eventKey: string;
  244. /**
  245. * The English string used as the name of the event.
  246. */
  247. eventName: string;
  248. organization_id: string | number | null;
  249. }) => void;
  250. /**
  251. * Trigger analytics tracking in the hook store.
  252. */
  253. type AnalyticsTrackEventV2 = (
  254. data: {
  255. /**
  256. * Arbitrary data to track
  257. */
  258. [key: string]: any;
  259. /**
  260. * The Reload event key.
  261. */
  262. eventKey: string;
  263. /**
  264. * The Amplitude event name. Set to null if event should not go to Amplitude.
  265. */
  266. eventName: string | null;
  267. organization: Organization | null;
  268. },
  269. options?: {
  270. /**
  271. * An arbitrary function to map the parameters to new parameters
  272. */
  273. mapValuesFn?: (params: Record<string, any>) => Record<string, any>;
  274. /**
  275. * If true, starts an analytics session. This session can be used
  276. * to construct funnels. The start of the funnel should have
  277. * startSession set to true.
  278. */
  279. startSession?: boolean;
  280. }
  281. ) => void;
  282. /**
  283. * Trigger ad hoc analytics tracking in the hook store.
  284. */
  285. type AnalyticsTrackAdhocEvent = (opts: {
  286. /**
  287. * Arbitrary data to track
  288. */
  289. [key: string]: any;
  290. /**
  291. * The key used to identify the event.
  292. */
  293. eventKey: string;
  294. }) => void;
  295. /**
  296. * Trigger experiment observed logging.
  297. */
  298. type AnalyticsLogExperiment = (opts: {
  299. /**
  300. * The experiment key
  301. */
  302. key: ExperimentKey;
  303. /**
  304. * The organization. Must be provided for organization experiments.
  305. */
  306. organization?: Organization;
  307. }) => void;
  308. /**
  309. * Trigger analytics tracking in the hook store.
  310. *
  311. * Prefer using `analytics:track-event` or `analytics:track-adhock-event`.
  312. *
  313. * @deprecated This is the legacy interface.
  314. */
  315. type LegacyAnalyticsEvent = (
  316. /**
  317. * The key used to identify the event.
  318. */
  319. name: string,
  320. /**
  321. * Arbitrary data to track
  322. */
  323. data: {[key: string]: any}
  324. ) => void;
  325. /**
  326. * Trigger recording a metric in the hook store.
  327. */
  328. type MetricsEvent = (
  329. /**
  330. * Metric name
  331. */
  332. name: string,
  333. /**
  334. * Value to record for this metric
  335. */
  336. value: number,
  337. /**
  338. * An additional tags object
  339. */
  340. tags?: object
  341. ) => void;
  342. /**
  343. * Provides additional navigation components
  344. */
  345. type OrganizationSettingsHook = (organization: Organization) => React.ReactElement;
  346. /**
  347. * Provides additional setting configurations
  348. */
  349. type SettingsConfigHook = (organization: Organization) => NavigationSection;
  350. /**
  351. * Provides additional setting navigation items
  352. */
  353. type SettingsItemsHook = (organization?: Organization) => NavigationItem[];
  354. /**
  355. * Each sidebar label is wrapped with this hook, to allow sidebar item
  356. * augmentation.
  357. */
  358. type SidebarItemLabelHook = () => React.ComponentType<{
  359. /**
  360. * The item label being wrapped
  361. */
  362. children: React.ReactNode;
  363. /**
  364. * The key of the item label currently being rendered. If no id is provided
  365. * the hook will have no effect.
  366. */
  367. id?: string;
  368. }>;
  369. type SidebarItemOverrideHook = () => React.ComponentType<{
  370. /**
  371. * The item label being wrapped
  372. */
  373. children: (props: Partial<React.ComponentProps<typeof SidebarItem>>) => React.ReactNode;
  374. id?: string;
  375. }>;
  376. type SidebarProps = Pick<
  377. React.ComponentProps<typeof SidebarItem>,
  378. 'orientation' | 'collapsed' | 'hasPanel'
  379. >;
  380. /**
  381. * Returns an additional list of sidebar items.
  382. */
  383. type SidebarBottomItemsHook = (
  384. opts: SidebarProps & {organization: Organization}
  385. ) => React.ReactNode;
  386. /**
  387. * Provides augmentation of the help modal footer
  388. */
  389. type HelpModalFooterHook = (opts: {
  390. closeModal: () => void;
  391. organization: Organization;
  392. }) => React.ReactNode;
  393. /**
  394. * The DecoratedIntegrationFeature differs from the IntegrationFeature as it is
  395. * expected to have been transformed into marked up content.
  396. */
  397. type DecoratedIntegrationFeature = {
  398. /**
  399. * Marked up description
  400. */
  401. description: React.ReactNode;
  402. featureGate: string;
  403. };
  404. type IntegrationFeatureGroup = {
  405. /**
  406. * The list of features within this group
  407. */
  408. features: DecoratedIntegrationFeature[];
  409. /**
  410. * Weather the group has all of the features enabled within this group
  411. * or not.
  412. */
  413. hasFeatures: boolean;
  414. };
  415. type FeatureGateSharedProps = {
  416. /**
  417. * The list of features, typically this is provided by the backend.
  418. */
  419. features: DecoratedIntegrationFeature[];
  420. /**
  421. * Organization of the integration we're querying feature gate details for.
  422. */
  423. organization: Organization;
  424. };
  425. type IntegrationFeaturesProps = FeatureGateSharedProps & {
  426. /**
  427. * The children function which will be provided with gating details.
  428. */
  429. children: (opts: {
  430. /**
  431. * Is the integration disabled for installation because of feature gating?
  432. */
  433. disabled: boolean;
  434. /**
  435. * The translated reason that the integration is disabled.
  436. */
  437. disabledReason: React.ReactNode;
  438. /**
  439. * Features grouped based on specific gating criteria (for example, in
  440. * sentry.io this is features grouped by plans).
  441. */
  442. gatedFeatureGroups: IntegrationFeatureGroup[];
  443. /**
  444. * This is the list of features which have *not* been gated in any way.
  445. */
  446. ungatedFeatures: DecoratedIntegrationFeature[];
  447. }) => React.ReactElement;
  448. };
  449. type IntegrationFeatureListProps = FeatureGateSharedProps & {
  450. provider: Pick<IntegrationProvider, 'key'>;
  451. };
  452. /**
  453. * The integration features gate hook provides components to customize
  454. * integration feature lists.
  455. */
  456. type IntegrationsFeatureGatesHook = () => {
  457. /**
  458. * This component renders the list of integration features.
  459. */
  460. FeatureList: React.ComponentType<IntegrationFeatureListProps>;
  461. IntegrationDirectoryFeatureList: React.ComponentType<IntegrationFeatureListProps>;
  462. IntegrationDirectoryFeatures: React.ComponentType<IntegrationFeaturesProps>;
  463. /**
  464. * This is a render-prop style component that given a set of integration
  465. * features will call the children function with gating details about the
  466. * features.
  467. */
  468. IntegrationFeatures: React.ComponentType<IntegrationFeaturesProps>;
  469. };
  470. /**
  471. * Invite Modal customization allows for a render-prop component to add
  472. * additional react elements into the modal, and add invite-send middleware.
  473. */
  474. type InviteModalCustomizationHook = () => React.ComponentType<{
  475. children: (opts: {
  476. /**
  477. * Indicates that the modal's send invites button should be enabled and
  478. * invites may currently be sent.
  479. */
  480. canSend: boolean;
  481. /**
  482. * Trigger sending invites
  483. */
  484. sendInvites: () => void;
  485. /**
  486. * Additional react elements to render in the header of the modal, just
  487. * under the description.
  488. */
  489. headerInfo?: React.ReactNode;
  490. }) => React.ReactElement;
  491. /**
  492. * When the children's sendInvites renderProp is called, this will also be
  493. * triggered.
  494. */
  495. onSendInvites: () => void;
  496. /**
  497. * The organization that members will be invited to.
  498. */
  499. organization: Organization;
  500. /**
  501. * Indicates if clicking 'send invites' will immediately send invites, or
  502. * would just create invite requests.
  503. */
  504. willInvite: boolean;
  505. }>;