From c228b1917389203cc80832d19bccace94ae8b926 Mon Sep 17 00:00:00 2001 From: Martin Marosi Date: Mon, 16 Dec 2024 11:16:02 +0100 Subject: [PATCH] feat(feo): add search index interceptor --- navnotes.md | 6 ++ packages/config-utils/src/feo/feo-types.ts | 11 ++++ .../src/feo/search-interceptor.test.ts | 58 +++++++++++++++++++ .../src/feo/search-interceptor.ts | 9 +++ 4 files changed, 84 insertions(+) create mode 100644 packages/config-utils/src/feo/search-interceptor.test.ts create mode 100644 packages/config-utils/src/feo/search-interceptor.ts diff --git a/navnotes.md b/navnotes.md index 407a89a71..6638a86cb 100644 --- a/navnotes.md +++ b/navnotes.md @@ -28,3 +28,9 @@ Same as `bundleSegmentRef`, but for global segments. ### frontendRef Required to match nav item in bundle to current app + +# Search interceptor notes + +## frontendRef + +search entries need a `frontendRef` attribute. Without the attribute, we can modify/add frontend entries, but we can't remove them diff --git a/packages/config-utils/src/feo/feo-types.ts b/packages/config-utils/src/feo/feo-types.ts index c1efcb34c..543c5d661 100644 --- a/packages/config-utils/src/feo/feo-types.ts +++ b/packages/config-utils/src/feo/feo-types.ts @@ -54,6 +54,16 @@ export type ChromeModuleRegistry = { [moduleName: string]: ChromeModule; }; +export type ChromeStaticSearchEntry = { + frontendRef: string; + id: string; + href: string; + title: string; + description: string; + alt_title?: string[]; + isExternal?: boolean; +}; + export type SegmentRef = { segmentId: string; frontendName: string; @@ -95,6 +105,7 @@ export type CRDObject = { bundleSegments?: BundleSegment[]; navigationSegments?: DirectNavItem[]; module: ChromeModule; + searchEntries?: ChromeStaticSearchEntry[]; }; }; diff --git a/packages/config-utils/src/feo/search-interceptor.test.ts b/packages/config-utils/src/feo/search-interceptor.test.ts new file mode 100644 index 000000000..fb69a5d46 --- /dev/null +++ b/packages/config-utils/src/feo/search-interceptor.test.ts @@ -0,0 +1,58 @@ +import { ChromeStaticSearchEntry, FrontendCRD } from './feo-types'; +import searchInterceptor from './search-interceptor'; + +describe('SearchInterceptor', () => { + it('should replace search entries with the ones from the frontendCRD', () => { + const frontendName = 'frontendName'; + const frontendCRD: FrontendCRD = { + objects: [ + { + metadata: { + name: frontendName, + }, + spec: { + module: { + manifestLocation: 'location', + }, + searchEntries: [ + { + frontendRef: frontendName, + id: 'id-1', + href: 'href-1', + title: 'title-1', + description: 'description-1', + }, + { + frontendRef: frontendName, + id: 'id-1', + href: 'href-1', + title: 'title-1', + description: 'description-1', + }, + ], + }, + }, + ], + }; + const remoteSearchEntries: ChromeStaticSearchEntry[] = [ + { + frontendRef: 'otherFrontend', + id: 'otherFrontend', + href: 'otherFrontend', + title: 'otherFrontend', + description: 'otherFrontend', + }, + { + frontendRef: frontendName, + id: frontendName, + href: frontendName, + title: frontendName, + description: frontendName, + }, + ]; + + const expectedSearchEntries: ChromeStaticSearchEntry[] = [remoteSearchEntries[0], ...(frontendCRD.objects[0].spec.searchEntries ?? [])]; + const result = searchInterceptor(remoteSearchEntries, frontendCRD); + expect(result).toEqual(expectedSearchEntries); + }); +}); diff --git a/packages/config-utils/src/feo/search-interceptor.ts b/packages/config-utils/src/feo/search-interceptor.ts new file mode 100644 index 000000000..fd7b4a774 --- /dev/null +++ b/packages/config-utils/src/feo/search-interceptor.ts @@ -0,0 +1,9 @@ +import { ChromeStaticSearchEntry, FrontendCRD } from './feo-types'; + +function searchInterceptor(staticSearchIndex: ChromeStaticSearchEntry[], frontendCRD: FrontendCRD): ChromeStaticSearchEntry[] { + const frontendRef = frontendCRD.objects[0].metadata.name; + const result = staticSearchIndex.filter((entry) => entry.frontendRef !== frontendRef); + return [...result, ...(frontendCRD.objects[0].spec.searchEntries ?? [])]; +} + +export default searchInterceptor;