Skip to content

Commit

Permalink
fix: Remove file name from path after search
Browse files Browse the repository at this point in the history
With #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.
  • Loading branch information
paultranvan committed Nov 13, 2024
1 parent 6209323 commit 431c37b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import CozyClient from 'cozy-client'
import { IOCozyFile } from 'cozy-client/types/types'
import { IOCozyContact, IOCozyFile } from 'cozy-client/types/types'

import {
addFilePaths,
computeFileFullpath,
normalizeFileWithFolders,
shouldKeepFile
} from './normalizeFile'
import { cleanFilePath } from './normalizeSearchResult'
import { FILES_DOCTYPE } from '../consts'
import { queryDocById } from '../queries'
import { CozyDoc } from '../types'

Expand Down Expand Up @@ -293,3 +295,35 @@ 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 = { _type: FILES_DOCTYPE, name: 'name' } as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})

it('should remove name from path if path ends with name', () => {
const doc = {
_type: FILES_DOCTYPE,
path: '/the/path/myname',
name: 'myname'
} 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 = {
_type: FILES_DOCTYPE,
path: '/the/path/othername',
name: 'name'
} as IOCozyFile
expect(cleanFilePath(doc)).toEqual(doc)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = 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
Expand Down

0 comments on commit 431c37b

Please sign in to comment.