Skip to content

Commit 1e2f890

Browse files
committed
Fix bugs
1 parent 1a57744 commit 1e2f890

File tree

3 files changed

+103
-30
lines changed

3 files changed

+103
-30
lines changed

src/client/extension.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { EXTENSION_ID, SUCCESS } from "../common/constants";
4444
import { AadIdKey, EnvIdKey, TenantIdKey } from "../common/OneDSLoggerTelemetry/telemetryConstants";
4545
import { PowerPagesAppName, PowerPagesClientName } from "../common/ecs-features/constants";
4646
import { ECSFeaturesClient } from "../common/ecs-features/ecsFeatureClient";
47-
import { getECSOrgLocationValue } from "../common/utilities/Utils";
47+
import { getECSOrgLocationValue, getWorkspaceFolders } from "../common/utilities/Utils";
4848
import { CliAcquisitionContext } from "./lib/CliAcquisitionContext";
4949
import { PreviewSite, SITE_PREVIEW_COMMAND_ID } from "./runtime-site-preview/PreviewSite";
5050

@@ -190,13 +190,8 @@ export async function activate(
190190

191191
let copilotNotificationShown = false;
192192

193-
const workspaceFolders =
194-
vscode.workspace.workspaceFolders?.map(
195-
(fl) => ({ ...fl, uri: fl.uri.fsPath } as WorkspaceFolder)
196-
) || [];
197-
193+
const workspaceFolders = getWorkspaceFolders();
198194

199-
let websiteURL: string | undefined = undefined;
200195
const isSiteRuntimePreviewEnabled = PreviewSite.isSiteRuntimePreviewEnabled();
201196

202197
vscode.commands.executeCommand("setContext", "microsoft.powerplatform.pages.siteRuntimePreviewEnabled", isSiteRuntimePreviewEnabled);
@@ -257,7 +252,7 @@ export async function activate(
257252
}
258253

259254
if (artemisResponse !== null && isSiteRuntimePreviewEnabled) {
260-
websiteURL = await PreviewSite.getWebSiteURL(workspaceFolders, artemisResponse?.stamp, orgDetails.EnvironmentId, _telemetry);
255+
await PreviewSite.loadSiteUrl(workspaceFolders, artemisResponse?.stamp, orgDetails.EnvironmentId, _telemetry);
261256
}
262257

263258
})
@@ -282,17 +277,17 @@ export async function activate(
282277

283278
_telemetry.sendTelemetryEvent("EnableSiteRuntimePreview", {
284279
isEnabled: isSiteRuntimePreviewEnabled.toString(),
285-
websiteURL: websiteURL || "undefined"
280+
websiteURL: PreviewSite.getSiteUrl() || "undefined"
286281
});
287282
oneDSLoggerWrapper.getLogger().traceInfo("EnableSiteRuntimePreview", {
288283
isEnabled: isSiteRuntimePreviewEnabled.toString(),
289-
websiteURL: websiteURL || "undefined"
284+
websiteURL: PreviewSite.getSiteUrl() || "undefined"
290285
});
291286

292287
_context.subscriptions.push(
293288
vscode.commands.registerCommand(
294289
SITE_PREVIEW_COMMAND_ID,
295-
async () => await PreviewSite.handlePreviewRequest(isSiteRuntimePreviewEnabled, websiteURL, _telemetry)
290+
async () => await PreviewSite.handlePreviewRequest(isSiteRuntimePreviewEnabled, _telemetry, pacTerminal)
296291
)
297292
);
298293

src/client/runtime-site-preview/PreviewSite.ts

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,42 @@ import { PPAPIService } from '../../common/services/PPAPIService';
1414
import { VSCODE_EXTENSION_GET_WEBSITE_RECORD_ID_EMPTY } from '../../common/services/TelemetryConstants';
1515
import { EDGE_TOOLS_EXTENSION_ID } from '../../common/constants';
1616
import { oneDSLoggerWrapper } from "../../common/OneDSLoggerTelemetry/oneDSLoggerWrapper";
17-
import { showProgressWithNotification } from '../../common/utilities/Utils';
17+
import { getWorkspaceFolders, showProgressWithNotification } from '../../common/utilities/Utils';
18+
import { PacTerminal } from '../lib/PacTerminal';
19+
import { initializeOrgDetails } from '../../common/utilities/OrgHandlerUtils';
20+
import { ArtemisService } from '../../common/services/ArtemisService';
1821

1922
export const SITE_PREVIEW_COMMAND_ID = "microsoft.powerplatform.pages.preview-site";
2023

2124
export class PreviewSite {
25+
private static _websiteUrl: string | undefined = undefined;
26+
2227
static isSiteRuntimePreviewEnabled(): boolean {
2328
const enableSiteRuntimePreview = ECSFeaturesClient.getConfig(EnableSiteRuntimePreview).enableSiteRuntimePreview
2429

2530
if (enableSiteRuntimePreview === undefined) {
2631
return false;
2732
}
2833

29-
return enableSiteRuntimePreview;
34+
return true;
35+
}
36+
37+
static async loadSiteUrl(
38+
workspaceFolders: WorkspaceFolder[],
39+
stamp: ServiceEndpointCategory,
40+
envId: string,
41+
telemetry: ITelemetry)
42+
: Promise<void> {
43+
const websiteUrl = await PreviewSite.getWebSiteUrl(workspaceFolders, stamp, envId, telemetry);
44+
45+
this._websiteUrl = websiteUrl;
3046
}
3147

32-
static async getWebSiteURL(workspaceFolders: WorkspaceFolder[], stamp: ServiceEndpointCategory, envId: string, telemetry: ITelemetry): Promise<string> {
48+
static getSiteUrl(): string | undefined {
49+
return this._websiteUrl;
50+
}
51+
52+
private static async getWebSiteUrl(workspaceFolders: WorkspaceFolder[], stamp: ServiceEndpointCategory, envId: string, telemetry: ITelemetry): Promise<string> {
3353
const websiteRecordId = getWebsiteRecordId(workspaceFolders, telemetry);
3454
if (!websiteRecordId) {
3555
telemetry.sendTelemetryEvent(VSCODE_EXTENSION_GET_WEBSITE_RECORD_ID_EMPTY, {
@@ -74,9 +94,15 @@ export class PreviewSite {
7494
await settings.update('defaultUrl', currentDefaultUrl);
7595
}
7696
);
97+
98+
await vscode.window.showInformationMessage(vscode.l10n.t('The preview shown is for published changes.'));
7799
}
78100

79-
static async handlePreviewRequest(isSiteRuntimePreviewEnabled: boolean, websiteURL: string | undefined, telemetry: ITelemetry) {
101+
static async handlePreviewRequest(
102+
isSiteRuntimePreviewEnabled: boolean,
103+
telemetry: ITelemetry,
104+
pacTerminal: PacTerminal) {
105+
80106
telemetry.sendTelemetryEvent("StartCommand", {
81107
commandId: SITE_PREVIEW_COMMAND_ID,
82108
});
@@ -94,25 +120,66 @@ export class PreviewSite {
94120
return;
95121
}
96122

97-
if (websiteURL === undefined) {
123+
if (this._websiteUrl === undefined) {
98124
await vscode.window.showWarningMessage(vscode.l10n.t("Initializing site preview. Please try again after few seconds."));
99125
return;
100126
}
101127

102-
if (websiteURL === "") {
103-
const shouldInitiateLogin = await vscode.window.showErrorMessage(
104-
vscode.l10n.t(
105-
`Website not found in the environment. Please check the credentials and login with correct account.`
106-
),
107-
vscode.l10n.t('Login')
108-
);
128+
if (this._websiteUrl === "") {
129+
let shouldRepeatLoginFlow = true;
109130

110-
if (shouldInitiateLogin === vscode.l10n.t('Login')) {
111-
await vscode.authentication.getSession(PROVIDER_ID, [], { })
131+
while (shouldRepeatLoginFlow) {
132+
shouldRepeatLoginFlow = await PreviewSite.handleEmptyWebsiteUrl(pacTerminal, telemetry);
112133
}
113-
return;
114134
}
115135

116-
await PreviewSite.launchBrowserAndDevToolsWithinVsCode(websiteURL);
136+
await PreviewSite.launchBrowserAndDevToolsWithinVsCode(this._websiteUrl);
137+
}
138+
139+
private static async handleEmptyWebsiteUrl(pacTerminal: PacTerminal, telemetry: ITelemetry): Promise<boolean> {
140+
const shouldInitiateLogin = await vscode.window.showErrorMessage(
141+
vscode.l10n.t(
142+
`Website not found in the environment. Please check the credentials and login with correct account.`
143+
),
144+
vscode.l10n.t('Login'),
145+
vscode.l10n.t('Cancel')
146+
);
147+
148+
let shouldRepeatLoginFlow = false;
149+
150+
if (shouldInitiateLogin === vscode.l10n.t('Login')) {
151+
await vscode.authentication.getSession(PROVIDER_ID, [], { forceNewSession: true, clearSessionPreference: true });
152+
153+
await showProgressWithNotification(
154+
vscode.l10n.t('Initializing site preview'),
155+
async (progress) => {
156+
progress.report({ message: vscode.l10n.t('Getting org details...') });
157+
158+
const orgDetails = await initializeOrgDetails(false, pacTerminal.getWrapper());
159+
160+
progress.report({ message: vscode.l10n.t('Getting region information...') });
161+
162+
const artemisResponse = await ArtemisService.getArtemisResponse(orgDetails.orgID, telemetry, "");
163+
164+
if (artemisResponse === null || artemisResponse.response === null) {
165+
vscode.window.showErrorMessage(vscode.l10n.t("Failed to get website endpoint. Please try again later"));
166+
return;
167+
}
168+
169+
progress.report({ message: vscode.l10n.t('Getting website endpoint...') });
170+
171+
const websiteUrl = await PreviewSite.getWebSiteUrl(getWorkspaceFolders(), artemisResponse?.stamp, orgDetails.environmentID, telemetry);
172+
173+
if (websiteUrl === "") {
174+
shouldRepeatLoginFlow = true;
175+
}
176+
else {
177+
this._websiteUrl = websiteUrl;
178+
}
179+
}
180+
);
181+
}
182+
183+
return shouldRepeatLoginFlow;
117184
}
118185
}

src/common/utilities/Utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { oneDSLoggerWrapper } from "../OneDSLoggerTelemetry/oneDSLoggerWrapper";
1717
import { bapServiceAuthentication } from "../services/AuthenticationProvider";
1818
import { BAP_API_VERSION, BAP_ENVIRONMENT_LIST_URL, BAP_SERVICE_ENDPOINT, ServiceEndpointCategory } from "../services/Constants";
1919
import { VSCODE_EXTENSION_GET_ENV_LIST_SUCCESS, VSCODE_EXTENSION_GET_ENV_LIST_FAILED, VSCODE_EXTENSION_GET_BAP_ENDPOINT_UNSUPPORTED_REGION } from "../services/TelemetryConstants";
20+
import { WorkspaceFolder } from "vscode-languageserver";
21+
import { Progress } from "vscode";
2022

2123
export function getSelectedCode(editor: vscode.TextEditor): string {
2224
if (!editor) {
@@ -104,13 +106,16 @@ export async function showInputBoxAndGetOrgUrl() {
104106
});
105107
}
106108

107-
export async function showProgressWithNotification<T>(title: string, task: () => Promise<T>): Promise<T> {
109+
export async function showProgressWithNotification<T>(title: string, task: (progress: Progress<{
110+
message?: string;
111+
increment?: number;
112+
}>) => Promise<T>): Promise<T> {
108113
return await vscode.window.withProgress({
109114
location: vscode.ProgressLocation.Notification,
110115
title: title,
111116
cancellable: false
112-
}, async () => {
113-
return await task();
117+
}, async (progress) => {
118+
return await task(progress);
114119
});
115120
}
116121

@@ -397,3 +402,9 @@ export function getBAPEndpoint(serviceEndpointStamp: ServiceEndpointCategory, te
397402

398403
return BAP_SERVICE_ENDPOINT.replace('{rootURL}', bapEndpoint)
399404
}
405+
406+
export function getWorkspaceFolders(): WorkspaceFolder[] {
407+
return vscode.workspace.workspaceFolders?.map(
408+
(fl) => ({ ...fl, uri: fl.uri.fsPath } as WorkspaceFolder)
409+
) || [];
410+
}

0 commit comments

Comments
 (0)