agent-cleandrive 0.0.0-stage → 0.2.2

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/ui/index.html ADDED
@@ -0,0 +1,489 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>CleanDrive</title>
7
+ <style>
8
+ :root {
9
+ --page: #f9f9f7; --surface: #fcfcfb;
10
+ --ink: #0b0b0b; --ink-2: #52514e; --muted: #898781;
11
+ --grid: #e1e0d9; --baseline: #c3c2b7; --border: rgba(11,11,11,0.10);
12
+ --c-pkgcache: #2a78d6; --c-cache: #1baf7a; --c-nm: #eda100;
13
+ --c-projects: #008300; --c-appdata: #4a3aa7; --c-apps: #e87ba4; --c-models: #e34948;
14
+ --seq-light: #86b6ef; --seq-mid: #2a78d6;
15
+ --good: #0ca30c; --warning: #fab219; --serious: #ec835a; --critical: #d03b3b;
16
+ --good-ink: #006300;
17
+ }
18
+ @media (prefers-color-scheme: dark) {
19
+ :root {
20
+ --page: #0d0d0d; --surface: #1a1a19;
21
+ --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
22
+ --grid: #2c2c2a; --baseline: #383835; --border: rgba(255,255,255,0.10);
23
+ --c-pkgcache: #3987e5; --c-cache: #199e70; --c-nm: #c98500;
24
+ --c-projects: #008300; --c-appdata: #9085e9; --c-apps: #d55181; --c-models: #e66767;
25
+ --seq-light: #1c5cab; --seq-mid: #3987e5;
26
+ --good-ink: #0ca30c;
27
+ }
28
+ }
29
+ * { box-sizing: border-box; margin: 0; }
30
+ body {
31
+ background: var(--page); color: var(--ink);
32
+ font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
33
+ padding: 24px; max-width: 1180px; margin: 0 auto;
34
+ }
35
+ h1 { font-size: 20px; font-weight: 650; }
36
+ h2 { font-size: 13px; font-weight: 600; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 12px; }
37
+ header { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
38
+ header .meta { color: var(--muted); font-size: 12.5px; }
39
+ .spacer { flex: 1; }
40
+ button {
41
+ font: inherit; font-size: 13px; padding: 6px 14px; border-radius: 7px; cursor: pointer;
42
+ border: 1px solid var(--border); background: var(--surface); color: var(--ink);
43
+ }
44
+ button:hover { border-color: var(--baseline); }
45
+ button.primary { background: var(--seq-mid); border-color: transparent; color: #fff; }
46
+ button.danger { background: var(--critical); border-color: transparent; color: #fff; }
47
+ button:disabled { opacity: .45; cursor: default; }
48
+ .card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; }
49
+ .tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
50
+ .tile .label { font-size: 12px; color: var(--ink-2); margin-bottom: 4px; }
51
+ .tile .value { font-size: 28px; font-weight: 650; letter-spacing: -.01em; }
52
+ .tile .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
53
+ .chip { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--ink-2); }
54
+ .chip .dot { width: 8px; height: 8px; border-radius: 50%; }
55
+ /* capacity bar */
56
+ .capbar { display: flex; height: 26px; border-radius: 6px; overflow: hidden; background: transparent; border: 1px solid var(--grid); }
57
+ .capbar .seg { height: 100%; }
58
+ .capbar .seg + .seg { margin-left: 2px; }
59
+ .caplegend { display: flex; gap: 18px; margin-top: 10px; font-size: 12.5px; color: var(--ink-2); flex-wrap: wrap; }
60
+ .caplegend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; border: 1px solid var(--border); }
61
+ /* category bars */
62
+ .catrow { display: grid; grid-template-columns: 170px 1fr 90px; align-items: center; gap: 12px; padding: 5px 0; }
63
+ .catrow .name { font-size: 13px; color: var(--ink); display: flex; align-items: center; gap: 8px; }
64
+ .catrow .track { height: 12px; position: relative; }
65
+ .catrow .bar { height: 12px; border-radius: 0 4px 4px 0; min-width: 2px; }
66
+ .catrow .val { font-size: 13px; color: var(--ink-2); text-align: right; font-variant-numeric: tabular-nums; }
67
+ /* filters + table */
68
+ .filters { display: flex; gap: 8px; margin: 4px 0 12px; flex-wrap: wrap; }
69
+ .filters button { padding: 4px 12px; border-radius: 999px; font-size: 12.5px; }
70
+ .filters button.on { background: var(--ink); color: var(--page); border-color: transparent; }
71
+ table { width: 100%; border-collapse: collapse; font-size: 13px; }
72
+ th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--grid); }
73
+ td { padding: 7px 8px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
74
+ td.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
75
+ td .path { font-family: ui-monospace, monospace; font-size: 12px; word-break: break-all; }
76
+ td .note { color: var(--muted); font-size: 11.5px; }
77
+ .risk { font-size: 11.5px; font-weight: 600; white-space: nowrap; }
78
+ .risk.low { color: var(--good-ink); } .risk.medium { color: var(--ink-2); } .risk.review { color: var(--ink-2); }
79
+ select { font: inherit; font-size: 12.5px; background: var(--surface); color: var(--ink); border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; }
80
+ .rowbtns { display: flex; gap: 6px; }
81
+ .rowbtns button { padding: 3px 10px; font-size: 12px; }
82
+ .st { font-size: 11.5px; font-weight: 600; }
83
+ .st.approved { color: var(--seq-mid); } .st.done { color: var(--good-ink); } .st.failed { color: var(--critical); } .st.skipped { color: var(--muted); } .st.reviewed { color: var(--c-appdata); }
84
+ .filters .divider { width: 1px; background: var(--grid); margin: 0 2px; align-self: stretch; }
85
+ .filters button.toggle { border-style: dashed; }
86
+ .filters button.toggle.on { background: var(--c-appdata); border-color: transparent; color: #fff; }
87
+ .rowbtns button.reviewed { background: var(--c-appdata); border-color: transparent; color: #fff; }
88
+ .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
89
+ @media (max-width: 900px) { .grid2 { grid-template-columns: 1fr; } }
90
+ .logbox { font-family: ui-monospace, monospace; font-size: 11.5px; background: var(--page); border: 1px solid var(--grid); border-radius: 8px; padding: 10px; height: 240px; overflow-y: auto; white-space: pre-wrap; color: var(--ink-2); }
91
+ .remote input { font: inherit; font-size: 13px; padding: 6px 9px; border-radius: 7px; border: 1px solid var(--border); background: var(--page); color: var(--ink); width: 100%; }
92
+ .remote .frow { display: grid; grid-template-columns: 80px 1fr; gap: 8px; align-items: center; margin-bottom: 8px; }
93
+ .remote label { font-size: 12.5px; color: var(--ink-2); }
94
+ .hint { font-size: 12px; color: var(--muted); margin-top: 8px; }
95
+ /* tooltip */
96
+ #tip { position: fixed; pointer-events: none; background: var(--ink); color: var(--page); font-size: 12px; padding: 6px 10px; border-radius: 7px; z-index: 50; display: none; max-width: 340px; }
97
+ /* modal */
98
+ #modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: none; align-items: center; justify-content: center; z-index: 40; }
99
+ #modal .box { background: var(--surface); border-radius: 14px; padding: 24px; width: 440px; max-width: 92vw; }
100
+ #modal input { font: inherit; width: 100%; padding: 8px 10px; margin: 12px 0; border-radius: 7px; border: 1px solid var(--border); background: var(--page); color: var(--ink); }
101
+ #modal ul { margin: 10px 0 4px 18px; font-size: 12.5px; color: var(--ink-2); max-height: 180px; overflow-y: auto; }
102
+ section { margin-top: 16px; }
103
+ </style>
104
+ </head>
105
+ <body>
106
+ <header>
107
+ <h1>CleanDrive</h1>
108
+ <span class="meta" id="scanTs">no scan yet</span>
109
+ <span class="spacer"></span>
110
+ <span class="chip" id="diskChip"></span>
111
+ <button id="rescan">Rescan</button>
112
+ </header>
113
+
114
+ <div class="tiles">
115
+ <div class="card tile"><div class="label">Disk used</div><div class="value" id="tUsed">–</div><div class="sub" id="tUsedSub"></div></div>
116
+ <div class="card tile"><div class="label">Free now</div><div class="value" id="tFree">–</div><div class="sub">on the data volume</div></div>
117
+ <div class="card tile"><div class="label">Reclaimable, low risk</div><div class="value" id="tReclaim">–</div><div class="sub">caches + node_modules</div></div>
118
+ <div class="card tile"><div class="label">Approved in plan</div><div class="value" id="tPlan">–</div><div class="sub" id="tPlanSub">nothing approved yet</div></div>
119
+ </div>
120
+
121
+ <div class="card">
122
+ <h2>Capacity</h2>
123
+ <div class="capbar" id="capbar"></div>
124
+ <div class="caplegend" id="caplegend"></div>
125
+ </div>
126
+
127
+ <section class="card">
128
+ <h2>Where the space goes</h2>
129
+ <div id="catbars"></div>
130
+ <div class="hint">Project sizes exclude their node_modules (listed separately). App data and AI models unlock deletion only after you mark them reviewed — then choose rm, approve, and confirm. Applications are never deleted by the dashboard.</div>
131
+ </section>
132
+
133
+ <section class="card">
134
+ <h2>Items</h2>
135
+ <div class="filters" id="filters"></div>
136
+ <div style="overflow-x:auto"><table>
137
+ <thead><tr><th>Path</th><th style="text-align:right">Size</th><th>Risk</th><th>Action</th><th>Status</th><th></th></tr></thead>
138
+ <tbody id="rows"></tbody>
139
+ </table></div>
140
+ </section>
141
+
142
+ <div class="grid2">
143
+ <div class="card">
144
+ <h2>Plan &amp; execution</h2>
145
+ <div class="filters" id="presets"></div>
146
+ <div id="planSummary" style="font-size:13px;color:var(--ink-2);margin-bottom:10px">No approved actions.</div>
147
+ <div style="display:flex;gap:8px;margin-bottom:12px">
148
+ <button class="danger" id="execBtn" disabled>Execute plan…</button>
149
+ <button id="dryrunBtn" disabled>Dry-run archives</button>
150
+ </div>
151
+ <div class="logbox" id="logbox">idle</div>
152
+ </div>
153
+ <div class="card remote">
154
+ <h2>Remote archive target (SSH / Tailscale)</h2>
155
+ <div class="frow"><label>Host</label><input id="rHost" placeholder="raspberrypi (tailscale name)"></div>
156
+ <div class="frow"><label>User</label><input id="rUser" placeholder="pi"></div>
157
+ <div class="frow"><label>Path</label><input id="rPath" placeholder="/mnt/external/mac-archive"></div>
158
+ <div class="frow"><label>SSH key</label><input id="rKey" placeholder="~/.ssh/id_ed25519 (optional — blank uses agent)"></div>
159
+ <div class="frow"><label>SSH port</label><input id="rPort" placeholder="22 (optional)"></div>
160
+ <div style="display:flex;gap:8px;align-items:center">
161
+ <button id="rSave">Save</button>
162
+ <button id="rTest">Test connection</button>
163
+ <span id="rStatus" class="risk" style="font-size:12px"></span>
164
+ </div>
165
+ <div class="hint">Archives are rsync'd preserving the path relative to your home folder, then checksum-verified. Local copies are only removed with the “archive + remove local” action, and only after verification succeeds.</div>
166
+ </div>
167
+ </div>
168
+
169
+ <section class="card">
170
+ <h2>Offload to the remote drive <span class="note" style="text-transform:none;letter-spacing:0;font-weight:400">— like iCloud: a verified copy goes to your remote drive, a small <code>.cloud</code> placeholder stays here</span></h2>
171
+ <div style="display:flex;gap:8px;align-items:center;margin-bottom:10px;flex-wrap:wrap">
172
+ <button id="ofFind">Find candidates</button>
173
+ <button class="primary" id="ofGo" disabled>Offload selected…</button>
174
+ <span class="note" id="ofSummary"></span>
175
+ </div>
176
+ <div style="overflow-x:auto"><table>
177
+ <thead><tr><th></th><th>File</th><th style="text-align:right">Size</th><th>State</th><th></th></tr></thead>
178
+ <tbody id="ofRows"><tr><td colspan="5" class="note">No candidates yet. Click “Find candidates” (videos and other big media ≥ 200 MB, unused for 30 days, duplicates first).</td></tr></tbody>
179
+ </table></div>
180
+ <div class="hint">Nothing runs in the background. The local file is removed only after its sha256 matches on the remote drive. Fetch downloads it, checks it again, and restores the original. The remote copy stays, so offloading it again is instant. CLI: <code>cleandrive offload / fetch / offloaded</code>.</div>
181
+ </section>
182
+
183
+ <div id="tip"></div>
184
+ <div id="modal"><div class="box">
185
+ <h2 style="margin-bottom:6px">Execute approved actions</h2>
186
+ <div style="font-size:13px;color:var(--ink-2)" id="modalSummary"></div>
187
+ <ul id="modalList"></ul>
188
+ <div style="font-size:12.5px;color:var(--ink-2)">Type <b>FREE</b> to confirm. Deletes are permanent; archives are verified before any local removal.</div>
189
+ <input id="confirmWord" placeholder="FREE" autocomplete="off">
190
+ <div style="display:flex;gap:8px;justify-content:flex-end">
191
+ <button id="modalCancel">Cancel</button>
192
+ <button class="danger" id="modalGo" disabled>Execute</button>
193
+ </div>
194
+ </div></div>
195
+
196
+ <script>
197
+ const CAT = {
198
+ pkgcache: { label: 'Package caches', color: 'var(--c-pkgcache)', risk: 'low' },
199
+ cache: { label: 'App & tool caches', color: 'var(--c-cache)', risk: 'low' },
200
+ node_modules:{ label: 'node_modules', color: 'var(--c-nm)', risk: 'low' },
201
+ projects: { label: 'Projects', color: 'var(--c-projects)', risk: 'medium' },
202
+ appdata: { label: 'App data', color: 'var(--c-appdata)', risk: 'review' },
203
+ apps: { label: 'Applications', color: 'var(--c-apps)', risk: 'review' },
204
+ models: { label: 'AI models', color: 'var(--c-models)', risk: 'review' },
205
+ };
206
+ const RISK_LABEL = { low: '✓ low risk', medium: '⚠ verify first', review: '◉ manual review' };
207
+ const ACTIONS = {
208
+ pkgcache: ['clean', 'rm'], cache: ['rm'], node_modules: ['rm'],
209
+ projects: ['archive', 'archive+rm', 'keep'], appdata: [], apps: [], models: [],
210
+ };
211
+ // review-only categories where rm unlocks after "Mark reviewed"; apps stay manual
212
+ const REVIEW_RM = ['appdata', 'models'];
213
+ function actionsFor(it) {
214
+ if (it.protected) return [];
215
+ const base = ACTIONS[it.category] || [];
216
+ if (base.length) return base;
217
+ return REVIEW_RM.includes(it.category) && planOf(it).reviewed ? ['keep', 'rm'] : [];
218
+ }
219
+ let S = { scan: null, plan: { items: {} }, filter: 'all', executing: false };
220
+ const $ = (id) => document.getElementById(id);
221
+ const gb = (kb) => { const g = kb/1048576; return g >= 10 ? g.toFixed(0)+' GB' : g >= 1 ? g.toFixed(1)+' GB' : (kb/1024).toFixed(0)+' MB'; };
222
+ const esc = (s) => s.replace(/[&<>"]/g, (c) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));
223
+
224
+ async function api(path, body) {
225
+ const r = await fetch(path, body ? { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(body) } : undefined);
226
+ return r.json();
227
+ }
228
+
229
+ function planOf(it) { return S.plan.items[it.id] || {}; }
230
+ function effectiveAction(it) { return planOf(it).action || it.suggest; }
231
+ function isApproved(it) { return planOf(it).status === 'approved'; }
232
+
233
+ function tip(el, html) {
234
+ el.addEventListener('mousemove', (e) => { const t = $('tip'); t.innerHTML = html(); t.style.display = 'block'; t.style.left = Math.min(e.clientX + 14, innerWidth - 360) + 'px'; t.style.top = (e.clientY + 14) + 'px'; });
235
+ el.addEventListener('mouseleave', () => { $('tip').style.display = 'none'; });
236
+ }
237
+
238
+ function render() {
239
+ const scan = S.scan;
240
+ if (!scan) return;
241
+ const d = S.disk || scan.disk;
242
+ $('scanTs').textContent = 'scanned ' + new Date(scan.ts).toLocaleString();
243
+ $('tUsed').textContent = d.pct + '%';
244
+ $('tUsedSub').textContent = gb(d.usedKB) + ' of ' + gb(d.sizeKB);
245
+ $('tFree').textContent = gb(d.availKB);
246
+
247
+ const chip = $('diskChip');
248
+ const level = d.pct >= 95 ? ['critical', '⛔ critical'] : d.pct >= 85 ? ['serious', '⚠ tight'] : ['good', '✓ healthy'];
249
+ chip.innerHTML = '<span class="dot" style="background:var(--' + level[0] + ')"></span>' + level[1] + ' · ' + d.pct + '% full';
250
+
251
+ const lowRisk = scan.items.filter((i) => CAT[i.category]?.risk === 'low');
252
+ const reclaimKB = lowRisk.reduce((s, i) => s + i.kb, 0);
253
+ $('tReclaim').textContent = gb(reclaimKB);
254
+ const approved = scan.items.filter(isApproved);
255
+ const apKB = approved.reduce((s, i) => s + i.kb, 0);
256
+ $('tPlan').textContent = approved.length ? gb(apKB) : '0';
257
+ $('tPlanSub').textContent = approved.length ? approved.length + ' action(s) approved' : 'nothing approved yet';
258
+ $('execBtn').disabled = !approved.length || S.executing;
259
+ $('dryrunBtn').disabled = !approved.some((i) => (effectiveAction(i) || '').startsWith('archive')) || S.executing;
260
+ $('planSummary').textContent = approved.length
261
+ ? approved.length + ' approved action(s), ~' + gb(apKB) + ' affected.'
262
+ : 'No approved actions. Approve items in the table above.';
263
+
264
+ // one-click presets: bulk-approve whole low-risk groups with their suggested action
265
+ const presetDefs = [
266
+ { label: 'caches', cats: ['pkgcache', 'cache'] },
267
+ { label: 'caches + node_modules', cats: ['pkgcache', 'cache', 'node_modules'] },
268
+ ];
269
+ const pr = $('presets'); pr.innerHTML = '';
270
+ for (const def of presetDefs) {
271
+ const eligible = scan.items.filter((i) => def.cats.includes(i.category) && !i.protected
272
+ && !['done', 'skipped'].includes(planOf(i).status));
273
+ if (!eligible.length) continue;
274
+ const kb = eligible.reduce((s, i) => s + i.kb, 0);
275
+ const b = document.createElement('button');
276
+ b.textContent = 'Select ' + def.label + ' — ' + gb(kb);
277
+ b.disabled = S.executing;
278
+ b.onclick = async () => {
279
+ await api('/api/plan/bulk', { changes: eligible.map((i) => ({ id: i.id, status: 'approved', action: planOf(i).action || i.suggest })) });
280
+ refresh();
281
+ };
282
+ pr.appendChild(b);
283
+ }
284
+ if (approved.length) {
285
+ const c = document.createElement('button');
286
+ c.textContent = 'Clear selection';
287
+ c.disabled = S.executing;
288
+ c.onclick = async () => {
289
+ await api('/api/plan/bulk', { changes: approved.map((i) => ({ id: i.id, status: 'proposed' })) });
290
+ refresh();
291
+ };
292
+ pr.appendChild(c);
293
+ }
294
+
295
+ // capacity bar: other-used | reclaimable | free (APFS: used+avail is the real total)
296
+ const totKB = d.usedKB + d.availKB;
297
+ const otherKB = Math.max(0, d.usedKB - reclaimKB);
298
+ const segs = [
299
+ { kb: otherKB, color: 'var(--seq-mid)', label: 'Used (everything else)' },
300
+ { kb: reclaimKB, color: 'var(--seq-light)', label: 'Reclaimable low-risk (caches, node_modules)' },
301
+ { kb: d.availKB, color: 'transparent', label: 'Free' },
302
+ ];
303
+ const bar = $('capbar'); bar.innerHTML = '';
304
+ const leg = $('caplegend'); leg.innerHTML = '';
305
+ for (const s of segs) {
306
+ const el = document.createElement('div');
307
+ el.className = 'seg';
308
+ el.style.width = (s.kb / totKB * 100) + '%';
309
+ el.style.background = s.color;
310
+ tip(el, () => '<b>' + s.label + '</b><br>' + gb(s.kb) + ' · ' + (s.kb / totKB * 100).toFixed(1) + '%');
311
+ bar.appendChild(el);
312
+ leg.insertAdjacentHTML('beforeend', '<span><span class="sw" style="background:' + s.color + '"></span>' + s.label + ' — ' + gb(s.kb) + '</span>');
313
+ }
314
+
315
+ // category bars
316
+ const totals = Object.keys(CAT).map((k) => ({ k, kb: scan.items.filter((i) => i.category === k).reduce((s, i) => s + i.kb, 0) }));
317
+ const maxKB = Math.max(...totals.map((t) => t.kb), 1);
318
+ const cb = $('catbars'); cb.innerHTML = '';
319
+ for (const t of totals) {
320
+ if (!t.kb) continue;
321
+ const row = document.createElement('div');
322
+ row.className = 'catrow';
323
+ row.innerHTML = '<span class="name"><span class="sw" style="display:inline-block;width:10px;height:10px;border-radius:3px;background:' + CAT[t.k].color + '"></span>' + CAT[t.k].label + '</span>'
324
+ + '<span class="track"><span class="bar" style="width:' + (t.kb / maxKB * 100) + '%;background:' + CAT[t.k].color + '"></span></span>'
325
+ + '<span class="val">' + gb(t.kb) + '</span>';
326
+ tip(row.querySelector('.track'), () => '<b>' + CAT[t.k].label + '</b><br>' + gb(t.kb) + ' across ' + scan.items.filter((i) => i.category === t.k).length + ' item(s)');
327
+ cb.appendChild(row);
328
+ }
329
+
330
+ // filters
331
+ const fl = $('filters'); fl.innerHTML = '';
332
+ const cats = ['all', ...Object.keys(CAT).filter((k) => scan.items.some((i) => i.category === k))];
333
+ for (const c of cats) {
334
+ const b = document.createElement('button');
335
+ b.textContent = c === 'all' ? 'All' : CAT[c].label;
336
+ b.className = S.filter === c ? 'on' : '';
337
+ b.onclick = () => { S.filter = c; render(); };
338
+ fl.appendChild(b);
339
+ }
340
+ // Review-progress: how many review-only items are handled (reviewed or skipped)
341
+ const reviewItems = scan.items.filter((i) => CAT[i.category].risk === 'review');
342
+ const handled = reviewItems.filter((i) => ['reviewed', 'skipped'].includes(planOf(i).status)).length;
343
+ if (reviewItems.length) {
344
+ const div = document.createElement('span'); div.className = 'divider'; fl.appendChild(div);
345
+ const t = document.createElement('button');
346
+ t.className = 'toggle' + (S.hideReviewed ? ' on' : '');
347
+ t.textContent = (S.hideReviewed ? '☑' : '☐') + ' Hide handled — ' + handled + '/' + reviewItems.length + ' reviewed';
348
+ t.title = 'Hide review-only items you have marked reviewed or skipped';
349
+ t.onclick = () => { S.hideReviewed = !S.hideReviewed; render(); };
350
+ fl.appendChild(t);
351
+ }
352
+
353
+ // table
354
+ const rows = $('rows'); rows.innerHTML = '';
355
+ const visible = scan.items.filter((i) => S.filter === 'all' || i.category === S.filter)
356
+ .filter((i) => !(S.hideReviewed && CAT[i.category].risk === 'review' && ['reviewed', 'skipped'].includes(planOf(i).status)));
357
+ for (const it of visible.slice(0, 80)) {
358
+ const p = planOf(it);
359
+ const opts = actionsFor(it);
360
+ // review items default to 'keep' so rm is always a deliberate pick
361
+ const act = opts.length && !opts.includes(effectiveAction(it)) ? opts[0] : effectiveAction(it);
362
+ const tr = document.createElement('tr');
363
+ const shortPath = it.path.replace(/^\/Users\/[^/]+/, '~');
364
+ tr.innerHTML =
365
+ '<td><div class="path">' + esc(shortPath) + '</div><div class="note">' + esc(it.note || '') + '</div></td>'
366
+ + '<td class="num">' + gb(it.kb) + '</td>'
367
+ + '<td><span class="risk ' + CAT[it.category].risk + '">' + (it.protected ? '🔒 protected' : RISK_LABEL[CAT[it.category].risk]) + '</span></td>'
368
+ + '<td>' + (opts.length ? '<select>' + opts.map((o) => '<option' + (o === act ? ' selected' : '') + '>' + o + '</option>').join('') + '</select>' : '<span class="note">' + esc(it.suggest) + '</span>') + '</td>'
369
+ + '<td><span class="st ' + (p.status || '') + '">' + (p.status || '·') + '</span></td>'
370
+ + '<td><div class="rowbtns">'
371
+ + (opts.length && !['keep'].includes(act) ? '<button data-a="approve">' + (p.status === 'approved' ? 'Unapprove' : 'Approve') + '</button>' : '')
372
+ + (CAT[it.category].risk === 'review' && !it.protected ? '<button data-a="review" class="' + (p.status === 'reviewed' ? 'reviewed' : '') + '">' + (p.status === 'reviewed' ? '✓ Reviewed' : 'Mark reviewed') + '</button>' : '')
373
+ + '<button data-a="skip">Skip</button></div></td>';
374
+ const sel = tr.querySelector('select');
375
+ if (sel) sel.onchange = async () => { await api('/api/plan', { id: it.id, action: sel.value }); refresh(); };
376
+ tr.querySelectorAll('button').forEach((b) => {
377
+ b.onclick = async () => {
378
+ if (b.dataset.a === 'review') {
379
+ const on = p.status !== 'reviewed';
380
+ await api('/api/plan', { id: it.id, status: on ? 'reviewed' : 'proposed', reviewed: on });
381
+ return refresh();
382
+ }
383
+ let status;
384
+ if (b.dataset.a === 'skip') status = p.status === 'skipped' ? 'proposed' : 'skipped';
385
+ else status = p.status === 'approved' ? 'proposed' : 'approved';
386
+ await api('/api/plan', { id: it.id, status, action: sel ? sel.value : it.suggest });
387
+ refresh();
388
+ };
389
+ });
390
+ rows.appendChild(tr);
391
+ }
392
+ if (visible.length > 80) rows.insertAdjacentHTML('beforeend', '<tr><td colspan="6" class="note" style="padding:10px 8px">… ' + (visible.length - 80) + ' smaller items not shown (see CLI: cleandrive state)</td></tr>');
393
+ }
394
+
395
+ async function refresh() {
396
+ const st = await api('/api/state');
397
+ S.scan = st.scan; S.plan = st.plan || { items: {} }; S.disk = st.disk; S.executing = st.executing;
398
+ const cfgR = st.config?.remote || {};
399
+ if (document.activeElement?.tagName !== 'INPUT') {
400
+ $('rHost').value = cfgR.host || ''; $('rUser').value = cfgR.user || ''; $('rPath').value = cfgR.path || '';
401
+ $('rKey').value = cfgR.sshKey || ''; $('rPort').value = cfgR.sshPort || '';
402
+ }
403
+ $('rescan').textContent = st.scanning ? 'Scanning… (' + (st.scanStep || '') + ')' : 'Rescan';
404
+ $('rescan').disabled = st.scanning;
405
+ const lb = $('logbox');
406
+ if (st.log?.length) { lb.textContent = st.log.join('\n'); lb.scrollTop = lb.scrollHeight; }
407
+ render();
408
+ }
409
+
410
+ $('rescan').onclick = async () => { await api('/api/scan', {}); refresh(); };
411
+ $('rSave').onclick = async () => {
412
+ await api('/api/remote/config', { host: $('rHost').value.trim(), user: $('rUser').value.trim(), path: $('rPath').value.trim(), sshKey: $('rKey').value.trim(), sshPort: $('rPort').value.trim() });
413
+ $('rStatus').textContent = 'saved'; setTimeout(() => $('rStatus').textContent = '', 1500);
414
+ };
415
+ $('rTest').onclick = async () => {
416
+ $('rStatus').textContent = 'testing…';
417
+ const r = await api('/api/remote/test', {});
418
+ $('rStatus').textContent = (r.ok ? '✓ ' : '✗ ') + r.msg;
419
+ $('rStatus').style.color = r.ok ? 'var(--good-ink)' : 'var(--critical)';
420
+ };
421
+ $('dryrunBtn').onclick = async () => { await api('/api/offload/dryrun', {}); };
422
+ $('execBtn').onclick = () => {
423
+ const approved = S.scan.items.filter(isApproved);
424
+ $('modalSummary').textContent = approved.length + ' action(s), ~' + gb(approved.reduce((s, i) => s + i.kb, 0)) + ':';
425
+ $('modalList').innerHTML = approved.map((i) => '<li>' + esc(effectiveAction(i)) + ' — ' + esc(i.path.replace(/^\/Users\/[^/]+/, '~')) + ' (' + gb(i.kb) + ')</li>').join('');
426
+ $('confirmWord').value = ''; $('modalGo').disabled = true;
427
+ $('modal').style.display = 'flex'; $('confirmWord').focus();
428
+ };
429
+ $('confirmWord').oninput = () => { $('modalGo').disabled = $('confirmWord').value.trim() !== 'FREE'; };
430
+ $('modalCancel').onclick = () => { $('modal').style.display = 'none'; };
431
+ $('modalGo').onclick = async () => {
432
+ $('modal').style.display = 'none';
433
+ await api('/api/execute', { confirm: 'FREE' });
434
+ refresh();
435
+ };
436
+
437
+ // ---- offload card ----
438
+ const OF = { sel: new Set(), data: null };
439
+ async function refreshOffload() {
440
+ OF.data = await api('/api/offload');
441
+ const d = OF.data, rows = $('ofRows');
442
+ const offloaded = d.files.filter((f) => f.state === 'offloaded');
443
+ const known = new Set(d.files.filter((f) => f.state !== 'local').map((f) => f.path));
444
+ const cands = (d.candidates?.candidates || []).filter((c) => !known.has(c.path));
445
+ const list = [...offloaded.map((f) => ({ ...f, kind: 'off' })), ...cands.map((c) => ({ ...c, kind: 'cand' }))];
446
+ $('ofFind').disabled = d.finding; $('ofFind').textContent = d.finding ? 'Searching…' : 'Find candidates';
447
+ const selKB = cands.filter((c) => OF.sel.has(c.path)).reduce((s, c) => s + c.kb, 0);
448
+ $('ofGo').disabled = !OF.sel.size || S.executing;
449
+ $('ofSummary').textContent = offloaded.length + ' offloaded (' + gb(offloaded.reduce((s, f) => s + f.size, 0) / 1024) + ' on the remote drive)'
450
+ + (cands.length ? ' · ' + cands.length + ' candidates, ' + gb(cands.reduce((s, c) => s + c.kb, 0)) : '')
451
+ + (OF.sel.size ? ' · selected ' + gb(selKB) : '') + (d.candidates?.unreadable ? ' · ⚠ some folders unreadable (Full Disk Access)' : '');
452
+ if (!list.length) return;
453
+ rows.innerHTML = '';
454
+ for (const it of list.slice(0, 60)) {
455
+ const tr = document.createElement('tr');
456
+ const sp = it.path.replace(/^\/Users\/[^/]+/, '~');
457
+ const sub = it.kind === 'off' ? 'offloaded ' + new Date(it.offloadedAt).toLocaleDateString()
458
+ : (it.duplicateOf ? '⧉ duplicate of ' + it.duplicateOf.replace(/^\/Users\/[^/]+/, '~') + ' · ' : '') + 'unused ' + it.idleDays + ' d';
459
+ tr.innerHTML = '<td>' + (it.kind === 'cand' ? '<input type="checkbox"' + (OF.sel.has(it.path) ? ' checked' : '') + '>' : '☁︎') + '</td>'
460
+ + '<td><div class="path">' + esc(sp) + '</div><div class="note">' + esc(sub) + '</div></td>'
461
+ + '<td class="num">' + gb(it.kind === 'off' ? it.size / 1024 : it.kb) + '</td>'
462
+ + '<td><span class="st ' + (it.kind === 'off' ? 'done' : '') + '">' + (it.kind === 'off' ? 'remote' : 'local') + '</span></td>'
463
+ + '<td><div class="rowbtns">' + (it.kind === 'off' ? '<button data-a="fetch">Fetch</button>' : '<button data-a="pin">Keep local</button>') + '</div></td>';
464
+ const cb = tr.querySelector('input');
465
+ if (cb) cb.onchange = () => { cb.checked ? OF.sel.add(it.path) : OF.sel.delete(it.path); refreshOffload(); };
466
+ tr.querySelectorAll('button').forEach((b) => b.onclick = async () => {
467
+ if (b.dataset.a === 'fetch') { b.disabled = true; await api('/api/offload/fetch', { paths: [it.path] }); }
468
+ else { OF.sel.delete(it.path); await api('/api/offload/pin', { path: it.path, pinned: true }); }
469
+ refreshOffload();
470
+ });
471
+ rows.appendChild(tr);
472
+ }
473
+ }
474
+ $('ofFind').onclick = async () => { await api('/api/offload/candidates', {}); refreshOffload(); };
475
+ $('ofGo').onclick = async () => {
476
+ const paths = [...OF.sel];
477
+ const w = prompt(paths.length + ' file(s) go to the remote drive. Each one is verified before the local copy is removed.\nType OFFLOAD to confirm:');
478
+ if ((w || '').trim() !== 'OFFLOAD') return;
479
+ await api('/api/offload/run', { paths, confirm: 'OFFLOAD' });
480
+ OF.sel.clear(); refreshOffload();
481
+ };
482
+
483
+ refresh();
484
+ refreshOffload();
485
+ setInterval(refresh, 2500);
486
+ setInterval(refreshOffload, 5000);
487
+ </script>
488
+ </body>
489
+ </html>