From 365d2b92a14f7b93164bd18fa647ccab02999d0d Mon Sep 17 00:00:00 2001 From: Paul Tran-Van Date: Sat, 9 Nov 2024 12:11:16 +0100 Subject: [PATCH] fix: Remove file name from path after search With https://github.com/cozy/cozy-libs/pull/2612, we added the possibility to search on path + name by adding the name in the path, at indexing time. This is "heavy" from UX perspective, so let's remove it. --- .../src/search/helpers/normalizeFile.spec.ts | 27 ++++++++++++++++++- .../search/helpers/normalizeSearchResult.ts | 19 ++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeFile.spec.ts b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeFile.spec.ts index eeec615dc6..684513495f 100644 --- a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeFile.spec.ts +++ b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeFile.spec.ts @@ -1,5 +1,5 @@ import CozyClient from 'cozy-client' -import { IOCozyFile } from 'cozy-client/types/types' +import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types' import { addFilePaths, @@ -7,6 +7,7 @@ import { normalizeFileWithFolders, shouldKeepFile } from './normalizeFile' +import { cleanFilePath } from './normalizeSearchResult' import { queryDocById } from '../queries' import { CozyDoc } from '../types' @@ -293,3 +294,27 @@ describe('computeFileFullpath', () => { expect(res.path).toEqual('ROOT/MYDIR/file3') }) }) + +describe('cleanFilePath', () => { + it('should return the document unchanged if it is not an IOCozyFile', () => { + const doc = { fullname: 'name' } as IOCozyContact + expect(cleanFilePath(doc)).toEqual(doc) + }) + + it('should return the document unchanged if path is undefined', () => { + const doc = { name: 'name' } as IOCozyFile + expect(cleanFilePath(doc)).toEqual(doc) + }) + + it('should remove name from path if path ends with /name', () => { + const doc = { path: '/the/path/name', name: 'name' } as IOCozyFile + const expected = { ...doc, path: '/the/path' } + + expect(cleanFilePath(doc)).toEqual(expected) + }) + + it('should return the document unchanged if path does not end with /name', () => { + const doc = { path: '/the/path/othername', name: 'name' } as IOCozyFile + expect(cleanFilePath(doc)).toEqual(doc) + }) +}) diff --git a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts index abdfce2d55..b4f09d5390 100644 --- a/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts +++ b/packages/cozy-dataproxy-lib/src/search/helpers/normalizeSearchResult.ts @@ -16,7 +16,7 @@ export const normalizeSearchResult = ( searchResults: RawSearchResult, query: string ): SearchResult => { - const doc = searchResults.doc + const doc = cleanFilePath(searchResults.doc) const url = buildOpenURL(client, doc) const slug = getSearchResultSlug(doc) const title = getSearchResultTitle(doc) @@ -30,6 +30,23 @@ export const normalizeSearchResult = ( return normalizedRes } +export const cleanFilePath = (doc: CozyDoc): CozyDoc => { + if (!isIOCozyFile(doc)) { + return doc + } + const { path, name } = doc + if (!path) { + return doc + } + let newPath = doc.path + if (path.endsWith(`/${name}`)) { + // Remove the name from the path, which is added at indexing time to search on it + newPath = path.slice(0, -name.length - 1) + } + + return { ...doc, path: newPath } +} + const getSearchResultTitle = (doc: CozyDoc): string | null => { if (isIOCozyFile(doc)) { return doc.name