diff --git a/static/js/app/main.js b/static/js/app/main.js index d7dba76d..03e6dbcf 100644 --- a/static/js/app/main.js +++ b/static/js/app/main.js @@ -15,14 +15,14 @@ import { sharedView } from '../views/shared/sharedView.js'; import { checkAuthentication } from './authSession.js'; import { loadFiles } from './filesView.js'; import { + SECTIONS_MAPPER, switchToFavoritesSection, switchToFilesSection, switchToMusicSection, switchToPhotosSection, switchToRecentFilesSection, switchToSharedSection, - switchToTrashSection, - VIEW_FLAGS + switchToTrashSection } from './navigation.js'; import { performSearch } from './searchView.js'; import { app, appElements as elements } from './state.js'; @@ -247,7 +247,7 @@ function deserializeHash() { const section = hash_elements[1]; - if (section in VIEW_FLAGS) { + if (section in SECTIONS_MAPPER) { hashContext.section = section; } @@ -306,40 +306,13 @@ function switchSectionTo(section) { // no change ... return; - //TODO: better to use a registry for the future (easier to add new section) - switch (section) { - case '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(); + if ((!section) in SECTIONS_MAPPER) { + console.warn(`context view ${section} unkonwn fallback to files section`); + section = 'files'; } + + const switchHandler = SECTIONS_MAPPER[section]; + switchHandler(); } /** @@ -528,8 +501,8 @@ function setupEventListeners() { if (searchDebounceTimer) clearTimeout(searchDebounceTimer); const query = elements.searchInput.value.trim(); - // In shared view, filter locally - if (app.isSharedView && sharedView) { + // In shared section, filter locally + if (app.currentSection === 'shared' && sharedView) { sharedView.filterAndSortItems(); return; } diff --git a/static/js/app/navigation.js b/static/js/app/navigation.js index 749babd7..00b3c066 100644 --- a/static/js/app/navigation.js +++ b/static/js/app/navigation.js @@ -107,17 +107,6 @@ function initSidebarToggle() { // Initialize sidebar toggle when DOM is ready 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. * @param {HTMLElement} navItem - The nav item element @@ -128,6 +117,17 @@ function getSectionFromNavItem(navItem) { 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. * @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; // 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; }); diff --git a/static/js/app/searchView.js b/static/js/app/searchView.js index 2ace946f..4c7ef06b 100644 --- a/static/js/app/searchView.js +++ b/static/js/app/searchView.js @@ -26,7 +26,7 @@ async function performSearch(query, sortBy) { sort_by: sortBy || 'relevance' }; - if (!app.isTrashView) { + if (app.currentSection !== 'trash') { // Ensure we have a valid folder_id before searching if (!app.currentPath || app.currentPath === '') { await resolveHomeFolder(); diff --git a/static/js/app/state.js b/static/js/app/state.js index 9397fcdc..0fd2575a 100644 --- a/static/js/app/state.js +++ b/static/js/app/state.js @@ -12,13 +12,8 @@ export const app = { contextMenuTargetFile: null, selectedTargetFolderId: '', moveDialogMode: 'file', - isFilesView: true, - isTrashView: false, - isSharedView: false, - isFavoritesView: false, - isRecentView: false, - isPhotosView: false, - currentSection: 'files', + + currentSection: null, // will be defined on first call isSearchMode: false, shareDialogItem: null, shareDialogItemType: null,