From e3256bb3b57b86c2fc1d7a64275973cd5582365f Mon Sep 17 00:00:00 2001 From: DioCrafts Date: Fri, 4 Apr 2025 04:30:49 +0200 Subject: [PATCH] fix ui home folder --- static/js/app.js | 91 ++++++++++++++++++++++++++++++++++-------- static/js/favorites.js | 24 ++++++++--- static/js/ui.js | 9 ++++- 3 files changed, 101 insertions(+), 23 deletions(-) diff --git a/static/js/app.js b/static/js/app.js index d7377be9..b79182e9 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -385,9 +385,32 @@ function setupEventListeners() { */ async function loadFiles() { try { - let url = '/api/folders'; - if (app.currentPath) { - // Use the correct endpoint for folder contents + // Always ensure a userHomeFolderId is set + if (!app.userHomeFolderId) { + // If we don't have a home folder ID yet, try to get the user's username + const USER_DATA_KEY = 'oxicloud_user'; + const userData = JSON.parse(localStorage.getItem(USER_DATA_KEY) || '{}'); + if (userData.username) { + // Find user's home folder + await findUserHomeFolder(userData.username); + } + } + + let url; + // ALWAYS use the userHomeFolderId (current folder or home folder) to avoid showing root + if (!app.currentPath || app.currentPath === '') { + // If at root, force user to their home folder + if (app.userHomeFolderId) { + url = `/api/folders/${app.userHomeFolderId}/contents`; + app.currentPath = app.userHomeFolderId; + ui.updateBreadcrumb(app.userHomeFolderName || 'Home'); + } else { + // Emergency fallback - this should rarely happen but prevents errors + url = '/api/folders'; + console.warn("Emergency fallback to root folder - this should not normally happen"); + } + } else { + // Normal case - viewing subfolder contents url = `/api/folders/${app.currentPath}/contents`; } @@ -440,7 +463,29 @@ async function loadFiles() { // Add folders (check if it's an array) const folderList = Array.isArray(folders) ? folders : []; - folderList.forEach(folder => { + + // Get user info for filtering + const USER_DATA_KEY = 'oxicloud_user'; + const userData = JSON.parse(localStorage.getItem(USER_DATA_KEY) || '{}'); + const username = userData.username || ''; + + // Filter folders before adding them to the view + const visibleFolders = folderList.filter(folder => { + // Skip system folders (starting with dot) when at root + if (!app.currentPath && folder.name.startsWith('.')) { + return false; + } + + // Skip other users' folders when at root + if (!app.currentPath && folder.name.startsWith('Mi Carpeta - ') && !folder.name.includes(username)) { + return false; + } + + return true; + }); + + // Add filtered folders to the view + visibleFolders.forEach(folder => { ui.addFolderToView(folder); }); @@ -852,9 +897,14 @@ function switchToFilesView() { filesListView.style.display = app.currentView === 'list' ? 'block' : 'none'; } - // Reset path and load files - app.currentPath = ''; - ui.updateBreadcrumb(''); + // Use user's home folder instead of root path + if (app.userHomeFolderId) { + app.currentPath = app.userHomeFolderId; + ui.updateBreadcrumb(app.userHomeFolderName || 'Home'); + } else { + // If no home folder is set, this will trigger finding it in loadFiles() + app.currentPath = ''; + } loadFiles(); } @@ -1262,17 +1312,26 @@ async function findUserHomeFolder(username) { console.log(`Found ${folderList.length} folders at root`); // Look for a folder with a name pattern that matches the user's home folder - // Typically named "Mi Carpeta - username" + // Only exact match "Mi Carpeta - username" const homeFolderPattern = `Mi Carpeta - ${username}`; - let homeFolder = folderList.find(folder => folder.name === homeFolderPattern); - // If exact match not found, try a more flexible match - if (!homeFolder) { - homeFolder = folderList.find(folder => - folder.name.toLowerCase().includes(username.toLowerCase()) || - folder.name.startsWith('Mi Carpeta -') - ); - } + // Filter first to remove system folders like .trash that shouldn't be visible + const visibleFolders = folderList.filter(folder => { + // Skip system folders (starting with dot) + if (folder.name.startsWith('.')) { + return false; + } + + // Skip other users' folders + if (folder.name.startsWith('Mi Carpeta - ') && !folder.name.includes(username)) { + return false; + } + + return true; + }); + + // Find the user's home folder from filtered list + let homeFolder = visibleFolders.find(folder => folder.name === homeFolderPattern); if (homeFolder) { console.log(`Found user's home folder: ${homeFolder.name} (${homeFolder.id})`); diff --git a/static/js/favorites.js b/static/js/favorites.js index 0478baf3..95e90fe6 100644 --- a/static/js/favorites.js +++ b/static/js/favorites.js @@ -27,18 +27,32 @@ const favorites = { */ async checkBackendAvailability() { try { + // Add error handling to prevent console errors by catching 500 errors + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 3000); // 3s timeout + const response = await fetch('/api/favorites', { method: 'GET', headers: { 'Authorization': `Bearer ${localStorage.getItem('oxicloud_token')}` - } + }, + signal: controller.signal + }).catch(err => { + console.warn('Network error checking favorites API:', err); + return { ok: false, status: 0 }; }); - this.backendApiAvailable = response.ok; - console.log(`Backend favorites API ${this.backendApiAvailable ? 'is' : 'is not'} available`); + clearTimeout(timeoutId); - // If backend API is available, sync local favorites with server - if (this.backendApiAvailable) { + // Check if the response indicates the API is properly implemented + this.backendApiAvailable = response.ok; + + if (!response.ok) { + console.log(`Backend favorites API returned status ${response.status} - using local storage fallback`); + this.backendApiAvailable = false; + } else { + console.log('Backend favorites API is available'); + // If backend API is available, sync local favorites with server this.syncWithServer(); } } catch (error) { diff --git a/static/js/ui.js b/static/js/ui.js index 49f58056..0a00a609 100644 --- a/static/js/ui.js +++ b/static/js/ui.js @@ -362,12 +362,17 @@ const ui = { return window.i18n.t(key); }; + // First determine if the current view is the user's home folder + const isUserHomeFolder = username && window.app.userHomeFolderName && + window.app.userHomeFolderName.includes(username) && + folderName === window.app.userHomeFolderName; + // Set appropriate text for home item - if (username && folderName && folderName.includes(username)) { + if (isUserHomeFolder) { // If the current folder is the user's home folder, label it as "Home" homeItem.textContent = getTranslatedText('breadcrumb.home', 'Home'); } else if (folderName && folderName.startsWith('Mi Carpeta')) { - // If the current folder is another user's home folder or a special folder, use its name + // If viewing a root folder but not the user's home folder, use its full name homeItem.textContent = folderName; } else { // Default - use "Home" label