Skip to content

Commit 166b76c

Browse files
authored
Merge pull request #38 from MetabobProject/feat-expiration-for-state
Feat expiration for state
2 parents 60c5555 + 4e40548 commit 166b76c

File tree

7 files changed

+63
-4
lines changed

7 files changed

+63
-4
lines changed

ext-src/extension.ts

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
activateAnalyzeCommand,
1111
} from './commands';
1212
import {
13+
handleAnalyzeExpiration,
1314
createOrUpdateUserSession,
1415
initState,
1516
AnalyzeDocumentOnSave,
@@ -24,6 +25,8 @@ import {
2425
import { Analyze } from './state';
2526
import { Problem } from './types';
2627

28+
let expirationTimer: any = undefined;
29+
2730
export function activate(context: vscode.ExtensionContext): void {
2831
let previousEditor: vscode.TextEditor | undefined = undefined;
2932
bootstrapExtensionEventEmitter();
@@ -37,6 +40,15 @@ export function activate(context: vscode.ExtensionContext): void {
3740
const analyzeDocumentOnSaveConfig = AnalyzeDocumentOnSaveConfig();
3841

3942
try {
43+
// handle Analyze State Expiration
44+
handleAnalyzeExpiration(context);
45+
46+
const one_minute = 60_000;
47+
const thirty_minutes = one_minute * 30;
48+
expirationTimer = setInterval(() => {
49+
handleAnalyzeExpiration(context);
50+
}, thirty_minutes)
51+
4052
// Create User Session, If already created get the refresh token
4153
// otherwise, ping server every 60 second to not destroy the token
4254
// if the user has not done any activity
@@ -460,4 +472,7 @@ export function activate(context: vscode.ExtensionContext): void {
460472
export function deactivate(): void {
461473
decorationType.dispose();
462474
disposeExtensionEventEmitter();
475+
if (expirationTimer) {
476+
clearInterval(expirationTimer);
477+
}
463478
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import vscode from "vscode";
2+
import { Analyze, AnalyzeState } from "../state";
3+
4+
export const handleAnalyzeExpiration = (context: vscode.ExtensionContext) => {
5+
const today = new Date();
6+
let results: AnalyzeState = {};
7+
const setAnalyzeState = new Analyze(context)
8+
const analyzeStateValue = new Analyze(context).get()?.value;
9+
10+
if (!analyzeStateValue) return undefined;
11+
12+
// If the expiration field on analyzeState does not exist; discard it.
13+
Object.keys(analyzeStateValue).forEach(key => {
14+
const problem = analyzeStateValue[key];
15+
if (problem.expiration !== undefined) {
16+
results[key] = { ...problem }
17+
}
18+
});
19+
20+
let nonExpiredResults: AnalyzeState = {};
21+
22+
// If the problem is expired, discard it as well.
23+
Object.keys(results).forEach(key => {
24+
const problem = analyzeStateValue[key];
25+
if (problem.expiration) {
26+
// Ensure both dates are in the same timezone (e.g., UTC)
27+
const todayUTC = new Date(today.toISOString());
28+
const problemDate = new Date(problem.expiration)
29+
if (problemDate >= todayUTC) {
30+
nonExpiredResults[key] = { ...problem }
31+
}
32+
}
33+
});
34+
35+
setAnalyzeState.set(nonExpiredResults);
36+
return
37+
}

ext-src/helpers/HandleDocumentAnalyze.ts

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export const handleDocumentAnalyze = async (
3131
jobId?: string,
3232
suppressRateLimitErrors = false,
3333
) => {
34+
const tomorrow = new Date();
35+
tomorrow.setDate(tomorrow.getDate() + 1);
36+
3437
const currentWorkSpaceFolder = Util.getRootFolderName();
3538
const editor = vscode.window.activeTextEditor;
3639
if (!editor || editor.document.fileName !== metaDataDocument.filePath) {
@@ -168,6 +171,7 @@ export const handleDocumentAnalyze = async (
168171
isEndorsed: problem.endorsed,
169172
isViewed: false,
170173
fullFilePath: currentWorkSpaceFolder,
174+
expiration: tomorrow.toISOString(),
171175
};
172176
results[key] = { ...analyzeMetaData };
173177
});

ext-src/helpers/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './CreateOrUpdateUserSession'
33
export * from './GenerateDecorations'
44
export * from './HandleDocumentAnalyze'
55
export * from './InitState'
6+
export * from "./HandleAnalyzeExpiration"

ext-src/state/Analyze.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type AnalyseMetaData = {
1515
isEndorsed?: boolean;
1616
isViewed?: boolean;
1717
fullFilePath?: string;
18+
expiration?: string;
1819
};
1920

2021
export type AnalyzeState = {

src/hooks/useUser.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { useContext } from 'react'
2-
import { AccountSettingContext } from '../context/UserContext'
1+
import { useContext } from 'react';
2+
import { AccountSettingContext } from '../context/UserContext';
33

4-
export const useUser = () => useContext(AccountSettingContext)
4+
// @ts-ignore
5+
export const useUser = () => useContext(AccountSettingContext);

src/theme.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const shadows: ThemeOptions['shadows'] = [
2929
'0px 11px 15px -7px rgba(19, 17, 32, 0.2), 0px 24px 38px 3px rgba(19, 17, 32, 0.14), 0px 9px 46px 8px rgba(19, 17, 32, 0.12)',
3030
];
3131

32-
export const registerTheme = () => {
32+
export const registerTheme = (): ThemeOptions => {
3333
const vscodeTheme = getComputedStyle(document.documentElement);
3434
const mainColor = vscodeTheme.getPropertyValue('--vscode-input-foreground');
3535
const backgroundDefault = vscodeTheme.getPropertyValue('--vscode-input-background');

0 commit comments

Comments
 (0)