File size: 4,054 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { useRef, useEffect, useReducer } from 'react';
import * as React from 'react';
import type { ExPlatClient, ExperimentAssignment } from '@automattic/explat-client';

export interface ExperimentOptions {
	/**
	 * Determines whether a participant is currenlty eligible for an assignment.
	 * - Only loads the experimentAssignment if isEligible is true.
	 * - Only returns the experimentAssignment if isEligible is true.
	 */
	isEligible?: boolean;
}

const defaultExperimentOptions = {
	isEligible: true,
};

export interface ExPlatClientReactHelpers {
	/**
	 * An ExPlat useExperiment hook.
	 *
	 * NOTE: Doesn't obey ExperimentAssignment TTL in order to keep stable UX.
	 * @returns [isExperimentAssignmentLoading, ExperimentAssignment | null]
	 */
	useExperiment: (
		experimentName: string,
		options?: ExperimentOptions
	) => [ boolean, ExperimentAssignment | null ];

	/**
	 * Experiment component, safest and simplest, should be first choice if usable.
	 */
	Experiment: ( props: {
		name: string;
		defaultExperience: React.ReactNode;
		treatmentExperience: React.ReactNode;
		loadingExperience: React.ReactNode;
		options?: ExperimentOptions;
	} ) => JSX.Element;

	/**
	 * Inject experiment data into a child component.
	 * Use when hooks aren't available.
	 */
	ProvideExperimentData: ( props: {
		children: (
			isLoading: boolean,
			experimentAssignment: ExperimentAssignment | null
		) => JSX.Element;
		name: string;
		options?: ExperimentOptions;
	} ) => JSX.Element;
}

export default function createExPlatClientReactHelpers(
	exPlatClient: ExPlatClient
): ExPlatClientReactHelpers {
	const useExperiment = (
		experimentName: string,
		providedOptions: ExperimentOptions = {}
	): [ boolean, ExperimentAssignment | null ] => {
		const options: Required< ExperimentOptions > = {
			...defaultExperimentOptions,
			...providedOptions,
		};

		// Manual updates to ensure rerendering when we want it:
		// https://legacy.reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate
		const [ , forceUpdate ] = useReducer( ( x ) => x + 1, 0 );
		const previousExperimentNameRef = useRef( experimentName );

		useEffect( () => {
			let isSubscribed = true;
			if ( options.isEligible ) {
				exPlatClient.loadExperimentAssignment( experimentName ).then( () => {
					if ( isSubscribed ) {
						forceUpdate();
					}
				} );
			}
			return () => {
				isSubscribed = false;
			};
		}, [ experimentName, options.isEligible ] );

		if (
			experimentName !== previousExperimentNameRef.current &&
			! previousExperimentNameRef.current.startsWith( 'explat_test' )
		) {
			exPlatClient.config.logError( {
				message: '[ExPlat] useExperiment: experimentName should never change between renders!',
			} );
		}

		if ( ! options.isEligible ) {
			return [ false, null ];
		}

		const maybeExperimentAssignment =
			exPlatClient.dangerouslyGetMaybeLoadedExperimentAssignment( experimentName );
		return [ ! maybeExperimentAssignment, maybeExperimentAssignment ];
	};

	const Experiment = ( {
		defaultExperience,
		treatmentExperience,
		loadingExperience,
		name: experimentName,
		options,
	}: {
		defaultExperience: React.ReactNode;
		treatmentExperience: React.ReactNode;
		loadingExperience: React.ReactNode;
		name: string;
		options?: ExperimentOptions;
	} ) => {
		const [ isLoading, experimentAssignment ] = useExperiment( experimentName, options );
		if ( isLoading ) {
			return <>{ loadingExperience }</>;
		} else if ( ! experimentAssignment?.variationName ) {
			return <>{ defaultExperience }</>;
		}
		return <>{ treatmentExperience }</>;
	};

	const ProvideExperimentData = ( {
		children,
		name: experimentName,
		options,
	}: {
		children: (
			isLoading: boolean,
			experimentAssignment: ExperimentAssignment | null
		) => JSX.Element;
		name: string;
		options?: ExperimentOptions;
	} ) => {
		const [ isLoading, experimentAssignment ] = useExperiment( experimentName, options );
		return children( isLoading, experimentAssignment );
	};

	return {
		useExperiment,
		Experiment,
		ProvideExperimentData,
	};
}