From 14db0dd105d3db5051132b51ab489ba3fed632a4 Mon Sep 17 00:00:00 2001 From: Renan Mendes Carvalho Date: Fri, 22 Sep 2023 09:51:14 +0200 Subject: [PATCH] feature(plugins/home): Improves title resolution Signed-off-by: Renan Mendes Carvalho --- plugins/home/src/components/VisitListener.test.tsx | 7 ++----- plugins/home/src/components/VisitListener.tsx | 12 +++++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/plugins/home/src/components/VisitListener.test.tsx b/plugins/home/src/components/VisitListener.test.tsx index 7a0ce57de5..72c530071a 100644 --- a/plugins/home/src/components/VisitListener.test.tsx +++ b/plugins/home/src/components/VisitListener.test.tsx @@ -53,7 +53,6 @@ describe('', () => { afterEach(jest.resetAllMocks); it('registers a visit', async () => { - jest.spyOn(document, 'title', 'get').mockReturnValue('MockedTitle'); const pathname = '/catalog/default/component/playback-order'; await renderInTestApp( @@ -68,7 +67,7 @@ describe('', () => { visit: { pathname, entityRef: 'component:default/playback-order', - name: 'MockedTitle', + name: 'playback-order', }, }); }); @@ -86,7 +85,6 @@ describe('', () => { }); it('is able to override how visit names are defined', async () => { - jest.spyOn(document, 'title', 'get').mockReturnValue('MockedTitle'); const pathname = '/catalog/default/component/playback-order'; const visitNameOverride = ({ pathname: path }: { pathname: string }) => @@ -111,7 +109,6 @@ describe('', () => { }); it('is able to override how entityRefs are defined', async () => { - jest.spyOn(document, 'title', 'get').mockReturnValue('MockedTitle'); const pathname = '/catalog/default/component/playback-order'; const toEntityRefOverride = ({ pathname: path }: { pathname: string }) => @@ -129,7 +126,7 @@ describe('', () => { visit: { pathname, entityRef: pathname, - name: 'MockedTitle', + name: 'playback-order', }, }), ); diff --git a/plugins/home/src/components/VisitListener.tsx b/plugins/home/src/components/VisitListener.tsx index 4cddd307c5..de7d16e44f 100644 --- a/plugins/home/src/components/VisitListener.tsx +++ b/plugins/home/src/components/VisitListener.tsx @@ -54,17 +54,23 @@ const getToEntityRef = /** * @public * This function returns an implementation of visitName which is responsible - * for receiving a pathname and returning a string (name). The default - * implementation ignores the pathname and uses the document.title . + * for receiving a pathname and returning a string (name). */ export const getVisitName = ({ rootPath = 'catalog', document = global.document } = {}) => ({ pathname }: { pathname: string }) => { + // If it is a catalog entity, get the name from the path const regex = new RegExp( `^\/${rootPath}\/(?[^\/]+)\/(?[^\/]+)\/(?[^\/]+)`, ); - const result = regex.exec(pathname); + let result = regex.exec(pathname); if (result && result?.groups) return result.groups.name; + + // If it is a root pathname, get the name from there + result = /^\/(?[^\/]+)$/.exec(pathname); + if (result && result?.groups) return result.groups.name; + + // Fallback to document title return document.title; };