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