Cap tag suggestions at 500

This commit is contained in:
Henrik Giesel 2021-09-10 01:09:07 +02:00
parent 1fd7fe4391
commit e85c93f3e7
4 changed files with 12 additions and 8 deletions

View file

@ -63,6 +63,7 @@ message FindAndReplaceTagRequest {
message CompleteTagRequest {
// a partial tag, optionally delimited with ::
string input = 1;
uint32 amount = 2;
}
message CompleteTagResponse {

View file

@ -91,7 +91,7 @@ impl TagsService for Backend {
fn complete_tag(&self, input: pb::CompleteTagRequest) -> Result<pb::CompleteTagResponse> {
self.with_col(|col| {
let tags = col.complete_tag(&input.input)?;
let tags = col.complete_tag(&input.input, input.amount)?;
Ok(pb::CompleteTagResponse { tags })
})
}

View file

@ -2,18 +2,19 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use regex::Regex;
use std::convert::TryInto;
use crate::prelude::*;
impl Collection {
pub fn complete_tag(&self, input: &str) -> Result<Vec<String>> {
pub fn complete_tag(&self, input: &str, amount: u32) -> Result<Vec<String>> {
let filters: Vec<_> = input
.split("::")
.map(component_to_regex)
.collect::<Result<_, _>>()?;
let mut tags = vec![];
self.storage.get_tags_by_predicate(|tag| {
if filters_match(&filters, tag) {
if tags.len() <= amount.try_into().unwrap() && filters_match(&filters, tag) {
tags.push(tag.to_string());
}
// we only need the tag name

View file

@ -58,7 +58,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const data = await postRequest(
"/_anki/completeTag",
Tags.CompleteTagRequest.encode(
Tags.CompleteTagRequest.create({ input: replaceWithColons(input) })
Tags.CompleteTagRequest.create({ input, amount: 500 })
).finish()
);
const response = Tags.CompleteTagResponse.decode(data);
@ -72,10 +72,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
autocompleteDisabled = activeName.length === 0;
suggestionsPromise = autocompleteDisabled
? noSuggestions
: fetchSuggestions(activeTag.name).then((names: string[]): string[] => {
autocompleteDisabled = names.length === 0;
return names.map(replaceWithUnicodeSeparator);
});
: fetchSuggestions(replaceWithColons(activeTag.name)).then(
(names: string[]): string[] => {
autocompleteDisabled = names.length === 0;
return names.map(replaceWithUnicodeSeparator);
}
);
}
function onAutocomplete(selected: string): void {