experiments.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type {experimentList, unassignedValue} from 'sentry/data/experimentConfig';
  2. /**
  3. * The grouping of the experiment
  4. */
  5. export enum ExperimentType {
  6. ORGANIZATION = 'organization',
  7. USER = 'user',
  8. }
  9. /**
  10. * An experiment configuration object defines an experiment in the frontend.
  11. * This drives various logic in experiment helpers.
  12. */
  13. export type ExperimentConfig = {
  14. /**
  15. * Possible assignment values of the experiment
  16. */
  17. assignments: ReadonlyArray<string | number | typeof unassignedValue>;
  18. /**
  19. * The name of the organization. This maps to the key exposed by the
  20. * organization manager on the backend.
  21. */
  22. key: string;
  23. /**
  24. * The parameter used to access the assignment value
  25. */
  26. parameter: string | 'variant' | 'exposed';
  27. /**
  28. * The type of experiment. This configures what group the experiment is
  29. * performed on.
  30. *
  31. * A Organization experiment assigns the whole organization.
  32. * A User experiment assigns a single user.
  33. */
  34. type: ExperimentType;
  35. };
  36. // NOTE: The code below is mostly type mechanics to provide utility types
  37. // around experiments for use in experiment helpers. You probably don't need to
  38. // modify this and likely just need to make changes to the experiment list [0]
  39. //
  40. // [0]: app/data/experimentConfig.tsx
  41. type ExperimentList = (typeof experimentList)[number];
  42. type ExperimentSelect<
  43. C extends ExperimentConfig,
  44. N extends ExperimentConfig['key'],
  45. > = C extends {key: N} ? C : never;
  46. type TypeSelect<
  47. C extends ExperimentConfig,
  48. T extends ExperimentConfig['type'],
  49. > = C extends {type: T} ? C : never;
  50. /**
  51. * A mapping of experiment key to the experiment configuration.
  52. */
  53. export type Experiments = {
  54. [E in ExperimentList['key']]: ExperimentSelect<ExperimentList, E>;
  55. };
  56. /**
  57. * Represents an active experiment key
  58. */
  59. export type ExperimentKey = keyof Experiments;
  60. type GetExperimentAssignment<E extends ExperimentList['key']> = {
  61. [K in E]: Experiments[K]['assignments'][number];
  62. };
  63. export type OrgExperiments = GetExperimentAssignment<
  64. TypeSelect<ExperimentList, ExperimentType.ORGANIZATION>['key']
  65. >;
  66. export type UserExperiments = GetExperimentAssignment<
  67. TypeSelect<ExperimentList, ExperimentType.USER>['key']
  68. >;
  69. export type ExperimentAssignment = GetExperimentAssignment<ExperimentList['key']>;