Skip to content

Commit 374da38

Browse files
authored
Merge pull request #131 from Friedrich482/features
fix: update after sync
2 parents 0f2de9e + f1c2691 commit 374da38

10 files changed

Lines changed: 53 additions & 53 deletions

apps/vscode-extension/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mooncode",
33
"displayName": "MoonCode",
44
"description": "MoonCode is an extension that tracks your coding time (like WakaTime) and gives you a detailed summary about all your coding statistics. With MoonCode, developers get the full history of their coding activity.",
5-
"version": "0.0.62",
5+
"version": "0.0.63",
66
"icon": "./public/moon.png",
77
"publisher": "Friedrich482",
88
"author": {

apps/vscode-extension/src/types-schemas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { IsoDateStringSchema } from "@repo/common/types-schemas";
55
/**
66
* A file is marked as frozen when it is no longer the active file or if it is the active file but the user is inactive in the file for more than `MAX_IDLE_TIME` seconds
77
* - `frozenTime` is an accumulator of time to track the elapsedTime on a file when the language is frozen
8-
* - `freezeStartTime` is the time when the file is marked as frozen
8+
* - `freezeStartTime` is the moment when the file is marked as frozen
99
* - `startTime` is the moment we start counting the time for that file
1010
* - `lastActivityTime` is the moment when the file is no longer focused, i.e no longer the active file
11-
* - `elapsedTime` is the time elapsed in total and what we send to the server/ save in the global state
11+
* - `elapsedTime` is the time elapsed in total and what we send to the server/save in the global state
1212
* - `isFrozen` is a boolean to freeze/unfreeze the file
1313
*/
1414
export type FileData = {

apps/vscode-extension/src/utils/commands/init-extension-commands.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { login } from "../auth/login";
77
import { logout } from "../auth/logout";
88
import { openDashboard } from "../dashboard/open-dashboard";
99
import { getGlobalStateData } from "../global-state/get-global-state-data";
10-
import { logDir, logInfo } from "../logger/logger";
10+
import { logInfo } from "../logger/logger";
1111
import { calculateTime } from "../time/calculate-time";
1212

1313
export const initExtensionCommands = (
@@ -69,10 +69,9 @@ export const initExtensionCommands = (
6969
"MoonCode.showRawFilesDataCommand",
7070
() => {
7171
const filesData = getTime();
72-
const formattedData = Object.entries(filesData)
73-
.map(([key, fileData]) => `${key}:${JSON.stringify(fileData, null, 2)}`)
74-
.join("\n");
75-
logInfo(`Raw files Data computed:\n${formattedData}`);
72+
logInfo(
73+
`Raw files Data computed:\n${JSON.stringify(filesData, null, 2)}`,
74+
);
7675
},
7776
);
7877

@@ -93,7 +92,7 @@ export const initExtensionCommands = (
9392
"MoonCode.showGlobalStateData",
9493
async () => {
9594
const data = await getGlobalStateData();
96-
logDir(data);
95+
logInfo(`Global State data: ${JSON.stringify(data, null, 2)}`);
9796
},
9897
);
9998

apps/vscode-extension/src/utils/dashboard/serve-dashboard/serve-dashboard-dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const serveDashboardDev = async () => {
3535
const data = JSON.parse(message.toString());
3636
const validated = WsDataSchema.safeParse(data);
3737
if (!validated.success) {
38-
console.error("Invalid message shape");
38+
logError("Invalid message shape");
3939
return;
4040
}
4141

apps/vscode-extension/src/utils/dashboard/serve-dashboard/serve-dashboard-prod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ export const serveDashboardProd = async () => {
5454
const data = JSON.parse(message.toString());
5555
const validated = WsDataSchema.safeParse(data);
5656
if (!validated.success) {
57-
console.error("Invalid message shape");
57+
logError("Invalid message shape");
5858
return;
5959
}
6060

6161
logInfo("Received from dashboard:");
62-
logDir(data);
62+
logDir(validated.data);
6363

6464
const { type } = validated.data;
6565

apps/vscode-extension/src/utils/files/update-files-data-after-sync.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export const updateFilesDataAfterSync = (
1212

1313
if (filesData[filePath]) {
1414
filesData[filePath].elapsedTime = file.timeSpent;
15+
16+
if (!filesData[filePath].isFrozen) {
17+
filesData[filePath].startTime = now - file.timeSpent * 1000;
18+
return;
19+
}
20+
21+
filesData[filePath].frozenTime = file.timeSpent;
1522
return;
1623
}
1724

apps/vscode-extension/src/utils/files/update-files-data-elapsed-time.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ export const updateFilesDataElapsedTime = () => {
66

77
Object.keys(filesData).forEach((filePath) => {
88
const fileData = filesData[filePath];
9+
910
// Only use frozenTime if it is not null (0 is valid)
1011
fileData.elapsedTime =
12+
// we check if the file is in a frozen state or not
1113
fileData.isFrozen && fileData.frozenTime !== null
1214
? fileData.frozenTime
1315
: Math.floor((now - fileData.startTime) / 1000);

apps/vscode-extension/src/utils/files/update-files-data-frozen-states.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ export const updateFilesDataFrozenStates = () => {
1313

1414
Object.keys(filesData).forEach((filePath) => {
1515
const fileData = filesData[filePath];
16-
/**
17-
* immediately freeze non active files
18-
*/
16+
// immediately freeze non active files
1917
if (
2018
!latestFile ||
2119
!latestFile.absolutePath ||
@@ -31,28 +29,22 @@ export const updateFilesDataFrozenStates = () => {
3129

3230
const latestFileObj = filesData[latestFile.absolutePath];
3331

34-
/**
35-
* We track the time elapsed since when the latest file has been modified
36-
* in a variable called `idleDuration`
37-
*/
32+
// We track the time elapsed since when the latest file has been modified
33+
// in a variable called `idleDuration`
3834
const idleDuration = Math.floor(
3935
(now - latestFileObj.lastActivityTime) / 1000,
4036
);
4137

42-
/**
43-
* we freeze the time for the active file if the user is idling for more than `MAX_IDLE_TIME`
44-
*/
38+
// we freeze the time for the active file if the user is idling for more than `MAX_IDLE_TIME`
4539
if (idleDuration >= MAX_IDLE_TIME && !latestFileObj.isFrozen) {
4640
latestFileObj.frozenTime = Math.floor(
4741
(now - latestFileObj.startTime) / 1000,
4842
);
4943
latestFileObj.freezeStartTime = now;
5044
latestFileObj.isFrozen = true;
5145
} else if (
52-
/**
53-
* we unfreeze if it is active,
54-
* marked as frozen and if the user hasn't been idled for more than `MAX_IDLE_TIME` seconds
55-
*/
46+
// we unfreeze if it is active, marked as frozen and
47+
// if the user hasn't been idled for more than `MAX_IDLE_TIME` seconds
5648
idleDuration < MAX_IDLE_TIME &&
5749
latestFileObj.isFrozen &&
5850
latestFileObj.freezeStartTime

apps/vscode-extension/src/utils/global-state/get-global-state-data.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import vscode from "vscode";
2-
import { ZodError } from "zod";
32

43
import { SYNC_DATA_KEY } from "@/constants";
54
import { getExtensionContext } from "@/extension";
@@ -17,15 +16,15 @@ export const getGlobalStateData: () => Promise<GlobalStateData> = async () => {
1716
const context = getExtensionContext();
1817
const todaysDateString = getLocaleDate(new Date());
1918

20-
try {
21-
const globalStateData = globalStateInitialDataSchema.parse(
22-
await context.globalState.get(SYNC_DATA_KEY),
23-
);
19+
const parsedGlobalStateData = globalStateInitialDataSchema.safeParse(
20+
await context.globalState.get(SYNC_DATA_KEY),
21+
);
22+
23+
if (!parsedGlobalStateData.success) {
24+
const error = formatZodError(parsedGlobalStateData.error);
2425

25-
return globalStateData;
26-
} catch (error) {
2726
vscode.window.showErrorMessage(
28-
`Invalid data shape: ${error instanceof ZodError ? formatZodError(error) : error}. Defaulting to default data`,
27+
`Invalid data shape: ${error}. Defaulting to default data`,
2928
);
3029

3130
return {
@@ -40,4 +39,8 @@ export const getGlobalStateData: () => Promise<GlobalStateData> = async () => {
4039
},
4140
};
4241
}
42+
43+
const { data: globalStateData } = parsedGlobalStateData;
44+
45+
return globalStateData;
4346
};

apps/vscode-extension/src/utils/periodic-sync-data.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,27 @@ export const periodicSyncData = async (
4343

4444
timeSpentPerLanguage = timeSpentPerLanguageToday;
4545

46-
const timeSpentPerProject = Object.entries(filesDataToUpsert)
47-
.map(([, fileData]) => ({
48-
project: fileData.projectPath,
49-
timeSpent: fileData.elapsedTime,
50-
}))
51-
.reduce(
52-
(acc, curr) => {
53-
if (acc[curr.project]) {
54-
acc[curr.project] += curr.timeSpent;
55-
} else {
56-
acc[curr.project] = curr.timeSpent;
57-
}
58-
return acc;
59-
},
60-
{} as Record<string, number>,
61-
);
46+
const timeSpentPerProjectToday = Object.values(filesDataToUpsert).reduce(
47+
(acc, { projectPath, elapsedTime }) => {
48+
acc[projectPath] = (acc[projectPath] || 0) + elapsedTime;
49+
return acc;
50+
},
51+
{} as Record<string, number>,
52+
);
6253

6354
const todayFilesData = Object.fromEntries(
6455
Object.entries(filesDataToUpsert).map(
65-
([filePath, { elapsedTime, ...rest }]) => [
56+
([
57+
filePath,
58+
{ elapsedTime, languageSlug, projectName, projectPath, fileName },
59+
]) => [
6660
filePath,
6761
{
6862
timeSpent: elapsedTime,
69-
...rest,
63+
languageSlug,
64+
projectName,
65+
projectPath,
66+
fileName,
7067
},
7168
],
7269
),
@@ -114,7 +111,7 @@ export const periodicSyncData = async (
114111
const files = await trpc.extension.upsertFiles.mutate({
115112
filesData: todayFilesData,
116113
targetedDate: todaysDateString,
117-
timeSpentPerProject,
114+
timeSpentPerProject: timeSpentPerProjectToday,
118115
});
119116
updateFilesDataAfterSync(files);
120117

0 commit comments

Comments
 (0)