This commit is contained in:
seb
2026-08-05 07:07:18 +02:00
parent 406684f4a2
commit b720c6ee25

View File

@@ -2,10 +2,9 @@ import { createContext, useContext, useRef } from 'react';
export const StorageManagerContext = createContext(null); export const StorageManagerContext = createContext(null);
function createStorageManager() { function loadArticleIndex() {
/** Fetch /articleIndex on construction and convert to the row shape the search page renders. */ /** Fetch /articleIndex and convert to the row shape the search page renders. */
const articleIndexPromise = fetch('/api/articleIndex') return fetch('/api/articleIndex').then(async (res) => {
.then(async (res) => {
const data = await res.json(); const data = await res.json();
const articles = Array.isArray(data) ? data : (data?.articles ?? []); const articles = Array.isArray(data) ? data : (data?.articles ?? []);
console.log('[storageMgr] GET /articleIndex', { console.log('[storageMgr] GET /articleIndex', {
@@ -23,12 +22,30 @@ function createStorageManager() {
eans: a.eans ?? [], eans: a.eans ?? [],
categories: Array.isArray(a.categoryIds) ? a.categoryIds.join(',') : null, categories: Array.isArray(a.categoryIds) ? a.categoryIds.join(',') : null,
})); }));
}) });
.catch((err) => { }
function createStorageManager() {
let articleIndexPromise = loadArticleIndex().catch((err) => {
console.error('[storageMgr] GET /articleIndex failed', err); console.error('[storageMgr] GET /articleIndex failed', err);
return []; return [];
}); });
/**
* Re-fetch /articleIndex. Searches keep using the old cache until the new
* load resolves; only then is the cache replaced. On failure the old cache stays.
*/
async function update() {
try {
const articles = await loadArticleIndex();
articleIndexPromise = Promise.resolve(articles);
return articles;
} catch (err) {
console.error('[storageMgr] update /articleIndex failed', err);
throw err;
}
}
async function doSearch({ target, filter, offset, maxCount, onUpdate }) { async function doSearch({ target, filter, offset, maxCount, onUpdate }) {
const articles = await articleIndexPromise; const articles = await articleIndexPromise;
const query = target.trim().toLowerCase(); const query = target.trim().toLowerCase();
@@ -48,8 +65,11 @@ function createStorageManager() {
return { return {
doSearch, doSearch,
// Expose so callers can await if they want the loaded index. update,
articleIndexPromise, // Always the current cache promise (including after update()).
get articleIndexPromise() {
return articleIndexPromise;
},
}; };
} }