claude-code-cost 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,6 +18,10 @@ npx claude-code-cost --open
18
18
 
19
19
  That's it — no hooks, no config. The dashboard reads the JSONL session logs Claude Code already writes to `~/.claude/projects/` and aggregates them server-side. Completely read-only; nothing leaves your machine.
20
20
 
21
+ ### Install as a desktop app
22
+
23
+ The dashboard is a PWA. With the server running, open it in Chrome or Edge and click **Install** in the top bar (or the install icon in the address bar). Safari on macOS: **File → Add to Dock**. The app opens in its own window with a Start menu / Dock entry and right-click shortcuts to Overview and Insights. The server still has to be running; the installed app is a window onto `http://localhost:3543`.
24
+
21
25
  ## Features
22
26
 
23
27
  - **Top-down drill-down** — total → project → session → message; every number is one click from its breakdown
@@ -44,7 +48,7 @@ That's it — no hooks, no config. The dashboard reads the JSONL session logs Cl
44
48
  --open Open browser on start
45
49
  ```
46
50
 
47
- The config dir can also be set via the `CLAUDE_CONFIG_DIR` environment variable.
51
+ The config dir can also be set via the `CLAUDE_CONFIG_DIR` environment variable. With a custom dir only `<dir>/projects` is scanned; the default profile also picks up `~/.config/claude/projects`.
48
52
 
49
53
  ## License
50
54
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-code-cost",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "Cost dashboard for Claude Code usage",
5
5
  "main": "server.js",
6
6
  "bin": {
package/public/app.js CHANGED
@@ -284,7 +284,7 @@ function migrateStoredPreset(value) {
284
284
  }
285
285
 
286
286
  // Reads the JSON tagged union and the legacy bare "3" / "today" scalar that predates it: the key is
287
- // a preference, so clearLocalCache() never evicts it and old values outlive any cache clear.
287
+ // a preference, so purgeLegacyLocalCache() never evicts it and old values outlive any cache clear.
288
288
  // Anything unrecognized returns null, which lands on the default preset.
289
289
  function parseStoredRange(raw) {
290
290
  if (!raw) return null;
@@ -444,61 +444,41 @@ function updateUrl() {
444
444
  // #region FETCH
445
445
 
446
446
  const BROWSER_CACHE_TTL = 5 * 60 * 1000; // 5 min
447
- const CACHE_VERSION = 6;
447
+ const MAX_CACHE_ENTRIES = 20;
448
448
  let forceRefresh = false;
449
449
  // Age of the data on screen, for the auto-refresh staleness check. A cache hit carries its own
450
- // timestamp forward, so a reload with a warm cache doesn't look freshly fetched.
450
+ // timestamp forward, so a re-render from cache doesn't look freshly fetched.
451
451
  let dataFetchedAt = 0;
452
452
 
453
+ // In memory on purpose: the server can be restarted against another CLAUDE_CONFIG_DIR (the hub does
454
+ // this on a config-dir switch and reloads the page), and a cache that outlives the page would keep
455
+ // showing the previous dir's data. The server's own 30 s cache absorbs the reload cost.
456
+ const dataCache = new Map();
457
+
453
458
  function getCached(key) {
454
459
  if (forceRefresh) return null;
455
- try {
456
- const raw = localStorage.getItem(`cc-cost:${key}`);
457
- if (!raw) return null;
458
- const { data, ts, v } = JSON.parse(raw);
459
- if (v !== CACHE_VERSION || Date.now() - ts > BROWSER_CACHE_TTL) return null;
460
- dataFetchedAt = Math.max(dataFetchedAt, ts);
461
- return data;
462
- } catch {
460
+ const hit = dataCache.get(key);
461
+ if (!hit) return null;
462
+ if (Date.now() - hit.ts > BROWSER_CACHE_TTL) {
463
+ dataCache.delete(key);
463
464
  return null;
464
465
  }
466
+ dataFetchedAt = Math.max(dataFetchedAt, hit.ts);
467
+ return hit.data;
465
468
  }
466
469
 
467
- function setLocalCache(key, data) {
468
- try {
469
- pruneLocalCache();
470
- localStorage.setItem(`cc-cost:${key}`, JSON.stringify({ data, ts: Date.now(), v: CACHE_VERSION }));
471
- } catch {
472
- /* storage full — ignore */
473
- }
474
- }
475
-
476
- function pruneLocalCache() {
477
- const MAX_ENTRIES = 20;
478
- const entries = [];
479
- for (let i = 0; i < localStorage.length; i++) {
480
- const k = localStorage.key(i);
481
- if (!k.startsWith('cc-cost:')) continue;
482
- // Prefs are not cache entries — they have no ts, so they'd sort oldest-first and be
483
- // evicted ahead of real data while also eating the entry budget.
484
- if (isPrefKey(k)) continue;
485
- try {
486
- const { ts } = JSON.parse(localStorage.getItem(k));
487
- entries.push({ k, ts });
488
- } catch {
489
- entries.push({ k, ts: 0 });
490
- }
491
- }
492
- if (entries.length <= MAX_ENTRIES) return;
493
- entries.sort((a, b) => a.ts - b.ts);
494
- const toRemove = entries.length - MAX_ENTRIES;
495
- for (let i = 0; i < toRemove; i++) localStorage.removeItem(entries[i].k);
470
+ function cacheData(key, data) {
471
+ dataCache.delete(key);
472
+ dataCache.set(key, { data, ts: Date.now() });
473
+ if (dataCache.size > MAX_CACHE_ENTRIES) dataCache.delete(dataCache.keys().next().value);
496
474
  }
497
475
 
498
476
  function isPrefKey(k) {
499
- return k.startsWith('cc-cost:sort:') || k === 'cc-cost:range' || k === 'cc-cost:scope';
477
+ return k.startsWith('cc-cost:sort:') || k === 'cc-cost:range' || k === 'cc-cost:scope' || k === 'cc-cost:view';
500
478
  }
501
- function clearLocalCache() {
479
+
480
+ // Earlier versions persisted API payloads under cc-cost:<url>; drop what they left behind.
481
+ function purgeLegacyLocalCache() {
502
482
  const keys = [];
503
483
  for (let i = 0; i < localStorage.length; i++) {
504
484
  const k = localStorage.key(i);
@@ -516,7 +496,7 @@ async function fetchJSON(url, skipCache) {
516
496
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
517
497
  const data = await res.json();
518
498
  dataFetchedAt = Date.now();
519
- if (!skipCache) setLocalCache(url, data);
499
+ if (!skipCache) cacheData(url, data);
520
500
  return data;
521
501
  }
522
502
 
@@ -2569,7 +2549,7 @@ async function refreshData() {
2569
2549
  const minWait = new Promise((r) => setTimeout(r, 250));
2570
2550
  try {
2571
2551
  await fetch('/api/refresh', { method: 'POST' });
2572
- clearLocalCache();
2552
+ dataCache.clear();
2573
2553
  forceRefresh = true;
2574
2554
  lastRenderHash = {};
2575
2555
  await loadAndRender(currentView);
@@ -3033,7 +3013,7 @@ function isAppActive() {
3033
3013
  async function refreshIfStale() {
3034
3014
  if (refreshing || !isAppActive()) return;
3035
3015
  if (Date.now() - dataFetchedAt < BROWSER_CACHE_TTL) return;
3036
- // No clearLocalCache() unlike refreshData: forceRefresh already bypasses the cache read, and the
3016
+ // No dataCache.clear() unlike refreshData: forceRefresh already bypasses the cache read, and the
3037
3017
  // fresh payloads overwrite their own keys. Views the user isn't on keep their cache.
3038
3018
  refreshing = true;
3039
3019
  forceRefresh = true;
@@ -3074,8 +3054,9 @@ async function refreshIfStale() {
3074
3054
  if (e.ctrlKey && e.altKey && (e.key === 'ArrowLeft' || e.key === 'ArrowRight')) {
3075
3055
  fwd(e.key);
3076
3056
  }
3077
- // Its own branch: the Alt+digit case below requires !ctrlKey.
3078
- if (e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey && e.key.toLowerCase() === 'p') {
3057
+ // Own branch: the Alt+digit case below requires !ctrlKey. The hub owns the Ctrl+Alt+letter
3058
+ // keymap and ignores unbound letters.
3059
+ if (e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey && /^[a-z]$/i.test(e.key)) {
3079
3060
  fwd(e.key);
3080
3061
  }
3081
3062
  if (e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey && /^[1-9]$/.test(e.key)) {
@@ -3167,6 +3148,7 @@ document.addEventListener('click', async (e) => {
3167
3148
  });
3168
3149
 
3169
3150
  document.addEventListener('DOMContentLoaded', async () => {
3151
+ purgeLegacyLocalCache();
3170
3152
  const state = getUrlState();
3171
3153
  dateRange = loadDateRange();
3172
3154
  buildRangePresets();
@@ -3225,4 +3207,27 @@ if ('serviceWorker' in navigator) {
3225
3207
  navigator.serviceWorker.register('/sw.js').catch(() => {});
3226
3208
  }
3227
3209
 
3210
+ const installBtn = document.getElementById('installBtn');
3211
+ let deferredInstallPrompt = null;
3212
+
3213
+ function setInstallPrompt(e) {
3214
+ deferredInstallPrompt = e;
3215
+ installBtn.hidden = !e;
3216
+ }
3217
+
3218
+ window.addEventListener('beforeinstallprompt', (e) => {
3219
+ e.preventDefault();
3220
+ setInstallPrompt(e);
3221
+ });
3222
+
3223
+ window.addEventListener('appinstalled', () => setInstallPrompt(null));
3224
+
3225
+ // biome-ignore lint/correctness/noUnusedVariables: called from topbar markup
3226
+ async function promptInstall() {
3227
+ const e = deferredInstallPrompt;
3228
+ if (!e) return;
3229
+ setInstallPrompt(null);
3230
+ await e.prompt();
3231
+ }
3232
+
3228
3233
  // #endregion
Binary file
Binary file
package/public/index.html CHANGED
@@ -85,6 +85,10 @@
85
85
  <button class="topbar-btn" id="themeBtn" title="Toggle theme" onclick="toggleTheme()">
86
86
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" id="themeIcon"><circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/></svg>
87
87
  </button>
88
+ <button class="topbar-btn" id="installBtn" title="Install as desktop app" onclick="promptInstall()" hidden>
89
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>
90
+ Install
91
+ </button>
88
92
  <button class="topbar-btn" id="infoBtn" title="Help & About (?)" onclick="toggleHelpModal()">
89
93
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/><circle cx="12" cy="17" r="0.5" fill="currentColor"/></svg>
90
94
  <kbd>?</kbd>
@@ -1,14 +1,21 @@
1
1
  {
2
+ "id": "/",
2
3
  "name": "Claude Code Cost",
3
4
  "short_name": "CC Cost",
4
5
  "description": "Cost dashboard for Claude Code usage",
5
6
  "start_url": "/",
7
+ "scope": "/",
6
8
  "display": "standalone",
7
9
  "theme_color": "#101114",
8
10
  "background_color": "#101114",
9
11
  "icons": [
10
12
  { "src": "/icons/icon-192.png", "sizes": "192x192", "type": "image/png" },
11
13
  { "src": "/icons/icon-512.png", "sizes": "512x512", "type": "image/png" },
14
+ { "src": "/icons/icon-512-maskable.png", "sizes": "512x512", "type": "image/png", "purpose": "maskable" },
12
15
  { "src": "/icons/icon-svg.svg", "sizes": "any", "type": "image/svg+xml" }
16
+ ],
17
+ "shortcuts": [
18
+ { "name": "Overview", "url": "/?view=overview" },
19
+ { "name": "Insights", "url": "/?view=insights" }
13
20
  ]
14
21
  }
package/public/style.css CHANGED
@@ -42,6 +42,11 @@
42
42
  padding: 0;
43
43
  }
44
44
 
45
+ [hidden] {
46
+ /* biome-ignore lint/complexity/noImportantStyles: must beat every component's own display */
47
+ display: none !important;
48
+ }
49
+
45
50
  body {
46
51
  font-family: var(--mono);
47
52
  font-size: 13px;
@@ -137,10 +142,6 @@ body {
137
142
  max-width: 220px;
138
143
  }
139
144
 
140
- .scope-chip[hidden] {
141
- display: none;
142
- }
143
-
144
145
  .scope-chip > span {
145
146
  overflow: hidden;
146
147
  text-overflow: ellipsis;
package/server.js CHANGED
@@ -21,17 +21,21 @@ function getArg(name) {
21
21
 
22
22
  const PORT = getArg('port') || process.env.PORT || 3543;
23
23
  const claudeDirArg = getArg('dir') || process.env.CLAUDE_CONFIG_DIR || process.env.CLAUDE_DIR;
24
- const CLAUDE_DIR = claudeDirArg
25
- ? claudeDirArg.replace(/^~/, os.homedir())
26
- : path.join(os.homedir(), '.claude');
24
+ const DEFAULT_CLAUDE_DIR = path.join(os.homedir(), '.claude');
25
+ const CLAUDE_DIR = claudeDirArg ? claudeDirArg.replace(/^~/, os.homedir()) : DEFAULT_CLAUDE_DIR;
27
26
 
28
27
  const PROJECTS_DIR = path.join(CLAUDE_DIR, 'projects');
29
- const ALT_PROJECTS_DIR = path.join(os.homedir(), '.config', 'claude', 'projects');
28
+ // XDG-style location some builds used for the default profile only. A custom
29
+ // CLAUDE_CONFIG_DIR holds everything itself, so scanning it there would leak
30
+ // the home profile's sessions into the custom one.
31
+ const ALT_PROJECTS_DIR = path.resolve(CLAUDE_DIR) === path.resolve(DEFAULT_CLAUDE_DIR)
32
+ ? path.join(os.homedir(), '.config', 'claude', 'projects')
33
+ : null;
30
34
 
31
35
  function getProjectsDirs() {
32
36
  const dirs = [];
33
37
  if (fs.existsSync(PROJECTS_DIR)) dirs.push(PROJECTS_DIR);
34
- if (fs.existsSync(ALT_PROJECTS_DIR)) dirs.push(ALT_PROJECTS_DIR);
38
+ if (ALT_PROJECTS_DIR && fs.existsSync(ALT_PROJECTS_DIR)) dirs.push(ALT_PROJECTS_DIR);
35
39
  return dirs;
36
40
  }
37
41