addons/vscode/webview/state.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {readAtom, writeAtom} from 'isl/src/jotaiUtils';
b69ab319import {registerDisposable} from 'isl/src/utils';
b69ab3110import {atom} from 'jotai';
b69ab3111import serverAPI from '../../isl/src/ClientToServerAPI';
b69ab3112
b69ab3113/** Should match the sapling.comparisonPanelMode enum in package.json */
b69ab3114export enum ComparisonPanelMode {
b69ab3115 Auto = 'Auto',
b69ab3116 AlwaysOpenPanel = 'Always Separate Panel',
b69ab3117}
b69ab3118
b69ab3119export const comparisonPanelMode = atom<undefined | ComparisonPanelMode>(ComparisonPanelMode.Auto);
b69ab3120serverAPI.postMessage({
b69ab3121 type: 'platform/subscribeToVSCodeConfig',
b69ab3122 config: 'sapling.comparisonPanelMode',
b69ab3123});
b69ab3124registerDisposable(
b69ab3125 comparisonPanelMode,
b69ab3126 serverAPI.onMessageOfType('platform/vscodeConfigChanged', config => {
b69ab3127 if (config.config === 'sapling.comparisonPanelMode' && typeof config.value === 'string') {
b69ab3128 writeAtom(comparisonPanelMode, config.value as ComparisonPanelMode);
b69ab3129 }
b69ab3130 }),
b69ab3131 import.meta.hot,
b69ab3132);
b69ab3133
b69ab3134export const setComparisonPanelMode = (mode: ComparisonPanelMode) => {
b69ab3135 // Optimistically set the state locally, and later get rewritten to the same value by the server.
b69ab3136 // NOTE: This relies on the server responding to these events in-order to ensure eventual consistency.
b69ab3137 writeAtom(comparisonPanelMode, mode);
b69ab3138 serverAPI.postMessage({
b69ab3139 type: 'platform/setVSCodeConfig',
b69ab3140 config: 'sapling.comparisonPanelMode',
b69ab3141 value: mode,
b69ab3142 scope: 'global',
b69ab3143 });
b69ab3144};
b69ab3145
b69ab3146export const getComparisonPanelMode = () => {
b69ab3147 return readAtom(comparisonPanelMode) ?? 'Auto';
b69ab3148};