Files
Oxicloud/static/js/app/searchView.js
T

68 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* Search view orchestration logic
*/
import { i18n } from '../core/i18n.js';
import { search } from '../features/files/search.js';
import { resolveHomeFolder } from './authSession.js';
import { app } from './state.js';
import { ui } from './ui.js';
2026-05-07 23:40:02 +02:00
/**
* @import {SearchCriteria, SortByEnnum} from '../core/types.js'
*/
/**
* @param {string} query
2026-05-07 23:40:02 +02:00
* @param {SortByEnnum} [sortBy]
*/
2026-05-07 23:40:02 +02:00
// FIXME: refactor with search.js ?
async function performSearch(query, sortBy) {
console.log(`Performing search for: "${query}" (sort: ${sortBy || 'relevance'})`);
try {
app.isSearchMode = true;
2026-04-30 02:02:27 +02:00
ui.updateBreadcrumb();
ui.showError(`<h3><i class="fas fa-spinner fa-spin search-spinner"></i> Searching for "${query}"...</h3>`);
2026-05-07 23:40:02 +02:00
/** @type {SearchCriteria} */
const options = {
recursive: true,
limit: 100,
2026-05-07 23:40:02 +02:00
offset: 0,
sort_by: sortBy || 'relevance'
};
if (app.currentSection !== 'trash') {
// Ensure we have a valid folder_id before searching
if (!app.currentPath || app.currentPath === '') {
await resolveHomeFolder();
}
// Only set folder_id if we have a valid value
if (app.currentPath && app.currentPath !== '') {
options.folder_id = app.currentPath;
}
// If still no valid folder_id, search will be global (without folder_id)
}
const searchResults = await search.searchFiles(query, options);
search.displaySearchResults(searchResults);
} catch (error) {
console.error('Search error:', error);
ui.showNotification(i18n.t('notif.errorTitle'), i18n.t('notif.searchError'));
}
}
document.addEventListener('search-resort', (e) => {
2026-04-30 02:02:27 +02:00
const event = /** @type {CustomEvent<{sort_by: string}>} */ (e);
const searchInput = /** @type {HTMLInputElement} */ (document.querySelector('.search-container input'));
2026-04-07 22:50:42 +02:00
if (searchInput?.value.trim()) {
2026-05-07 23:40:02 +02:00
const sortBy = /** @type {SortByEnnum} */ (event.detail.sort_by);
performSearch(searchInput.value.trim(), sortBy);
}
});
export { performSearch };