|
1 | 1 | import { ApolloClient, gql, NormalizedCacheObject } from "@apollo/client/core"; |
2 | 2 |
|
3 | 3 | import * as vscode from "vscode"; |
4 | | -import { API } from "./@types/git"; |
| 4 | +import { API, Commit } from "./@types/git"; |
| 5 | +import { Project, UserInfo } from "./@types/types"; |
5 | 6 | import { COMMIT_PROJECT_UDPATE_NOTIFICATION_INTERVAL } from "./common/constants"; |
| 7 | +import { getGitCommits } from "./utils"; |
6 | 8 |
|
7 | 9 | export enum SubscriptionType { |
8 | 10 | // eslint-disable-next-line @typescript-eslint/naming-convention |
@@ -220,6 +222,92 @@ export class CommitAPI { |
220 | 222 |
|
221 | 223 | public async showAddProjectUpdateNotification( |
222 | 224 | context: vscode.ExtensionContext |
| 225 | + ) { |
| 226 | + // If it was shown in the last configured minutes, don't show the notification |
| 227 | + const lastNotificationShownTime = context.workspaceState.get( |
| 228 | + "lastNotificationShownTime" |
| 229 | + ) as number; |
| 230 | + |
| 231 | + if ( |
| 232 | + lastNotificationShownTime && |
| 233 | + Date.now() - lastNotificationShownTime < |
| 234 | + COMMIT_PROJECT_UDPATE_NOTIFICATION_INTERVAL * 1000 * 20 |
| 235 | + ) { |
| 236 | + console.log("Notification already shown"); |
| 237 | + return; |
| 238 | + } |
| 239 | + |
| 240 | + // Get commit history from workspace state |
| 241 | + const userInfo = context.workspaceState.get("commitUserInfo") as UserInfo; |
| 242 | + |
| 243 | + if (userInfo === undefined) { |
| 244 | + return; |
| 245 | + } |
| 246 | + |
| 247 | + // Get the recent commit history for the project |
| 248 | + const gitCommitHistory = (await getGitCommits(context)) as Commit[]; |
| 249 | + |
| 250 | + if (gitCommitHistory.length === 0) { |
| 251 | + return; |
| 252 | + } |
| 253 | + |
| 254 | + if (userInfo.commits.length === 0) { |
| 255 | + context.workspaceState.update("commitUserInfo", { |
| 256 | + ...userInfo, |
| 257 | + commits: gitCommitHistory, |
| 258 | + }); |
| 259 | + return; |
| 260 | + } |
| 261 | + |
| 262 | + // Get the first commits from the user and git history, to compare the dates and hashes |
| 263 | + const userLastCommit = userInfo.commits[0]; |
| 264 | + const gitHistoryLastCommit = gitCommitHistory[0]; |
| 265 | + |
| 266 | + if ( |
| 267 | + userLastCommit.hash === gitHistoryLastCommit.hash || |
| 268 | + gitHistoryLastCommit.commitDate === undefined |
| 269 | + ) { |
| 270 | + return; |
| 271 | + } |
| 272 | + |
| 273 | + // If user last commit date is undefined, update the workspace state commit history for user and return |
| 274 | + if (userLastCommit.commitDate === undefined) { |
| 275 | + context.workspaceState.update("userInfo", { |
| 276 | + ...userInfo, |
| 277 | + commits: gitCommitHistory, |
| 278 | + }); |
| 279 | + return; |
| 280 | + } |
| 281 | + |
| 282 | + // Check if gitHistoryLastCommit.commitDate is greater than userLastCommit.commitDate |
| 283 | + if (gitHistoryLastCommit.commitDate > userLastCommit.commitDate) { |
| 284 | + // Show notification |
| 285 | + const promptResult = await vscode.window.showInformationMessage( |
| 286 | + "Looks like you've been hard at work, how about sharing a project update?", |
| 287 | + "Yes", |
| 288 | + "No" |
| 289 | + ); |
| 290 | + |
| 291 | + if (promptResult === "No") { |
| 292 | + return; |
| 293 | + } |
| 294 | + |
| 295 | + // Update the workspace state commit history for user |
| 296 | + context.workspaceState.update("commitUserInfo", { |
| 297 | + ...userInfo, |
| 298 | + commits: gitCommitHistory, |
| 299 | + }); |
| 300 | + |
| 301 | + // Update the last notification shown time |
| 302 | + context.workspaceState.update("lastNotificationShownTime", Date.now()); |
| 303 | + |
| 304 | + // Open the Commit Project view |
| 305 | + vscode.commands.executeCommand("commit-extension.shareProjectUpdate"); |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + public async showAddProjectUpdateNotificationOld( |
| 310 | + context: vscode.ExtensionContext |
223 | 311 | ): Promise<void> { |
224 | 312 | // Get Git API from workspace state |
225 | 313 | const gitAPI = context.workspaceState.get("gitAPI") as API; |
@@ -255,7 +343,7 @@ export class CommitAPI { |
255 | 343 | if (worktreeChanges?.length >= 1) { |
256 | 344 | // Check if the notification to add Project update should be shown |
257 | 345 | const commitAPI = context.workspaceState.get("commitAPI") as CommitAPI; |
258 | | - if (await commitAPI.showAddProjectUpdateNotification) { |
| 346 | + if (await commitAPI.showAddProjectUpdateNotificationOld) { |
259 | 347 | // Show notification with yes and no buttons |
260 | 348 | vscode.window |
261 | 349 | .showInformationMessage( |
|
0 commit comments