| 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 type {DiffId} from './types'; |
| 9 | |
| 10 | import {atom} from 'jotai'; |
| 11 | import serverAPI from './ClientToServerAPI'; |
| 12 | import {codeReviewProvider} from './codeReview/CodeReviewInfo'; |
| 13 | |
| 14 | /** |
| 15 | * In some cases, we need to explicitly disable message syncing after a failure. |
| 16 | * This setting overrides the default value from the code review provider. |
| 17 | * It's not intended to be set by users nor is it persisted across restarts. |
| 18 | * When this is set, a warning will also be shown to the user. |
| 19 | */ |
| 20 | export const messageSyncingOverrideState = atom<boolean | null>(null); |
| 21 | |
| 22 | /** Whether message syncing is enabled for the current repo. */ |
| 23 | export const messageSyncingEnabledState = atom(get => { |
| 24 | const override = get(messageSyncingOverrideState); |
| 25 | if (override != null) { |
| 26 | return override; |
| 27 | } |
| 28 | const provider = get(codeReviewProvider); |
| 29 | return provider?.enableMessageSyncing ?? false; |
| 30 | }); |
| 31 | |
| 32 | export async function updateRemoteMessage( |
| 33 | diffId: DiffId, |
| 34 | title: string, |
| 35 | description: string, |
| 36 | ): Promise<void> { |
| 37 | serverAPI.postMessage({type: 'updateRemoteDiffMessage', diffId, title, description}); |
| 38 | const response = await serverAPI.nextMessageMatching( |
| 39 | 'updatedRemoteDiffMessage', |
| 40 | msg => msg.diffId === diffId, |
| 41 | ); |
| 42 | if (response.error != null) { |
| 43 | throw new Error(response.error); |
| 44 | } |
| 45 | } |
| 46 | |