Skip to content

Commit 49c98bf

Browse files
committed
added time base proj update notifications
1 parent 7d5bdcb commit 49c98bf

3 files changed

Lines changed: 98 additions & 1 deletion

File tree

src/commitAPI.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ApolloClient, gql, NormalizedCacheObject } from "@apollo/client/core";
22

33
import * as vscode from "vscode";
4+
import { API } from "./@types/git";
5+
import { COMMIT_PROJECT_UDPATE_NOTIFICATION_INTERVAL } from "./common/constants";
46

57
export enum SubscriptionType {
68
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -207,4 +209,73 @@ export class CommitAPI {
207209
throw new Error("Error updating project");
208210
}
209211
}
212+
213+
public async setupConfig(context: vscode.ExtensionContext): Promise<void> {
214+
// Setup up project update notification interval
215+
context.workspaceState.update(
216+
"commitNotificationInterval",
217+
COMMIT_PROJECT_UDPATE_NOTIFICATION_INTERVAL
218+
);
219+
}
220+
221+
public async showAddProjectUpdateNotification(
222+
context: vscode.ExtensionContext
223+
): Promise<void> {
224+
// Get Git API from workspace state
225+
const gitAPI = context.workspaceState.get("gitAPI") as API;
226+
227+
// Get repository
228+
const repository = gitAPI?.repositories[0];
229+
230+
// TODO: Get worktree changes and suggest to add update to Commit Project
231+
const worktreeChanges = repository?.state.workingTreeChanges;
232+
233+
if (!worktreeChanges?.length) {
234+
return;
235+
}
236+
237+
// Get last time the notification was shown
238+
const lastNotificationShown = context.workspaceState.get(
239+
"commitLastNotificationShown"
240+
) as number;
241+
242+
// Check if the notification was shown in the last configured minutes
243+
if (lastNotificationShown) {
244+
const currentTime = new Date().getTime();
245+
const commitNotificationInterval =
246+
(context.workspaceState.get("commitNotificationInterval") as number) *
247+
1000;
248+
if (currentTime - lastNotificationShown < commitNotificationInterval) {
249+
return;
250+
}
251+
}
252+
253+
if (worktreeChanges?.length >= 1) {
254+
// Check if the notification to add Project update should be shown
255+
const commitAPI = context.workspaceState.get("commitAPI") as CommitAPI;
256+
if (await commitAPI.showAddProjectUpdateNotification) {
257+
// Show notification with yes and no buttons
258+
vscode.window
259+
.showInformationMessage(
260+
"Would you like to add an update to your Commit Project?",
261+
"Yes",
262+
"No"
263+
)
264+
.then(async (selection) => {
265+
if (selection === "Yes") {
266+
// Initiate the add Project update command
267+
vscode.commands.executeCommand(
268+
"commit-extension.shareProjectUpdate"
269+
);
270+
271+
// Update the last time the notification was shown in the workspace state
272+
context.workspaceState.update(
273+
"commitLastNotificationShown",
274+
new Date().getTime()
275+
);
276+
}
277+
});
278+
}
279+
}
280+
}
210281
}

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export const COMMIT_AUTH0_DOMAIN = process.env.COMMIT_AUTH0_DOMAIN;
66
export const COMMIT_CLIENT_ID = process.env.COMMIT_CLIENT_ID;
77
export const COMMIT_GITHUB_APP_CLIENT_ID =
88
process.env.COMMIT_GITHUB_APP_CLIENT_ID;
9+
export const COMMIT_PROJECT_UDPATE_NOTIFICATION_INTERVAL = 1; // 1 minute

src/extension.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from "vscode";
2-
import { API, GitExtension } from "./@types/git";
2+
import { API, GitExtension, PublishEvent } from "./@types/git";
33
import {
44
Auth0AuthenticationProvider,
55
AUTH_TYPE as AUTH0_AUTH_TYPE,
@@ -42,6 +42,28 @@ export async function activate(this: any, context: vscode.ExtensionContext) {
4242
}
4343
})
4444
);
45+
46+
// Get Git API from workspace state
47+
const gitAPI = context.workspaceState.get("gitAPI") as API;
48+
49+
// Get repository
50+
const repository = gitAPI?.repositories[0];
51+
52+
// Subscribe to on Git status change
53+
context.subscriptions.push(
54+
repository?.state.onDidChange(async () => {
55+
const commitAPI = context.workspaceState.get("commitAPI") as CommitAPI;
56+
await commitAPI.showAddProjectUpdateNotification(context);
57+
})
58+
);
59+
60+
// Subscribe to on Git publish event
61+
context.subscriptions.push(
62+
gitAPI?.onDidPublish((e: PublishEvent) => {
63+
// Get repository
64+
const repository = e.repository;
65+
})
66+
);
4567
}
4668

4769
const handleAuth0SessionChange = async (context: vscode.ExtensionContext) => {
@@ -104,6 +126,9 @@ const getCommitAPI = async (
104126
// Set commit session to commitAPI
105127
commitAPI.setUserCommitSession(commitSession);
106128

129+
// Setup config
130+
await commitAPI.setupConfig(context);
131+
107132
// Add the commitAPI to the workspace state
108133
context.workspaceState.update("commitAPI", commitAPI);
109134

0 commit comments

Comments
 (0)