2026-02-20 12:27:52 +01:00
|
|
|
/**
|
|
|
|
|
* Search view orchestration logic
|
|
|
|
|
*/
|
|
|
|
|
|
2026-06-15 00:17:17 +02:00
|
|
|
import { i18n } from '../core/i18n.js';
|
2026-04-13 15:09:10 +02:00
|
|
|
import { search } from '../features/files/search.js';
|
|
|
|
|
import { resolveHomeFolder } from './authSession.js';
|
|
|
|
|
import { app } from './state.js';
|
|
|
|
|
import { ui } from './ui.js';
|
2026-02-20 12:27:52 +01:00
|
|
|
|
2026-05-07 23:40:02 +02:00
|
|
|
/**
|
|
|
|
|
* @import {SearchCriteria, SortByEnnum} from '../core/types.js'
|
|
|
|
|
*/
|
|
|
|
|
|
2026-04-13 15:09:10 +02:00
|
|
|
/**
|
|
|
|
|
* @param {string} query
|
2026-05-07 23:40:02 +02:00
|
|
|
* @param {SortByEnnum} [sortBy]
|
2026-04-13 15:09:10 +02:00
|
|
|
*/
|
2026-05-07 23:40:02 +02:00
|
|
|
// FIXME: refactor with search.js ?
|
2026-04-13 15:09:10 +02:00
|
|
|
async function performSearch(query, sortBy) {
|
2026-02-20 12:27:52 +01:00
|
|
|
console.log(`Performing search for: "${query}" (sort: ${sortBy || 'relevance'})`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
app.isSearchMode = true;
|
2026-04-30 02:02:27 +02:00
|
|
|
ui.updateBreadcrumb();
|
2026-02-20 12:27:52 +01:00
|
|
|
|
2026-04-13 15:09:10 +02:00
|
|
|
ui.showError(`<h3><i class="fas fa-spinner fa-spin search-spinner"></i> Searching for "${query}"...</h3>`);
|
2026-02-20 12:27:52 +01:00
|
|
|
|
2026-05-07 23:40:02 +02:00
|
|
|
/** @type {SearchCriteria} */
|
2026-02-20 12:27:52 +01:00
|
|
|
const options = {
|
|
|
|
|
recursive: true,
|
|
|
|
|
limit: 100,
|
2026-05-07 23:40:02 +02:00
|
|
|
offset: 0,
|
2026-02-20 12:27:52 +01:00
|
|
|
sort_by: sortBy || 'relevance'
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-15 22:18:42 +02:00
|
|
|
if (app.currentSection !== 'trash') {
|
2026-03-17 14:19:09 +08:00
|
|
|
// Ensure we have a valid folder_id before searching
|
|
|
|
|
if (!app.currentPath || app.currentPath === '') {
|
2026-04-13 15:09:10 +02:00
|
|
|
await resolveHomeFolder();
|
2026-03-17 14:19:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only set folder_id if we have a valid value
|
|
|
|
|
if (app.currentPath && app.currentPath !== '') {
|
2026-02-20 12:27:52 +01:00
|
|
|
options.folder_id = app.currentPath;
|
|
|
|
|
}
|
2026-03-17 14:19:09 +08:00
|
|
|
// If still no valid folder_id, search will be global (without folder_id)
|
2026-02-20 12:27:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:09:10 +02:00
|
|
|
const searchResults = await search.searchFiles(query, options);
|
|
|
|
|
search.displaySearchResults(searchResults);
|
2026-02-20 12:27:52 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Search error:', error);
|
2026-06-15 00:17:17 +02:00
|
|
|
ui.showNotification(i18n.t('notif.errorTitle'), i18n.t('notif.searchError'));
|
2026-02-20 12:27:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2026-02-20 12:27:52 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-13 15:09:10 +02:00
|
|
|
export { performSearch };
|