-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
211 lines (178 loc) · 6.65 KB
/
renderer.js
File metadata and controls
211 lines (178 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Renderer logic
const selectDirBtn = document.getElementById('select-dir-btn');
const modulesList = document.getElementById('modules-list');
const totalSizeEl = document.getElementById('total-size');
const scanStatus = document.getElementById('scan-status');
const nukeBtn = document.getElementById('nuke-btn');
const listHeader = document.getElementById('list-header');
const selectAllCheckbox = document.getElementById('select-all');
let foundModules = []; // Array of { path, size }
let selectedIndices = new Set();
let isScanning = false;
selectDirBtn.addEventListener('click', async () => {
const p = await window.api.selectDirectory();
if (p) {
startScan(p);
}
});
async function startScan(path) {
if (isScanning) return;
isScanning = true;
scanStatus.classList.remove('hidden');
modulesList.innerHTML = '';
listHeader.classList.add('hidden');
nukeBtn.disabled = true;
foundModules = [];
selectedIndices.clear();
updateTotalSize();
try {
// We will implement the actual progressive scanning in main process next
// For now, let's assume scanDirectory returns the full list for simplicity
// or we handle progress events.
// Let's assume we want to listen to progress.
// This is a placeholder call until we implement the full logic in main
const results = await window.api.scanDirectory(path);
// results is array of { path, sizeBytes, sizeFormatted }
foundModules = results;
applySort(); // Default sort applied
} catch (err) {
console.error(err);
scanStatus.innerText = 'Error scanning directory.';
} finally {
isScanning = false;
scanStatus.classList.add('hidden');
if (foundModules.length > 0) {
listHeader.classList.remove('hidden');
}
}
}
function renderList() {
modulesList.innerHTML = '';
foundModules.forEach((mod, index) => {
const li = document.createElement('li');
li.className = 'module-item';
const checkboxContainer = document.createElement('label');
checkboxContainer.className = 'checkbox-container';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.dataset.index = index;
checkbox.checked = selectedIndices.has(index);
checkbox.addEventListener('change', (e) => toggleSelection(index, e.target.checked));
const checkmark = document.createElement('span');
checkmark.className = 'checkmark';
checkboxContainer.appendChild(checkbox);
checkboxContainer.appendChild(checkmark);
const pathSpan = document.createElement('span');
pathSpan.className = 'module-path';
pathSpan.innerText = mod.path;
pathSpan.title = mod.path; // Tooltip
const sizeSpan = document.createElement('span');
sizeSpan.className = 'module-size';
sizeSpan.innerText = mod.sizeFormatted;
li.appendChild(checkboxContainer);
li.appendChild(pathSpan);
li.appendChild(sizeSpan);
modulesList.appendChild(li);
});
updateUIState();
}
function toggleSelection(index, isSelected) {
if (isSelected) {
selectedIndices.add(index);
} else {
selectedIndices.delete(index);
}
updateUIState();
}
function updateUIState() {
// Update Nuke button
nukeBtn.disabled = selectedIndices.size === 0;
nukeBtn.innerText = selectedIndices.size > 0 ? `NUKE ${selectedIndices.size} FOLDER${selectedIndices.size === 1 ? '' : 'S'}` : 'NUKE SELECTED';
// Update Select All checkbox
const allSelected = foundModules.length > 0 && selectedIndices.size === foundModules.length;
selectAllCheckbox.checked = allSelected;
// Update total size found (just aesthetic, valid logic would sum selected)
updateTotalSize();
}
// Sorting Logic
let currentSort = { field: 'sizeBytes', dir: 'desc' }; // default sort
document.querySelectorAll('.sortable').forEach(el => {
el.addEventListener('click', () => {
const field = el.dataset.sort;
if (currentSort.field === field) {
currentSort.dir = currentSort.dir === 'asc' ? 'desc' : 'asc';
} else {
currentSort.field = field;
currentSort.dir = 'desc'; // default to desc for new field
}
applySort();
});
});
function applySort() {
// We need to preserve selection.
// Best way: Map selectedIndices to paths, sort, then re-map paths to new indices.
const selectedPaths = new Set();
selectedIndices.forEach(i => {
if (foundModules[i]) selectedPaths.add(foundModules[i].path);
});
foundModules.sort((a, b) => {
let valA = a[currentSort.field];
let valB = b[currentSort.field];
if (typeof valA === 'string') valA = valA.toLowerCase();
if (typeof valB === 'string') valB = valB.toLowerCase();
if (valA < valB) return currentSort.dir === 'asc' ? -1 : 1;
if (valA > valB) return currentSort.dir === 'asc' ? 1 : -1;
return 0;
});
// Re-calculate selected indices based on paths
selectedIndices.clear();
foundModules.forEach((m, index) => {
if (selectedPaths.has(m.path)) {
selectedIndices.add(index);
}
});
renderList();
updateSortIndicators();
}
function updateSortIndicators() {
document.querySelectorAll('.sortable span').forEach(span => span.innerText = '↕');
const activeHeader = document.querySelector(`.sortable[data-sort="${currentSort.field}"] span`);
if (activeHeader) {
activeHeader.innerText = currentSort.dir === 'asc' ? '↑' : '↓';
}
}
selectAllCheckbox.addEventListener('change', (e) => {
const isChecked = e.target.checked;
selectedIndices.clear();
if (isChecked) {
foundModules.forEach((_, i) => selectedIndices.add(i));
}
// Re-render essentially to update all checkboxes efficiently?
// Or just iterate DOM. Re-rendering is cleaner for this scale.
renderList();
});
nukeBtn.addEventListener('click', async () => {
if (confirm(`Are you sure you want to delete ${selectedIndices.size} folders? This cannot be undone.`)) {
const pathsToDelete = Array.from(selectedIndices).map(i => foundModules[i].path);
await window.api.deleteDirectories(pathsToDelete);
// Remove from list
// Sort indices descending to remove from back without messing up indices?
// Actually simplest is filter out deleted ones from foundModules and re-render.
const deletedSet = new Set(pathsToDelete);
foundModules = foundModules.filter(m => !deletedSet.has(m.path));
selectedIndices.clear();
renderList();
}
});
function updateTotalSize() {
let totalBytes = 0;
foundModules.forEach(m => totalBytes += m.sizeBytes);
totalSizeEl.innerText = formatSize(totalBytes) + ' found';
}
function formatSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}