| 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 {readAtom, writeAtom} from 'isl/src/jotaiUtils'; |
| 9 | import {registerDisposable} from 'isl/src/utils'; |
| 10 | import {atom} from 'jotai'; |
| 11 | import serverAPI from '../../isl/src/ClientToServerAPI'; |
| 12 | |
| 13 | /** Should match the sapling.comparisonPanelMode enum in package.json */ |
| 14 | export enum ComparisonPanelMode { |
| 15 | Auto = 'Auto', |
| 16 | AlwaysOpenPanel = 'Always Separate Panel', |
| 17 | } |
| 18 | |
| 19 | export const comparisonPanelMode = atom<undefined | ComparisonPanelMode>(ComparisonPanelMode.Auto); |
| 20 | serverAPI.postMessage({ |
| 21 | type: 'platform/subscribeToVSCodeConfig', |
| 22 | config: 'sapling.comparisonPanelMode', |
| 23 | }); |
| 24 | registerDisposable( |
| 25 | comparisonPanelMode, |
| 26 | serverAPI.onMessageOfType('platform/vscodeConfigChanged', config => { |
| 27 | if (config.config === 'sapling.comparisonPanelMode' && typeof config.value === 'string') { |
| 28 | writeAtom(comparisonPanelMode, config.value as ComparisonPanelMode); |
| 29 | } |
| 30 | }), |
| 31 | import.meta.hot, |
| 32 | ); |
| 33 | |
| 34 | export const setComparisonPanelMode = (mode: ComparisonPanelMode) => { |
| 35 | // Optimistically set the state locally, and later get rewritten to the same value by the server. |
| 36 | // NOTE: This relies on the server responding to these events in-order to ensure eventual consistency. |
| 37 | writeAtom(comparisonPanelMode, mode); |
| 38 | serverAPI.postMessage({ |
| 39 | type: 'platform/setVSCodeConfig', |
| 40 | config: 'sapling.comparisonPanelMode', |
| 41 | value: mode, |
| 42 | scope: 'global', |
| 43 | }); |
| 44 | }; |
| 45 | |
| 46 | export const getComparisonPanelMode = () => { |
| 47 | return readAtom(comparisonPanelMode) ?? 'Auto'; |
| 48 | }; |
| 49 | |