From 4c4c3a64ad2b37b31da2523ace134b27c5333da7 Mon Sep 17 00:00:00 2001 From: Cozy Pierre Date: Thu, 4 Aug 2022 17:49:00 +0200 Subject: [PATCH] feat(search): improve search by querying fewer data Selector is mandatory when using .select or .partialIndex https://github.com/cozy/cozy-client/issues/1216 --- CHANGELOG.md | 3 +- .../components/SuggestionProvider.jsx | 38 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c58896c76c..156d7ad454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## ✨ Features +* Improve speed of search suggestion by querying fewer data * Update cozy-stack-client and cozy-pouch-link to sync with cozy-client version * Update cozy-ui - Modify Viewers to handle [68.0.0 BC](https://github.com/cozy/cozy-ui/releases/tag/v68.0.0) @@ -12,7 +13,7 @@ * Improve cozy-bar implementation to fix UI bugs in Amirale * Fix navigation through mobile Flagship on Note creation and opening -* Remove unused contacts permissions on Photos +* Remove unused contacts permissions on Photos ## 🔧 Tech diff --git a/src/drive/web/modules/services/components/SuggestionProvider.jsx b/src/drive/web/modules/services/components/SuggestionProvider.jsx index abfccb3873..e8285d7232 100644 --- a/src/drive/web/modules/services/components/SuggestionProvider.jsx +++ b/src/drive/web/modules/services/components/SuggestionProvider.jsx @@ -1,10 +1,12 @@ /* global cozy */ import React from 'react' import FuzzyPathSearch from '../FuzzyPathSearch' -import { withClient } from 'cozy-client' +import { withClient, Q } from 'cozy-client' +import { DOCTYPE_FILES } from 'drive/lib/doctypes' import { TYPE_DIRECTORY, makeNormalizedFile } from './helpers' import { getIconUrl } from './iconContext' +import { TRASH_DIR_ID } from 'drive/constants/config' class SuggestionProvider extends React.Component { componentDidMount() { @@ -46,21 +48,37 @@ class SuggestionProvider extends React.Component { ) } - // fetches pretty much all the files and preloads FuzzyPathSearch + // fetches pretty much all the files not trashed and preloads FuzzyPathSearch async indexFiles() { const { client } = this.props // TODO: fix me // eslint-disable-next-line no-async-promise-executor return new Promise(async resolve => { - const resp = await cozy.client.fetchJSON( - 'GET', - `/data/io.cozy.files/_all_docs?include_docs=true` + const files = await client.queryAll( + Q(DOCTYPE_FILES) + .partialIndex({ + _id: { + $ne: TRASH_DIR_ID + }, + trashed: { + $or: [ + { + $exists: false + }, + { + $eq: false + } + ] + } + }) + .select(['_id', 'trashed', 'dir_id', 'name', 'path']) + .where({ + _id: { + $gt: null + } + }) + .limitBy(1000) ) - const files = resp.rows - // TODO: fix me - // eslint-disable-next-line no-prototype-builtins - .filter(row => !row.doc.hasOwnProperty('views')) - .map(row => ({ id: row.id, ...row.doc })) const folders = files.filter(file => file.type === TYPE_DIRECTORY)