Spaces:
Sleeping
Sleeping
File size: 11,628 Bytes
ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 61fcdc2 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 2fe2727 ffa0093 | 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | import { getFileIcon, getFileEmoji, getExt, isImage, formatSize, formatDate } from '../utils/formatters.js';
export class UIRenderer {
constructor(stateManager, hfService) {
this.state = stateManager;
this.hf = hfService;
this.containers = {
folders: document.getElementById('foldersContainer'),
files: document.getElementById('filesContainer'),
breadcrumbs: document.getElementById('breadcrumbs'),
toast: document.getElementById('toastContainer'),
progress: document.getElementById('uploadProgress'),
progressText: document.getElementById('progressText')
};
}
showToast(msg, type = 'info') {
const icons = {
success: 'ph-fill ph-check-circle',
error: 'ph-fill ph-warning-circle',
info: 'ph-fill ph-info',
warning: 'ph-fill ph-warning'
};
const t = document.createElement('div');
t.className = `toast toast-${type}`;
t.innerHTML = `<i class="${icons[type] || icons.info}"></i><span>${msg}</span>`;
this.containers.toast.appendChild(t);
requestAnimationFrame(() => t.classList.add('show'));
setTimeout(() => {
t.classList.remove('show');
setTimeout(() => t.remove(), 400);
}, 3500);
}
showProgress(msg = 'Working...') {
this.containers.progressText.textContent = msg;
this.containers.progress.classList.add('active');
}
hideProgress() {
this.containers.progress.classList.remove('active');
}
renderBreadcrumbs(onCrumbClick) {
this.containers.breadcrumbs.innerHTML = '';
const root = document.createElement('span');
root.className = 'breadcrumb-item' + (this.state.currentPath.length === 0 ? ' active' : '');
root.textContent = 'My Files';
root.onclick = () => onCrumbClick([]);
this.containers.breadcrumbs.appendChild(root);
this.state.currentPath.forEach((seg, idx) => {
const sep = document.createElement('span');
sep.className = 'breadcrumb-separator';
sep.innerHTML = '<i class="ph-bold ph-caret-right" style="font-size: 14px; margin: 0 4px"></i>';
this.containers.breadcrumbs.appendChild(sep);
const crumb = document.createElement('span');
crumb.className = 'breadcrumb-item' + (idx === this.state.currentPath.length - 1 ? ' active' : '');
crumb.textContent = seg;
crumb.onclick = () => onCrumbClick(this.state.currentPath.slice(0, idx + 1));
this.containers.breadcrumbs.appendChild(crumb);
});
}
renderFolders(folders, onFolderClick, onRename, onDelete) {
this.containers.folders.innerHTML = '';
if (!folders.length) return;
folders.forEach(folder => {
const card = document.createElement('div');
card.className = 'folder-card';
card.innerHTML = `
<div class="card-top-row">
<div class="item-icon">
<svg class="folder-icon" width="48" height="48" viewBox="0 0 24 24" fill="none">
<path d="M2 6.75C2 5.784 2.784 5 3.75 5H9.5l1.5 2H20.25C21.216 7 22 7.784 22 8.75v9.5A1.75 1.75 0 0 1 20.25 20H3.75A1.75 1.75 0 0 1 2 18.25z" fill="var(--folder-color)" opacity="0.3"/>
<path d="M2 8.75C2 7.784 2.784 7 3.75 7H20.25C21.216 7 22 7.784 22 8.75v9.5A1.75 1.75 0 0 1 20.25 20H3.75A1.75 1.75 0 0 1 2 18.25z" fill="var(--folder-color)"/>
</svg>
</div>
<div class="card-menu">
<button class="icon-btn card-menu-btn" title="More options"><i class="ph-bold ph-dots-three-vertical"></i></button>
<div class="dropdown-menu">
<button class="dropdown-item" data-action="rename">
<i class="ph-fill ph-pencil-simple"></i> Rename
</button>
<button class="dropdown-item danger" data-action="delete">
<i class="ph-fill ph-trash"></i> Delete
</button>
</div>
</div>
</div>
<div class="item-name">${folder.name}</div>
<div class="item-meta">Folder</div>`;
card.onclick = (e) => {
e.preventDefault();
e.stopPropagation();
if (e.target.closest('.card-menu')) return;
onFolderClick(folder.name);
};
card.querySelector('[data-action="rename"]').onclick = (e) => {
e.stopPropagation();
if (onRename) onRename(folder.path, folder.name);
};
card.querySelector('[data-action="delete"]').onclick = (e) => {
e.stopPropagation();
if (onDelete) onDelete(folder.path, folder.name);
};
const menuBtn = card.querySelector('.card-menu-btn');
const menu = card.querySelector('.dropdown-menu');
menuBtn.onclick = (e) => {
e.stopPropagation();
menu.classList.toggle('open');
};
this.containers.folders.appendChild(card);
});
}
renderFiles(files, actions) {
this.containers.files.innerHTML = '';
const mode = this.state.viewMode;
this.containers.files.className = mode === 'grid' ? 'grid-container' : 'list-container';
if (!files.length) {
this.showEmpty();
return;
}
files.forEach(file => {
const card = document.createElement('div');
card.className = mode === 'grid' ? 'file-card' : 'file-list-item';
const { icon, color } = getFileIcon(file.name);
const ext = getExt(file.name).toUpperCase();
const size = formatSize(file.size);
const starred = this.state.starred.includes(file.path);
const url = actions.getUrl(file.path);
if (mode === 'grid') {
const isImg = isImage(file.name);
const previewHTML = isImg
? `<img src="${url}" alt="${file.name}" loading="lazy" onerror="this.parentElement.innerHTML='<span class=\\'file-icon\\'>📄</span>'">`
: `<span class="file-icon">${getFileEmoji(ext)}</span>`;
card.innerHTML = `
<div class="file-preview">${previewHTML}</div>
<div class="file-info">
<span class="file-type" style="background-color: ${color}20; color: ${color}">${ext}</span>
<h4 class="file-name" title="${file.name}">${file.name}</h4>
<p class="file-meta">${size} • ${file.lastModified ? formatDate(file.lastModified) : 'Recently'}</p>
</div>
<div class="file-actions">⋮</div>
<div class="quick-actions">
<button class="quick-btn" data-action="preview" title="Preview"><i class="ph-fill ph-eye"></i></button>
<button class="quick-btn" data-action="download" title="Download"><i class="ph-fill ph-download-simple"></i></button>
<button class="quick-btn" data-action="rename" title="Rename"><i class="ph-fill ph-pencil-simple"></i></button>
<button class="quick-btn" data-action="history" title="History"><i class="ph-fill ph-clock-counter-clockwise"></i></button>
</div>`;
} else {
card.innerHTML = `
<div class="list-icon" style="color: ${color}"><i class="${icon}"></i></div>
<div class="list-info">
<h4 class="list-name" title="${file.name}">${file.name}</h4>
<span class="list-meta">${size} • ${ext}</span>
</div>
<div class="list-actions">
<button class="icon-btn" data-action="star" title="${starred ? 'Unstar' : 'Star'}"><i class="ph-fill ph-star${starred ? '' : '-bold'}"></i></button>
<button class="icon-btn" data-action="rename" title="Rename"><i class="ph-fill ph-pencil-simple"></i></button>
<button class="icon-btn" data-action="history" title="History"><i class="ph-fill ph-clock-counter-clockwise"></i></button>
<button class="icon-btn" data-action="download" title="Download"><i class="ph-fill ph-download-simple"></i></button>
<button class="icon-btn" data-action="delete" title="Delete"><i class="ph-fill ph-trash"></i></button>
</div>`;
}
card.onclick = (e) => {
if (e.target.closest('.file-actions, .quick-actions, .list-actions')) return;
actions.onPreview(file);
};
// Action Handlers
card.addEventListener('click', (e) => {
const btn = e.target.closest('[data-action]');
if (!btn) return;
const action = btn.dataset.action;
if (action === 'preview') actions.onPreview(file);
else if (action === 'download') actions.onDownload(url, file.name);
else if (action === 'rename') actions.onRename(file.path, file.name);
else if (action === 'star') actions.onStar(file.path);
else if (action === 'delete') actions.onDelete(file.path, file.name);
else if (action === 'history') actions.onHistory(file.path, file.name);
});
this.containers.files.appendChild(card);
});
}
showEmpty() {
this.containers.files.innerHTML = `
<div class="empty-state">
<i class="ph-fill ph-folder-open"></i>
<h3>Nothing here yet</h3>
<p>Upload files or create folders to get started.</p>
</div>`;
}
showError(message = 'Unable to load files right now.') {
this.containers.folders.innerHTML = '';
this.containers.files.innerHTML = `
<div class="empty-state">
<i class="ph-fill ph-warning-circle"></i>
<h3>Connection Problem</h3>
<p>${message}</p>
</div>`;
}
showSkeletons(foldersCount = 3, filesCount = 6) {
this.containers.folders.innerHTML = '';
this.containers.files.innerHTML = '';
for (let i = 0; i < foldersCount; i++) {
const el = document.createElement('div');
el.className = 'skeleton skeleton-card';
this.containers.folders.appendChild(el);
}
for (let i = 0; i < filesCount; i++) {
const el = document.createElement('div');
el.className = 'skeleton skeleton-card';
this.containers.files.appendChild(el);
}
}
showHistoryModal(fileName) {
const modal = document.getElementById('historyModal');
const nameEl = document.getElementById('historyFileName');
const list = document.getElementById('historyList');
nameEl.textContent = fileName;
list.innerHTML = '<div class="loading-state-sm"><div class="spinner-sm"></div><p>Fetching history...</p></div>';
modal.classList.add('active');
}
renderHistory(history, onRestore) {
const list = document.getElementById('historyList');
if (!history.length) {
list.innerHTML = '<div class="empty-state-sm"><p>No version history found.</p></div>';
return;
}
list.innerHTML = '';
history.forEach((commit, idx) => {
const item = document.createElement('div');
item.className = 'history-item';
const date = new Date(commit.timestamp).toLocaleString();
const shortId = commit.id.substring(0, 7);
item.innerHTML = `
<div class="history-info">
<span class="history-commit-msg" title="${commit.message}">${commit.message}</span>
<span class="history-meta">${date} • ${shortId} by ${commit.author}</span>
</div>
<div class="history-actions">
${idx === 0 ? '<span class="current-version-badge">Current</span>' : `
<button class="restore-btn copy" data-action="copy" title="Restore as a new file">Save as Copy</button>
<button class="restore-btn overwrite" data-action="overwrite" title="Overwrites current file!">Overwrite</button>
`}
</div>
`;
item.querySelectorAll('.restore-btn').forEach(btn => {
btn.onclick = () => {
const asCopy = btn.dataset.action === 'copy';
onRestore(commit.id, asCopy);
};
});
list.appendChild(item);
});
}
}
|