|
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"; |
| 5 | +import { COMMIT_PROJECT_UDPATE_NOTIFICATION_INTERVAL } from "./common/constants"; |
4 | 6 |
|
5 | 7 | export enum SubscriptionType { |
6 | 8 | // eslint-disable-next-line @typescript-eslint/naming-convention |
@@ -207,4 +209,73 @@ export class CommitAPI { |
207 | 209 | throw new Error("Error updating project"); |
208 | 210 | } |
209 | 211 | } |
| 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 | + } |
210 | 281 | } |
0 commit comments