Files
Oxicloud/static/js/features/library/recent.js
T

82 lines
2.4 KiB
JavaScript
Raw Normal View History

2026-05-28 11:50:32 +02:00
// @ts-check
2025-04-02 05:08:30 +02:00
/**
* OxiCloud - Recent Files Module (server-authoritative)
*
2026-05-28 11:50:32 +02:00
* Records file-access events via POST /api/recent/{type}/{id} and exposes
* `clearRecentFiles()` for the clear-all action.
*
* Display is now handled by `recentView.js` using the cursor-paginated
* `GET /api/recent/resources` endpoint.
2025-04-02 05:08:30 +02:00
*/
import { getCsrfHeaders } from '../../core/csrf.js';
2026-05-28 11:50:32 +02:00
/** @import {ItemTypeEnum} from '../../core/types.js' */
2026-05-07 23:40:02 +02:00
2025-04-02 05:08:30 +02:00
const recent = {
// ───────────────────── helpers ─────────────────────
_authHeaders() {
return { ...getCsrfHeaders() };
},
// ───────────────────── lifecycle ─────────────────────
2025-04-02 05:08:30 +02:00
/**
2026-05-28 11:50:32 +02:00
* Initialise the module. Called once from app.js on startup.
2025-04-02 05:08:30 +02:00
*/
init() {
this.setupEventListeners();
},
2025-04-02 05:08:30 +02:00
/**
* Listen for file-accessed events dispatched by ui.js and forward
* them to the backend.
2025-04-02 05:08:30 +02:00
*/
setupEventListeners() {
document.addEventListener('file-accessed', (event) => {
2026-05-07 23:40:02 +02:00
const e = /** @type {CustomEvent} */ (event);
if (e.detail?.file) {
const file = e.detail.file;
const itemType = file.item_type || 'file';
this._recordAccess(file.id, itemType);
2025-04-02 05:08:30 +02:00
}
});
},
2025-04-02 05:08:30 +02:00
/**
* Record an access event on the server.
2026-05-07 23:40:02 +02:00
* @param {string} itemId
* @param {ItemTypeEnum} itemType
2025-04-02 05:08:30 +02:00
*/
async _recordAccess(itemId, itemType) {
try {
await fetch(`/api/recent/${itemType}/${itemId}`, {
method: 'POST',
headers: this._authHeaders()
});
} catch (err) {
console.warn('Failed to record recent access:', err);
2025-04-02 05:08:30 +02:00
}
},
// ───────────────────── public API ─────────────────────
2025-04-02 05:08:30 +02:00
/**
* Clear all recent items (delegates to the server).
2025-04-02 05:08:30 +02:00
*/
async clearRecentFiles() {
2025-04-02 05:08:30 +02:00
try {
await fetch('/api/recent/clear', {
method: 'DELETE',
headers: this._authHeaders()
});
} catch (err) {
console.error('Error clearing recent files:', err);
2025-04-02 05:08:30 +02:00
}
}
};
export { recent };