Skip to content

Commit

Permalink
Build local search index DB from chrome service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed May 22, 2024
1 parent e01656f commit a87aee9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"@patternfly/react-core": "^5.3.0",
"@patternfly/react-icons": "^5.3.0",
"@patternfly/react-tokens": "^5.3.0",
"@orama/orama": "^2.0.3",
"@redhat-cloud-services/frontend-components": "^4.2.2",
"@redhat-cloud-services/chrome": "^1.0.9",
"@redhat-cloud-services/entitlements-client": "1.2.0",
Expand Down
93 changes: 93 additions & 0 deletions src/state/atoms/localSearchAtom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { atom } from 'jotai';
import { Orama, create, insert } from '@orama/orama';

import { getChromeStaticPathname } from '../../utils/common';
import axios from 'axios';

type IndexEntry = {
icon?: string;
title: string[];
bundle: string[];
bundleTitle: string[];
id: string;
uri: string;
relative_uri: string;
poc_description_t: string;
alt_title: string[];
};

type SearchEntry = {
title: string;
uri: string;
pathname: string;
description: string;
icon?: string;
id: string;
bundleTitle: string;
altTitle?: string[];
};

const asyncSearchIndexAtom = atom(async () => {
const staticPath = getChromeStaticPathname('search');
const { data: rawIndex } = await axios.get<IndexEntry[]>(`${staticPath}/search-index.json`);
const searchIndex: SearchEntry[] = [];
const idSet = new Set<string>();
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);
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;
});

const entrySchema = {
title: 'string',
description: 'string',
altTitle: 'string[]',
descriptionMatch: 'string',
bundleTitle: 'string',
pathname: 'string',
} as const;

async function insertEntry(db: Orama<typeof entrySchema>, entry: SearchEntry) {
return insert(db, {
id: entry.id,
title: entry.title,
description: entry.description,
descriptionMatch: entry.description,
altTitle: entry.altTitle ?? [],
bundleTitle: entry.bundleTitle,
pathname: entry.pathname,
});
}

export const asyncLocalOrama = atom(async (get) => {
const db: Orama<typeof entrySchema> = await create({
schema: entrySchema,
});

const insertCommands = (await get(asyncSearchIndexAtom)).map(async (entry) => {
return insertEntry(db, entry);
});

await Promise.all(insertCommands);
return db;
});
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export const chromeServiceStaticPathname = {
},
};

export function getChromeStaticPathname(type: 'modules' | 'navigation' | 'services') {
export function getChromeStaticPathname(type: 'modules' | 'navigation' | 'services' | 'search') {
const stableEnv = isBeta() ? 'beta' : 'stable';
const prodEnv = isProd() ? 'prod' : ITLess() ? 'itless' : 'stage';
return `${CHROME_SERVICE_BASE}${chromeServiceStaticPathname[stableEnv][prodEnv]}/${type}`;
Expand Down

0 comments on commit a87aee9

Please sign in to comment.