refactor(frontend/sections): remove app.is{Xxx}View and use only app.currentSection

This commit is contained in:
Edouard Vanbelle
2026-04-15 22:18:42 +02:00
parent b1827c9f3d
commit 7bb4ed0940
4 changed files with 26 additions and 58 deletions
+11 -38
View File
@@ -15,14 +15,14 @@ import { sharedView } from '../views/shared/sharedView.js';
import { checkAuthentication } from './authSession.js'; import { checkAuthentication } from './authSession.js';
import { loadFiles } from './filesView.js'; import { loadFiles } from './filesView.js';
import { import {
SECTIONS_MAPPER,
switchToFavoritesSection, switchToFavoritesSection,
switchToFilesSection, switchToFilesSection,
switchToMusicSection, switchToMusicSection,
switchToPhotosSection, switchToPhotosSection,
switchToRecentFilesSection, switchToRecentFilesSection,
switchToSharedSection, switchToSharedSection,
switchToTrashSection, switchToTrashSection
VIEW_FLAGS
} from './navigation.js'; } from './navigation.js';
import { performSearch } from './searchView.js'; import { performSearch } from './searchView.js';
import { app, appElements as elements } from './state.js'; import { app, appElements as elements } from './state.js';
@@ -247,7 +247,7 @@ function deserializeHash() {
const section = hash_elements[1]; const section = hash_elements[1];
if (section in VIEW_FLAGS) { if (section in SECTIONS_MAPPER) {
hashContext.section = section; hashContext.section = section;
} }
@@ -306,40 +306,13 @@ function switchSectionTo(section) {
// no change ... // no change ...
return; return;
//TODO: better to use a registry for the future (easier to add new section) if ((!section) in SECTIONS_MAPPER) {
switch (section) { console.warn(`context view ${section} unkonwn fallback to files section`);
case 'files': section = 'files';
switchToFilesSection();
break;
case 'shared':
switchToSharedSection();
break;
case 'recent':
switchToRecentFilesSection();
break;
case 'favorites':
switchToFavoritesSection();
break;
case 'photos':
switchToPhotosSection();
break;
case 'music':
switchToMusicSection();
break;
case 'trash':
switchToTrashSection();
break;
default:
console.warn(`context view ${section} unkonwn fallback to drive section`);
switchToFilesSection();
} }
const switchHandler = SECTIONS_MAPPER[section];
switchHandler();
} }
/** /**
@@ -528,8 +501,8 @@ function setupEventListeners() {
if (searchDebounceTimer) clearTimeout(searchDebounceTimer); if (searchDebounceTimer) clearTimeout(searchDebounceTimer);
const query = elements.searchInput.value.trim(); const query = elements.searchInput.value.trim();
// In shared view, filter locally // In shared section, filter locally
if (app.isSharedView && sharedView) { if (app.currentSection === 'shared' && sharedView) {
sharedView.filterAndSortItems(); sharedView.filterAndSortItems();
return; return;
} }
+12 -12
View File
@@ -107,17 +107,6 @@ function initSidebarToggle() {
// Initialize sidebar toggle when DOM is ready // Initialize sidebar toggle when DOM is ready
document.addEventListener('DOMContentLoaded', initSidebarToggle); document.addEventListener('DOMContentLoaded', initSidebarToggle);
// Mapping of section names to their corresponding view flags
export const VIEW_FLAGS = {
files: 'isFilesView',
shared: 'isSharedView',
recent: 'isRecentView',
favorites: 'isFavoritesView',
trash: 'isTrashView',
photos: 'isPhotosView',
music: 'isMusicView'
};
/** /**
* Derive section name from nav item's data-i18n attribute. * Derive section name from nav item's data-i18n attribute.
* @param {HTMLElement} navItem - The nav item element * @param {HTMLElement} navItem - The nav item element
@@ -128,6 +117,17 @@ function getSectionFromNavItem(navItem) {
return i18nKey ? i18nKey.replace('nav.', '') : null; return i18nKey ? i18nKey.replace('nav.', '') : null;
} }
// Mapping section name to associated switch functions
export const SECTIONS_MAPPER = {
files: switchToFilesSection,
shared: switchToSharedSection,
recent: switchToRecentFilesSection,
favorites: switchToFavoritesSection,
trash: switchToTrashSection,
photos: switchToPhotosSection,
music: switchToMusicSection
};
/** /**
* Set the current active section, updating all view flags and nav UI. * Set the current active section, updating all view flags and nav UI.
* @param {string} section - The section to activate ('files', 'shared', 'recent', 'favorites', 'trash') * @param {string} section - The section to activate ('files', 'shared', 'recent', 'favorites', 'trash')
@@ -137,7 +137,7 @@ function setCurrentSection(section) {
if (app.currentSection === section) return false; if (app.currentSection === section) return false;
// Set all view flags - true for active section, false for others // Set all view flags - true for active section, false for others
Object.entries(VIEW_FLAGS).forEach(([key, flag]) => { Object.entries(SECTIONS_MAPPER).forEach(([key, flag]) => {
app[flag] = key === section; app[flag] = key === section;
}); });
+1 -1
View File
@@ -26,7 +26,7 @@ async function performSearch(query, sortBy) {
sort_by: sortBy || 'relevance' sort_by: sortBy || 'relevance'
}; };
if (!app.isTrashView) { if (app.currentSection !== 'trash') {
// Ensure we have a valid folder_id before searching // Ensure we have a valid folder_id before searching
if (!app.currentPath || app.currentPath === '') { if (!app.currentPath || app.currentPath === '') {
await resolveHomeFolder(); await resolveHomeFolder();
+2 -7
View File
@@ -12,13 +12,8 @@ export const app = {
contextMenuTargetFile: null, contextMenuTargetFile: null,
selectedTargetFolderId: '', selectedTargetFolderId: '',
moveDialogMode: 'file', moveDialogMode: 'file',
isFilesView: true,
isTrashView: false, currentSection: null, // will be defined on first call
isSharedView: false,
isFavoritesView: false,
isRecentView: false,
isPhotosView: false,
currentSection: 'files',
isSearchMode: false, isSearchMode: false,
shareDialogItem: null, shareDialogItem: null,
shareDialogItemType: null, shareDialogItemType: null,