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