Skip to content

Commit

Permalink
Fix survey prompt for Native notebooks (#4021)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored Nov 25, 2020
1 parent 4e486cb commit a9e6437
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions src/client/datascience/insidersNativeNotebookSurveyBanner.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { inject, injectable } from 'inversify';
import { env, UIKind } from 'vscode';
import { UIKind } from 'vscode';
import { IApplicationEnvironment, IApplicationShell, IVSCodeNotebook } from '../common/application/types';
import { Experiments } from '../common/experiments/groups';
import { IBrowserService, IExperimentService, IJupyterExtensionBanner, IPersistentStateFactory } from '../common/types';
import {
IBrowserService,
IExperimentService,
IJupyterExtensionBanner,
IPersistentState,
IPersistentStateFactory
} from '../common/types';
import * as localize from '../common/utils/localize';
import { MillisecondsInADay } from '../constants';

export enum InsidersNotebookSurveyStateKeys {
ShowBanner = 'ShowInsidersNotebookSurveyBanner',
Expand All @@ -18,21 +25,44 @@ enum DSSurveyLabelIndex {

const NotebookOpenThreshold = 5;
const NotebookExecutionThreshold = 100;
export type ShowBannerWithExpiryTime = {
/**
* This value is not used.
* We are only interested in the value for `expiry`.
* This structure is based on the old data for older customers when we used PersistentState class.
*/
data: boolean;
/**
* If this is value `undefined`, then prompt can be displayed.
* If this value is `a number`, then a prompt was displayed at one point in time &
* we need to wait for Date.now() to be greater than that number to display it again.
*/
expiry?: number;
};

@injectable()
export class InsidersNativeNotebooksSurveyBanner implements IJupyterExtensionBanner {
private get enabled(): boolean {
return (
this.persistentState.createGlobalPersistentState<boolean>(InsidersNotebookSurveyStateKeys.ShowBanner, true)
.value && env.uiKind !== UIKind?.Web
);
if (this.applicationEnvironment.uiKind !== UIKind.Desktop) {
return false;
}
if (!this.showBannerState.value.expiry) {
return true;
}
return this.showBannerState.value.expiry! < Date.now();
}
private disabledInCurrentSession: boolean = false;

private disabledInCurrentSession = false;

private bannerMessage: string = localize.InsidersNativeNotebooksSurveyBanner.bannerMessage();

private bannerLabels: string[] = [
localize.DataScienceSurveyBanner.bannerLabelYes(),
localize.DataScienceSurveyBanner.bannerLabelNo()
];

private readonly showBannerState: IPersistentState<ShowBannerWithExpiryTime>;

private readonly surveyLink: string;

constructor(
Expand All @@ -42,9 +72,15 @@ export class InsidersNativeNotebooksSurveyBanner implements IJupyterExtensionBan
@inject(IVSCodeNotebook) vscodeNotebook: IVSCodeNotebook,
@inject(IExperimentService) private experimentService: IExperimentService,
@inject(IApplicationEnvironment) private applicationEnvironment: IApplicationEnvironment,
surveyLink: string = 'https://aka.ms/vscjupyternb'
surveyLink = 'https://aka.ms/vscjupyternb'
) {
this.surveyLink = surveyLink;
this.showBannerState = this.persistentState.createGlobalPersistentState<ShowBannerWithExpiryTime>(
InsidersNotebookSurveyStateKeys.ShowBanner,
{
data: true
}
);
vscodeNotebook.onDidOpenNotebookDocument(this.openedNotebook.bind(this));
}

Expand Down Expand Up @@ -115,10 +151,10 @@ export class InsidersNativeNotebooksSurveyBanner implements IJupyterExtensionBan
}

private async disable(monthsTillNextPrompt: number) {
const expiration = monthsTillNextPrompt * 31 * 24 * 60 * 60 * 1000;
await this.persistentState
.createGlobalPersistentState<boolean>(InsidersNotebookSurveyStateKeys.ShowBanner, false, expiration)
.updateValue(false);
await this.showBannerState.updateValue({
expiry: monthsTillNextPrompt * 31 * MillisecondsInADay + Date.now(),
data: true
});
}

private async openedNotebook() {
Expand Down

0 comments on commit a9e6437

Please sign in to comment.