| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {atom} from 'jotai'; |
| b69ab31 | | | 9 | import {configBackedAtom, localStorageBackedAtom} from '../jotaiUtils'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | // This config is intended to be controlled remotely. So it's read-only. |
| b69ab31 | | | 12 | const remoteExperimentalFeatures = configBackedAtom<boolean | null>( |
| b69ab31 | | | 13 | 'isl.experimental-features', |
| b69ab31 | | | 14 | false, |
| b69ab31 | | | 15 | true /* read-only */, |
| b69ab31 | | | 16 | ); |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | // 0: Respect remote config. 1: Enable experimental features. 2: Disable experimental features. |
| b69ab31 | | | 19 | const localExperimentalFeatures = localStorageBackedAtom<number>( |
| b69ab31 | | | 20 | 'isl.experimental-features-local-override', |
| b69ab31 | | | 21 | 0, |
| b69ab31 | | | 22 | ); |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | /** |
| b69ab31 | | | 25 | * List of all currently enabled experimental features, as UI labels. |
| b69ab31 | | | 26 | * UI setting to enable experimental features is only shown if this list is non-empty. |
| b69ab31 | | | 27 | */ |
| b69ab31 | | | 28 | export const currentExperimentalFeaturesList: Array<string> = []; |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | /** |
| b69ab31 | | | 31 | * Whether experimental features are enabled. |
| b69ab31 | | | 32 | * Backed by a remote config by default. Can also be set locally. |
| b69ab31 | | | 33 | */ |
| b69ab31 | | | 34 | export const hasExperimentalFeatures = atom( |
| b69ab31 | | | 35 | get => { |
| b69ab31 | | | 36 | const localOverride = get(localExperimentalFeatures); |
| b69ab31 | | | 37 | if (localOverride === 1) { |
| b69ab31 | | | 38 | return true; |
| b69ab31 | | | 39 | } else if (localOverride === 2) { |
| b69ab31 | | | 40 | return false; |
| b69ab31 | | | 41 | } else { |
| b69ab31 | | | 42 | return get(remoteExperimentalFeatures) ?? false; |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | }, |
| b69ab31 | | | 45 | (get, set, update) => { |
| b69ab31 | | | 46 | const newValue = typeof update === 'function' ? update(get(hasExperimentalFeatures)) : update; |
| b69ab31 | | | 47 | set(localExperimentalFeatures, newValue ? 1 : 2); |
| b69ab31 | | | 48 | }, |
| b69ab31 | | | 49 | ); |