1.6 KB52 lines
Blame
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
8import type {Comparison} from 'shared/Comparison';
9import type {Hash, RepoRelativePath} from './types';
10
11import {revsetForComparison} from 'shared/Comparison';
12import serverAPI from './ClientToServerAPI';
13import {configBackedAtom} from './jotaiUtils';
14import platform from './platform';
15import {copyAndShowToast, showToast} from './toast';
16
17export const supportsBrowseUrlForHash = configBackedAtom(
18 'fbcodereview.code-browser-url',
19 /* default */ false,
20 /* readonly */ true,
21 /* use raw value */ true,
22);
23
24export async function openBrowseUrlForHash(hash: Hash) {
25 serverAPI.postMessage({type: 'getRepoUrlAtHash', revset: hash});
26 const msg = await serverAPI.nextMessageMatching('gotRepoUrlAtHash', () => true);
27
28 const url = msg.url;
29 if (url.error) {
30 showToast('Failed to get repo URL to browse', {durationMs: 5000});
31 return;
32 } else if (url.value == null) {
33 return;
34 }
35 platform.openExternalLink(url.value);
36}
37
38export async function copyUrlForFile(path: RepoRelativePath, comparison: Comparison) {
39 const revset = revsetForComparison(comparison);
40 serverAPI.postMessage({type: 'getRepoUrlAtHash', revset, path});
41 const msg = await serverAPI.nextMessageMatching('gotRepoUrlAtHash', () => true);
42
43 const url = msg.url;
44 if (url.error) {
45 showToast('Failed to get repo URL to copy', {durationMs: 5000});
46 return;
47 } else if (url.value == null) {
48 return;
49 }
50 copyAndShowToast(url.value, undefined);
51}
52