|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// eslint-disable-next-line max-classes-per-file |
| 5 | +import { inject, named } from 'inversify'; |
| 6 | +import { DiagnosticSeverity, UIKind } from 'vscode'; |
| 7 | +import * as querystring from 'querystring'; |
| 8 | +import { IDisposableRegistry, Resource } from '../../../common/types'; |
| 9 | +import { ExtensionSurveyBanner } from '../../../common/utils/localize'; |
| 10 | +import { IServiceContainer } from '../../../ioc/types'; |
| 11 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 12 | +import { DiagnosticCodes } from '../constants'; |
| 13 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 14 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 15 | +import { IApplicationEnvironment } from '../../../common/application/types'; |
| 16 | +import { IPlatformService } from '../../../common/platform/types'; |
| 17 | +import { IDiagnosticsCommandFactory } from '../commands/types'; |
| 18 | + |
| 19 | +export class MPLSSurveyDiagnostic extends BaseDiagnostic { |
| 20 | + constructor(message: string, resource: Resource) { |
| 21 | + super( |
| 22 | + DiagnosticCodes.MPLSSurveyDiagnostic, |
| 23 | + message, |
| 24 | + DiagnosticSeverity.Information, |
| 25 | + DiagnosticScope.Global, |
| 26 | + resource, |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export const MPLSSurveyDiagnosticServiceId = 'MPLSSurveyDiagnosticServiceId'; |
| 32 | + |
| 33 | +export class MPLSSurveyDiagnosticService extends BaseDiagnosticsService { |
| 34 | + constructor( |
| 35 | + @inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 36 | + @inject(IDiagnosticHandlerService) |
| 37 | + @named(DiagnosticCommandPromptHandlerServiceId) |
| 38 | + protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>, |
| 39 | + @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry, |
| 40 | + @inject(IApplicationEnvironment) private appEnvironment: IApplicationEnvironment, |
| 41 | + @inject(IPlatformService) private platformService: IPlatformService, |
| 42 | + ) { |
| 43 | + super([DiagnosticCodes.MPLSSurveyDiagnostic], serviceContainer, disposableRegistry, true); |
| 44 | + } |
| 45 | + |
| 46 | + public async diagnose(resource: Resource): Promise<IDiagnostic[]> { |
| 47 | + if (this.appEnvironment.uiKind === UIKind?.Web) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + return [new MPLSSurveyDiagnostic(ExtensionSurveyBanner.mplsMessage(), resource)]; |
| 52 | + } |
| 53 | + |
| 54 | + protected async onHandle(diagnostics: IDiagnostic[]): Promise<void> { |
| 55 | + if (diagnostics.length === 0 || !this.canHandle(diagnostics[0])) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const diagnostic = diagnostics[0]; |
| 60 | + if (await this.filterService.shouldIgnoreDiagnostic(diagnostic.code)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 65 | + |
| 66 | + await this.messageService.handle(diagnostic, { |
| 67 | + commandPrompts: [ |
| 68 | + { |
| 69 | + prompt: ExtensionSurveyBanner.bannerLabelYes(), |
| 70 | + command: { |
| 71 | + diagnostic, |
| 72 | + invoke: () => this.launchSurvey(diagnostic), |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + prompt: ExtensionSurveyBanner.maybeLater(), |
| 77 | + }, |
| 78 | + { |
| 79 | + prompt: ExtensionSurveyBanner.bannerLabelNo(), |
| 80 | + command: commandFactory.createCommand(diagnostic, { |
| 81 | + type: 'ignore', |
| 82 | + options: DiagnosticScope.Global, |
| 83 | + }), |
| 84 | + }, |
| 85 | + ], |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private async launchSurvey(diagnostic: IDiagnostic) { |
| 90 | + const query = querystring.stringify({ |
| 91 | + o: encodeURIComponent(this.platformService.osType), // platform |
| 92 | + v: encodeURIComponent(this.appEnvironment.vscodeVersion), |
| 93 | + e: encodeURIComponent(this.appEnvironment.packageJson.version), // extension version |
| 94 | + m: encodeURIComponent(this.appEnvironment.sessionId), |
| 95 | + }); |
| 96 | + const url = `https://aka.ms/mpls-experience-survey?${query}`; |
| 97 | + |
| 98 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 99 | + await commandFactory.createCommand(diagnostic, { type: 'ignore', options: DiagnosticScope.Global }).invoke(); |
| 100 | + await commandFactory.createCommand(diagnostic, { type: 'launch', options: url }).invoke(); |
| 101 | + } |
| 102 | +} |
0 commit comments