forked from microsoft/vscode-java-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationManager.ts
More file actions
94 lines (78 loc) · 3.33 KB
/
notificationManager.ts
File metadata and controls
94 lines (78 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { commands, ExtensionContext, extensions, window } from "vscode";
import type { IUpgradeIssuesRenderer, UpgradeIssue } from "../type";
import { buildFixPrompt, buildNotificationMessage } from "../utility";
import { Commands } from "../../commands";
import { Settings } from "../../settings";
import { instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";
import { ExtensionName } from "../../constants";
const KEY_PREFIX = 'javaupgrade.notificationManager';
const NEXT_SHOW_TS_KEY = `${KEY_PREFIX}.nextShowTs`;
const BUTTON_TEXT_UPGRADE = "Upgrade Now";
const BUTTON_TEXT_INSTALL_AND_UPGRADE = "Install Extension and Upgrade";
const BUTTON_TEXT_NOT_NOW = "Not Now";
const SECONDS_IN_A_DAY = 24 * 60 * 60;
const SECONDS_COUNT_BEFORE_NOTIFICATION_RESHOW = 10 * SECONDS_IN_A_DAY;
function getNowTs() {
return Number(new Date()) / 1000;
}
class NotificationManager implements IUpgradeIssuesRenderer {
private hasShown = false;
private context?: ExtensionContext;
initialize(context: ExtensionContext) {
this.context = context;
}
async render(issues: UpgradeIssue[]) {
return (instrumentOperation(
"java.dependency.showUpgradeNotification",
async (operationId: string) => {
if (issues.length === 0) {
return;
}
const issue = issues[0];
if (!this.shouldShow()) {
return;
}
if (this.hasShown) {
return;
}
this.hasShown = true;
const hasExtension = !!extensions.getExtension(ExtensionName.APP_MODERNIZATION_UPGRADE_FOR_JAVA);
const prompt = buildFixPrompt(issue);
const notificationMessage = buildNotificationMessage(issue, hasExtension);
const upgradeButtonText = hasExtension ? BUTTON_TEXT_UPGRADE : BUTTON_TEXT_INSTALL_AND_UPGRADE;
const selection = await window.showInformationMessage(
notificationMessage,
upgradeButtonText,
BUTTON_TEXT_NOT_NOW);
sendInfo(operationId, {
operationName: "java.dependency.upgradeNotification.runUpgrade",
choice: selection ?? "",
});
switch (selection) {
case upgradeButtonText: {
commands.executeCommand(Commands.JAVA_UPGRADE_WITH_COPILOT, prompt);
break;
}
case BUTTON_TEXT_NOT_NOW: {
this.setNextShowTs(getNowTs() + SECONDS_COUNT_BEFORE_NOTIFICATION_RESHOW);
break;
}
}
}
))();
}
private shouldShow() {
return Settings.getEnableDependencyCheckup()
&& ((this.getNextShowTs() ?? 0) <= getNowTs());
}
private getNextShowTs() {
return this.context?.globalState.get<number>(NEXT_SHOW_TS_KEY);
}
private setNextShowTs(num: number) {
return this.context?.globalState.update(NEXT_SHOW_TS_KEY, num);
}
}
const notificationManager = new NotificationManager();
export default notificationManager;