Skip to content

Commit a04a569

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 919d2ac commit a04a569

10 files changed

Lines changed: 651 additions & 560 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1919
- **Default profile storage path regression** — the default profile now uses the legacy low-version database path (`app_data/timelens.db`) instead of a separate `app_data/profiles/default/timelens.db` folder. On startup, any data already written to the v2.0.0 `profiles/default` folder is automatically merged into the legacy database; raw usage rows are inserted without their synthetic `id` to avoid accidental data loss, and `daily_app_usage` is rebuilt from the merged raw rows so today’s totals stay correct. Other conflicting rows are skipped (`INSERT OR IGNORE`). Encrypted default-profile databases are relocated to the legacy path before decryption so they continue to work.
2020
- **Dashboard period total label** — the overview card now shows “Week Total” / “Month Total” instead of “Today’s Total” when the period selector is set to week or month; the numeric value already matched the selected range.
2121
- **VS Code extension API network errors** — dashboard no longer logs `Failed to fetch` errors when the optional local VS Code extension API is unreachable; it falls back to empty stats and emits the unavailable event once.
22+
- **Dependency security updates** — updated `sharp` to `^0.35.3` and added `overrides` for `brace-expansion`, `minimatch`, and `js-yaml` in the root `package.json` to resolve high-severity advisories. Updated `postcss` to `^8.5.23`. In `vscode-extension`, updated `@vscode/vsce` to `^3.9.2` and added overrides for `brace-expansion`/`minimatch`, clearing all high-severity Dependabot alerts in that package. `react-router-dom` remains at `^6.30.4` because the only patched v7 versions currently available introduce a separate high-severity advisory; this will be revisited once a clean patched release exists.
2223

2324
---
2425

browser-extension/api.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,14 @@ async function getConnectionSettings() {
5555
export async function discoverApiBaseUrl() {
5656
const now = Date.now();
5757
if (discoveredApiBaseCache && discoveredApiBaseCache.expiresAt > now) {
58+
log("Using cached API base URL:", discoveredApiBaseCache.value, "expires in", discoveredApiBaseCache.expiresAt - now, "ms");
5859
return discoveredApiBaseCache.value;
5960
}
6061

62+
log("Discovering API base URL...");
6163
const { manualPort, cacheMode, cacheSeconds } = await getConnectionSettings();
6264
const cacheMs = cacheMode === "startup" ? Number.MAX_SAFE_INTEGER : Math.max(0, cacheSeconds) * 1000;
65+
log("Cache mode:", cacheMode, "cacheMs:", cacheMs);
6366

6467
const portsToTry = [];
6568
const manualPortAllowed =
@@ -68,6 +71,8 @@ export async function discoverApiBaseUrl() {
6871
now > manualPortDisabledUntil &&
6972
manualPortFailureCount < MANUAL_PORT_FAILURE_THRESHOLD;
7073

74+
log("Manual port:", manualPort, "allowed:", manualPortAllowed, "failureCount:", manualPortFailureCount, "disabledUntil:", manualPortDisabledUntil);
75+
7176
if (manualPortAllowed) {
7277
portsToTry.push(manualPort);
7378
}
@@ -77,6 +82,7 @@ export async function discoverApiBaseUrl() {
7782
portsToTry.push(port);
7883
}
7984
}
85+
log("Will scan", portsToTry.length, "ports, first few:", portsToTry.slice(0, 5));
8086

8187
let manualPortTried = false;
8288
for (const port of portsToTry) {
@@ -94,40 +100,50 @@ export async function discoverApiBaseUrl() {
94100
if (response.ok) {
95101
const data = await response.json();
96102
if (data && typeof data.version === "string") {
103+
log("API found on port", port, "version:", data.version);
97104
if (port === manualPort) {
98105
manualPortFailureCount = 0;
99106
manualPortDisabledUntil = 0;
100107
}
101108
discoveredApiBaseCache = { value: baseUrl, expiresAt: now + cacheMs };
109+
log("Cached API base URL:", baseUrl, "until:", now + cacheMs);
102110
return baseUrl;
103111
}
112+
logWarn("Port", port, "responded but missing version field");
104113
}
105-
} catch {
114+
} catch (error) {
106115
// port not reachable — try next
116+
log("Port", port, "unreachable:", error?.name || error?.message || error);
107117
}
108118
}
109119

110120
if (manualPortTried) {
111121
manualPortFailureCount += 1;
122+
logWarn("Manual port", manualPort, "failed; count:", manualPortFailureCount, "/", MANUAL_PORT_FAILURE_THRESHOLD);
112123
if (manualPortFailureCount >= MANUAL_PORT_FAILURE_THRESHOLD) {
113-
// Temporarily ignore the manual port for 5 minutes so the fallback scan can work.
114124
manualPortDisabledUntil = now + 5 * 60 * 1000;
125+
logWarn("Manual port temporarily disabled until", new Date(manualPortDisabledUntil).toISOString());
115126
}
116127
}
117128

129+
logWarn("No TimeLens API found after scanning", portsToTry.length, "ports");
118130
discoveredApiBaseCache = { value: "", expiresAt: now + FALLBACK_CACHE_MS };
119131
return null;
120132
}
121133

122134
export function getApiBaseUrl() {
123-
return discoveredApiBaseCache?.value || `http://127.0.0.1:${DEFAULT_API_PORT}`;
135+
const url = discoveredApiBaseCache?.value || `http://127.0.0.1:${DEFAULT_API_PORT}`;
136+
log("getApiBaseUrl returning:", url);
137+
return url;
124138
}
125139

126140
export function clearApiBaseUrlCache() {
141+
log("Clearing API base URL cache");
127142
discoveredApiBaseCache = null;
128143
}
129144

130145
export function resetManualPortFailureTracking() {
146+
log("Resetting manual port failure tracking");
131147
manualPortFailureCount = 0;
132148
manualPortDisabledUntil = 0;
133149
}

0 commit comments

Comments
 (0)