File size: 5,860 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { ConfigData } from '@automattic/create-calypso-config/src';
// The JSON is filtered by `apps/stats/filter-json-config-loader.js`.
import productionConfig from '../../../../config/production.json';

export class ConfigApi extends Function {
	// Holds the config data.
	configData: ConfigData;
	_bound: ConfigApi;

	constructor() {
		/**
		 * Make instances callable.
		 */
		super( '...args', 'return this._bound._call(...args)' );
		this.configData = {};
		this._bound = this.bind( this );
		return this._bound;
	}

	/**
	 * Make instances callable.
	 */
	_call( key: string ) {
		if ( key in this.configData ) {
			return this.configData[ key ];
		}

		if ( 'development' === process.env.NODE_ENV ) {
			throw new ReferenceError(
				`Could not find config value for key '${ key }'\n` +
					"Please make sure that if you need it then it has a default value assigned in 'config/_shared.json'"
			);
		}

		return undefined;
	}

	/**
	 * Init config data
	 */
	init( configKey = 'configData' ) {
		/**
		 * Manages config flags for various deployment builds
		 * @module config/index
		 */
		if ( 'undefined' === typeof window ) {
			throw new Error( 'Trying to initialize the configuration outside of a browser context.' );
		}

		if ( ! ( window as { [ key: string ]: any } )[ configKey ] ) {
			if ( 'development' === process.env.NODE_ENV ) {
				// eslint-disable-next-line no-console
				console.error(
					'%cNo configuration was found: ' +
						'%cPlease see ' +
						'%cpackages/calypso-config/README.md ' +
						'%cfor more information.',
					'color: red; font-size: 120%', // error prefix
					'color: white;', // message
					'color: #0267ff;', // calypso-config README.md file reference
					'color: white' // message
				);
			}
		}
		// Fallback to window.configData for backwards compatibility.
		this.configData =
			( window as unknown as Record< string, ConfigData > )[ configKey ] ??
			window.configData ??
			this.configData;
		this._overrideConfigDataFeatures();
		this._applyUrlFlags();
	}

	getConfigData() {
		return this.configData;
	}

	setConfigData( data: ConfigData ) {
		this.configData = data;
	}

	// The following public methods are all originated from https://github.com/Automattic/wp-calypso/blob/ca7d8fe3e0a5fb87b0659fbab659078ebbfbc7be/packages/create-calypso-config/src/index.ts
	isEnabled( feature: string ): boolean {
		return ( this.configData.features && !! this.configData.features[ feature ] ) || false;
	}

	enabledFeatures(): string[] {
		if ( ! this.configData.features ) {
			return [];
		}
		return Object.entries( this.configData.features ).reduce(
			( enabled, [ feature, isEnabled ] ) => ( isEnabled ? [ ...enabled, feature ] : enabled ),
			[] as string[]
		);
	}

	enable( feature: string ) {
		if ( this.configData.features ) {
			this.configData.features[ feature ] = true;
		}
	}

	disable( feature: string ) {
		if ( this.configData.features ) {
			this.configData.features[ feature ] = false;
		}
	}

	// Copied from https://github.com/Automattic/wp-calypso/blob/ca7d8fe3e0a5fb87b0659fbab659078ebbfbc7be/apps/odyssey-stats/src/load-config.js
	_overrideConfigDataFeatures() {
		// Set is_running_in_jetpack_site to true if not specified (undefined or null).
		productionConfig.features.is_running_in_jetpack_site =
			this.configData.features?.is_running_in_jetpack_site ?? true;

		// The option enables loading of the whole translation file, and could be optimized by setting it to `true`, which needs the translation chunks in place.
		// @see https://github.com/Automattic/wp-calypso/blob/trunk/docs/translation-chunks.md
		productionConfig.features[ 'use-translation-chunks' ] = false;

		// Note: configData is hydrated in https://github.com/Automattic/jetpack/blob/d4d0f987cbf63a864b03b542b7813aabe87e0ed3/projects/packages/stats-admin/src/class-dashboard.php#L214
		this.configData.features = productionConfig.features;

		// Set a flag to identify if the app is running in WP Admin.
		// `is_running_in_jetpack_site` now means whether the app calls public-api directly or use the Jetpack ones.
		// For Simple sites running Odyssey Stats, `is_running_in_jetpack_site` is true and `is_odyssey` is false.
		this.configData.features.is_odyssey = true;

		// Sets the Blaze Dashboard path prefix.
		this.configData.advertising_dashboard_path_prefix = '/advertising';
	}

	// Copied from https://github.com/Automattic/wp-calypso/blob/ca7d8fe3e0a5fb87b0659fbab659078ebbfbc7be/packages/calypso-config/src/index.ts#L60
	_applyFlags( flagsString: string, modificationMethod: string ) {
		const flags = flagsString.split( ',' );
		flags.forEach( ( flagRaw ) => {
			const flag = flagRaw.replace( /^[-+]/, '' );
			const enabled = ! /^-/.test( flagRaw );
			if ( this.configData.features ) {
				this.configData.features[ flag ] = enabled;
				// eslint-disable-next-line no-console
				console.log(
					'%cConfig flag %s via %s: %s',
					'font-weight: bold;',
					enabled ? 'enabled' : 'disabled',
					modificationMethod,
					flag
				);
			}
		} );
	}

	/**
	 * Apply feature flags from the URL and Hash.
	 */
	_applyUrlFlags() {
		const UrlMatch =
			document.location.search && document.location.search.match( /[?&]flags=([^&]+)(&|$)/ );
		if ( UrlMatch ) {
			this._applyFlags( decodeURIComponent( UrlMatch[ 1 ] ), 'URL' );
		}
		const hashMatch =
			document.location.hash && document.location.hash.match( /[?&]flags=([^&]+)(&|$)/ );
		if ( hashMatch ) {
			this._applyFlags( decodeURIComponent( hashMatch[ 1 ] ), 'HASH' );
		}
	}
}

export function createOdysseyConfigFromKey( configKey = 'configData' ) {
	const configApi = new ConfigApi();
	configApi.init( configKey );
	return configApi;
}

export default function createOdysseyConfigFromConfigData( configData: ConfigData ) {
	const configApi = new ConfigApi();
	configApi.setConfigData( configData );
	return configApi;
}