addons/isl/src/platform.tsblame
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 type {LazyExoticComponent} from 'react';
b69ab319import type {Comparison} from 'shared/Comparison';
b69ab3110import type {Json} from 'shared/typeUtils';
b69ab3111import type {MessageBus} from './MessageBus';
b69ab3112import type {ThemeColor} from './theme';
b69ab3113import type {
b69ab3114 AbsolutePath,
b69ab3115 Disposable,
b69ab3116 OneIndexedLineNumber,
b69ab3117 PlatformName,
b69ab3118 RepoRelativePath,
b69ab3119 ServerToClientMessage,
b69ab3120} from './types';
b69ab3121
b69ab3122import {browserPlatform} from './BrowserPlatform';
b69ab3123import type {CodeReviewIssue} from './firstPassCodeReview/types';
b69ab3124
b69ab3125export type InitialParamKeys = 'token' | string;
b69ab3126
b69ab3127/**
b69ab3128 * Platform-specific API for each target: vscode extension, electron standalone, browser, ...
b69ab3129 */
b69ab3130export interface Platform {
b69ab3131 platformName: PlatformName;
b69ab3132 confirm(message: string, details?: string): Promise<boolean>;
b69ab3133 openFile(path: RepoRelativePath, options?: {line?: OneIndexedLineNumber}): void;
b69ab3134 openFiles(paths: ReadonlyArray<RepoRelativePath>, options?: {line?: OneIndexedLineNumber}): void;
b69ab3135 canCustomizeFileOpener: boolean;
b69ab3136 openContainingFolder?(path: RepoRelativePath): void;
b69ab3137 openDiff?(path: RepoRelativePath, comparison: Comparison): void;
b69ab3138 openExternalLink(url: string): void;
b69ab3139 clipboardCopy(text: string, html?: string): void;
b69ab3140 chooseFile?(title: string, multi: boolean): Promise<Array<File>>;
b69ab3141 /** Whether to ask to configure an external merge tool. Useful for standalone platforms, but not embedded ones like vscode. */
b69ab3142 upsellExternalMergeTool: boolean;
b69ab3143 /**
b69ab3144 * Get stored data from local persistent cache (usually browser local storage).
b69ab3145 * Note: Some platforms may not support this (e.g. browser with localStorage disabled),
b69ab3146 * or it may not be persisted indefinitely---usual localStorage caveats apply.
b69ab3147 */
b69ab3148 getPersistedState<T extends Json>(key: string): T | null;
b69ab3149 /** see getPersistedState */
b69ab3150 setPersistedState<T extends Json>(key: string, value: T | undefined): void;
b69ab3151 /** see getPersistedState */
b69ab3152 clearPersistedState(): void;
b69ab3153 /** see getPersistedState */
b69ab3154 getAllPersistedState(): Json | undefined;
b69ab3155
b69ab3156 handleServerMessage?: (message: ServerToClientMessage) => void;
b69ab3157
b69ab3158 openDedicatedComparison?: (comparison: Comparison) => Promise<boolean>;
b69ab3159
b69ab3160 /**
b69ab3161 * Component representing additional buttons/info in the cwd menu,
b69ab3162 * used to show a button or hint about how to add more cwds.
b69ab3163 * Note: This should be lazy-loaded via `React.lazy()` so that implementations
b69ab3164 * may import any files without worrying about the platform being set up yet or not.
b69ab3165 */
b69ab3166 AddMoreCwdsHint?: LazyExoticComponent<() => JSX.Element>;
b69ab3167
b69ab3168 /** Platform-specific settings, such as how ISL panels work */
b69ab3169 Settings?: LazyExoticComponent<() => JSX.Element>;
b69ab3170
b69ab3171 theme?: {
b69ab3172 getTheme(): ThemeColor;
b69ab3173 getThemeName?(): string | undefined;
b69ab3174 onDidChangeTheme(callback: (theme: ThemeColor) => unknown): Disposable;
b69ab3175 resetCSS?: string;
b69ab3176 };
b69ab3177
b69ab3178 /** If the platform has a notion of pending edits (typically from an AI), methods for listening and resolving them. */
b69ab3179 suggestedEdits?: {
b69ab3180 /** listen for changes to edits so ISL can confirm edits before taking actions. */
b69ab3181 onDidChangeSuggestedEdits(callback: (suggestedEdits: Array<AbsolutePath>) => void): Disposable;
b69ab3182 /** Accepts/Rejects edits */
b69ab3183 resolveSuggestedEdits(action: 'accept' | 'reject', files?: Array<AbsolutePath>): void;
b69ab3184 };
b69ab3185
b69ab3186 messageBus: MessageBus;
b69ab3187 /** In browser-like platforms, some ISL parameters are passed via URL query params */
b69ab3188 initialUrlParams?: Map<InitialParamKeys, string>;
b69ab3189
b69ab3190 /** If the platform has a notion of AI code review, methods for listening to them. */
b69ab3191 aiCodeReview?: {
b69ab3192 /** listen for new comments so ISL can render them */
b69ab3193 onDidChangeAIReviewComments(
b69ab3194 callback: (aiReviewComments: Array<CodeReviewIssue>) => void,
b69ab3195 ): Disposable;
b69ab3196 };
b69ab3197}
b69ab3198
b69ab3199declare global {
b69ab31100 interface Window {
b69ab31101 islPlatform?: Platform;
b69ab31102 }
b69ab31103}
b69ab31104
b69ab31105// [!] NOTE: On some platforms (vscode), this file is replaced at bundle time with a platform-specific implementation
b69ab31106// of the Platform interface.
b69ab31107// This file should have no other side effects than exporting the platform.
b69ab31108
b69ab31109// However, non-vscode but non-browser platforms are defined by setting window.islPlatform
b69ab31110// before the main ISL script loads.
b69ab31111
b69ab31112/** The ISL client Platform. This may be BrowserPlatform, VSCodeWebviewPlatform, or another platforms, determined at runtime. */
b69ab31113const platform = window.islPlatform ?? browserPlatform;
b69ab31114window.islPlatform = platform;
b69ab31115
b69ab31116export default platform;