4.5 KB117 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 {LazyExoticComponent} from 'react';
9import type {Comparison} from 'shared/Comparison';
10import type {Json} from 'shared/typeUtils';
11import type {MessageBus} from './MessageBus';
12import type {ThemeColor} from './theme';
13import type {
14 AbsolutePath,
15 Disposable,
16 OneIndexedLineNumber,
17 PlatformName,
18 RepoRelativePath,
19 ServerToClientMessage,
20} from './types';
21
22import {browserPlatform} from './BrowserPlatform';
23import type {CodeReviewIssue} from './firstPassCodeReview/types';
24
25export type InitialParamKeys = 'token' | string;
26
27/**
28 * Platform-specific API for each target: vscode extension, electron standalone, browser, ...
29 */
30export interface Platform {
31 platformName: PlatformName;
32 confirm(message: string, details?: string): Promise<boolean>;
33 openFile(path: RepoRelativePath, options?: {line?: OneIndexedLineNumber}): void;
34 openFiles(paths: ReadonlyArray<RepoRelativePath>, options?: {line?: OneIndexedLineNumber}): void;
35 canCustomizeFileOpener: boolean;
36 openContainingFolder?(path: RepoRelativePath): void;
37 openDiff?(path: RepoRelativePath, comparison: Comparison): void;
38 openExternalLink(url: string): void;
39 clipboardCopy(text: string, html?: string): void;
40 chooseFile?(title: string, multi: boolean): Promise<Array<File>>;
41 /** Whether to ask to configure an external merge tool. Useful for standalone platforms, but not embedded ones like vscode. */
42 upsellExternalMergeTool: boolean;
43 /**
44 * Get stored data from local persistent cache (usually browser local storage).
45 * Note: Some platforms may not support this (e.g. browser with localStorage disabled),
46 * or it may not be persisted indefinitely---usual localStorage caveats apply.
47 */
48 getPersistedState<T extends Json>(key: string): T | null;
49 /** see getPersistedState */
50 setPersistedState<T extends Json>(key: string, value: T | undefined): void;
51 /** see getPersistedState */
52 clearPersistedState(): void;
53 /** see getPersistedState */
54 getAllPersistedState(): Json | undefined;
55
56 handleServerMessage?: (message: ServerToClientMessage) => void;
57
58 openDedicatedComparison?: (comparison: Comparison) => Promise<boolean>;
59
60 /**
61 * Component representing additional buttons/info in the cwd menu,
62 * used to show a button or hint about how to add more cwds.
63 * Note: This should be lazy-loaded via `React.lazy()` so that implementations
64 * may import any files without worrying about the platform being set up yet or not.
65 */
66 AddMoreCwdsHint?: LazyExoticComponent<() => JSX.Element>;
67
68 /** Platform-specific settings, such as how ISL panels work */
69 Settings?: LazyExoticComponent<() => JSX.Element>;
70
71 theme?: {
72 getTheme(): ThemeColor;
73 getThemeName?(): string | undefined;
74 onDidChangeTheme(callback: (theme: ThemeColor) => unknown): Disposable;
75 resetCSS?: string;
76 };
77
78 /** If the platform has a notion of pending edits (typically from an AI), methods for listening and resolving them. */
79 suggestedEdits?: {
80 /** listen for changes to edits so ISL can confirm edits before taking actions. */
81 onDidChangeSuggestedEdits(callback: (suggestedEdits: Array<AbsolutePath>) => void): Disposable;
82 /** Accepts/Rejects edits */
83 resolveSuggestedEdits(action: 'accept' | 'reject', files?: Array<AbsolutePath>): void;
84 };
85
86 messageBus: MessageBus;
87 /** In browser-like platforms, some ISL parameters are passed via URL query params */
88 initialUrlParams?: Map<InitialParamKeys, string>;
89
90 /** If the platform has a notion of AI code review, methods for listening to them. */
91 aiCodeReview?: {
92 /** listen for new comments so ISL can render them */
93 onDidChangeAIReviewComments(
94 callback: (aiReviewComments: Array<CodeReviewIssue>) => void,
95 ): Disposable;
96 };
97}
98
99declare global {
100 interface Window {
101 islPlatform?: Platform;
102 }
103}
104
105// [!] NOTE: On some platforms (vscode), this file is replaced at bundle time with a platform-specific implementation
106// of the Platform interface.
107// This file should have no other side effects than exporting the platform.
108
109// However, non-vscode but non-browser platforms are defined by setting window.islPlatform
110// before the main ISL script loads.
111
112/** The ISL client Platform. This may be BrowserPlatform, VSCodeWebviewPlatform, or another platforms, determined at runtime. */
113const platform = window.islPlatform ?? browserPlatform;
114window.islPlatform = platform;
115
116export default platform;
117