Browse Source

ref(experiments): Add type for getsentry useExperiment hook (#45605)

Ref https://github.com/getsentry/sentry/issues/45557
Malachi Willey 2 years ago
parent
commit
570171fa9e
2 changed files with 12 additions and 5 deletions
  1. 2 0
      static/app/types/hooks.tsx
  2. 10 5
      static/app/utils/useExperiment.tsx

+ 2 - 0
static/app/types/hooks.tsx

@@ -6,6 +6,7 @@ import {ButtonProps} from 'sentry/components/button';
 import type DateRange from 'sentry/components/organizations/timeRangeSelector/dateRange';
 import type SelectorItems from 'sentry/components/organizations/timeRangeSelector/selectorItems';
 import type SidebarItem from 'sentry/components/sidebar/sidebarItem';
+import {UseExperiment} from 'sentry/utils/useExperiment';
 import {UsageStatsOrganizationProps} from 'sentry/views/organizationStats/usageStatsOrg';
 import {RouteAnalyticsContext} from 'sentry/views/routeAnalyticsContextProvider';
 import type {NavigationItem, NavigationSection} from 'sentry/views/settings/types';
@@ -236,6 +237,7 @@ export type ReactHooks = {
     props: RouteContextInterface
   ) => React.ContextType<typeof RouteAnalyticsContext>;
   'react-hook:use-button-tracking': (props: ButtonProps) => () => void;
+  'react-hook:use-experiment': UseExperiment;
 };
 
 /**

+ 10 - 5
static/app/utils/useExperiment.tsx

@@ -40,6 +40,11 @@ type UseExperimentReturnValue<E extends ExperimentKey> = {
   logExperiment: () => void;
 };
 
+export type UseExperiment = <E extends ExperimentKey>(
+  experiment: E,
+  options?: UseExperimentOptions
+) => UseExperimentReturnValue<E>;
+
 function useExperimentAssignment(experiment: ExperimentKey) {
   const organization = useOrganization();
   const {user} = useLegacyStore(ConfigStore);
@@ -89,10 +94,10 @@ function useExperimentAssignment(experiment: ExperimentKey) {
   return unassignedValue;
 }
 
-export function useExperiment<E extends ExperimentKey>(
-  experiment: E,
-  {logExperimentOnMount = true}: UseExperimentOptions = {}
-): UseExperimentReturnValue<E> {
+export const useExperiment: UseExperiment = (
+  experiment,
+  {logExperimentOnMount = true} = {}
+) => {
   const organization = useOrganization();
 
   const logExperiment = useCallback(() => {
@@ -114,4 +119,4 @@ export function useExperiment<E extends ExperimentKey>(
     experimentAssignment,
     logExperiment,
   };
-}
+};