From cbea8ffbb268b16433594b424e31b9c87617a00a Mon Sep 17 00:00:00 2001 From: Martin Marosi Date: Wed, 20 Nov 2024 10:59:05 +0100 Subject: [PATCH] Use generated search index --- src/state/atoms/localSearchAtom.ts | 81 ++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/src/state/atoms/localSearchAtom.ts b/src/state/atoms/localSearchAtom.ts index 7e89c5669..86457f0d0 100644 --- a/src/state/atoms/localSearchAtom.ts +++ b/src/state/atoms/localSearchAtom.ts @@ -29,37 +29,74 @@ type SearchEntry = { altTitle?: string[]; }; +const GENERATED_SEARCH_FLAG = '@chrome:generated-search-index'; +type GeneratedSearchIndexResponse = { + alt_title?: string[]; + id: string; + href: string; + title: string; + description?: string; +}; + export const SearchPermissions = new Map(); export const SearchPermissionsCache = new Map(); const asyncSearchIndexAtom = atom(async () => { const staticPath = getChromeStaticPathname('search'); - const { data: rawIndex } = await axios.get(`${staticPath}/search-index.json`); const searchIndex: SearchEntry[] = []; const idSet = new Set(); - rawIndex.forEach((entry) => { - if (idSet.has(entry.id)) { - console.warn('Duplicate id found in index', entry.id); - return; - } + if (localStorage.getItem(GENERATED_SEARCH_FLAG) === 'true') { + // parse data from generated search index + const { data: rawIndex } = await axios.get(`/api/chrome-service/v1/static/search-index-generated.json`); + rawIndex.forEach((entry) => { + if (idSet.has(entry.id)) { + console.warn('Duplicate id found in index', entry.id); + return; + } - if (!entry.relative_uri.startsWith('/')) { - console.warn('External ink found in the index. Ignoring: ', entry.relative_uri); - return; - } - idSet.add(entry.id); - SearchPermissions.set(entry.id, entry.permissions ?? []); - searchIndex.push({ - title: entry.title[0], - uri: entry.uri, - pathname: entry.relative_uri, - description: entry.poc_description_t || entry.relative_uri, - icon: entry.icon, - id: entry.id, - bundleTitle: entry.bundleTitle[0], - altTitle: entry.alt_title, + if (!entry.href.startsWith('/')) { + console.warn('External ink found in the index. Ignoring: ', entry.href); + return; + } + idSet.add(entry.id); + SearchPermissions.set(entry.id, []); + searchIndex.push({ + title: entry.title, + uri: entry.href, + pathname: entry.href, + description: entry.description ?? entry.href, + icon: undefined, + id: entry.id, + bundleTitle: entry.title, + altTitle: entry.alt_title, + }); }); - }); + } else { + const { data: rawIndex } = await axios.get(`${staticPath}/search-index.json`); + rawIndex.forEach((entry) => { + if (idSet.has(entry.id)) { + console.warn('Duplicate id found in index', entry.id); + return; + } + + if (!entry.relative_uri.startsWith('/')) { + console.warn('External ink found in the index. Ignoring: ', entry.relative_uri); + return; + } + idSet.add(entry.id); + SearchPermissions.set(entry.id, entry.permissions ?? []); + searchIndex.push({ + title: entry.title[0], + uri: entry.uri, + pathname: entry.relative_uri, + description: entry.poc_description_t || entry.relative_uri, + icon: entry.icon, + id: entry.id, + bundleTitle: entry.bundleTitle[0], + altTitle: entry.alt_title, + }); + }); + } return searchIndex; });