Skip to content

Commit

Permalink
feat(feo): add search index interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyperkid123 committed Dec 16, 2024
1 parent 0bf16ff commit c228b19
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions navnotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions packages/config-utils/src/feo/feo-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +105,7 @@ export type CRDObject = {
bundleSegments?: BundleSegment[];
navigationSegments?: DirectNavItem[];
module: ChromeModule;
searchEntries?: ChromeStaticSearchEntry[];
};
};

Expand Down
58 changes: 58 additions & 0 deletions packages/config-utils/src/feo/search-interceptor.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
9 changes: 9 additions & 0 deletions packages/config-utils/src/feo/search-interceptor.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c228b19

Please sign in to comment.