Fix group candidates fuzzy filter

This commit is contained in:
Cohee
2024-12-06 23:04:18 +02:00
parent 51f89aaf7b
commit b8a9a55246
3 changed files with 14 additions and 11 deletions

View File

@ -1836,7 +1836,7 @@ async function loadContextSettings() {
* Common function to perform fuzzy search with optional caching
* @param {string} type - Type of search from fuzzySearchCategories
* @param {any[]} data - Data array to search in
* @param {Array<{name: string, weight: number, getFn?: Function}>} keys - Fuse.js keys configuration
* @param {Array<{name: string, weight: number, getFn?: (obj: any) => string}>} keys - Fuse.js keys configuration
* @param {string} searchValue - The search term
* @param {Object.<string, { resultMap: Map<string, any> }>} [fuzzySearchCaches=null] - Optional fuzzy search caches
* @returns {import('fuse.js').FuseResult<any>[]} Results as items with their score
@ -1850,7 +1850,6 @@ function performFuzzySearch(type, data, keys, searchValue, fuzzySearchCaches = n
}
}
// @ts-ignore
const fuse = new Fuse(data, {
keys: keys,
includeScore: true,
@ -1924,7 +1923,7 @@ export function fuzzySearchPersonas(data, searchValue, fuzzySearchCaches = null)
const mappedData = data.map(x => ({
key: x,
name: power_user.personas[x] ?? '',
description: power_user.persona_descriptions[x]?.description ?? ''
description: power_user.persona_descriptions[x]?.description ?? '',
}));
const keys = [
@ -2080,19 +2079,21 @@ const compareFunc = (first, second) => {
* Sorts an array of entities based on the current sort settings
* @param {any[]} entities An array of objects with an `item` property
* @param {boolean} forceSearch Whether to force search sorting
* @param {import('./filters.js').FilterHelper} [filterHelper=null] Filter helper to use
*/
export function sortEntitiesList(entities, forceSearch) {
export function sortEntitiesList(entities, forceSearch, filterHelper = null) {
filterHelper = filterHelper ?? entitiesFilter;
if (power_user.sort_field == undefined || entities.length === 0) {
return;
}
if (power_user.sort_order === 'random') {
const isSearch = forceSearch || $('#character_sort_order option[data-field="search"]').is(':selected');
if (!isSearch && power_user.sort_order === 'random') {
shuffle(entities);
return;
}
const isSearch = forceSearch || $('#character_sort_order option[data-field="search"]').is(':selected');
entities.sort((a, b) => {
// Sort tags/folders will always be at the top
if (a.type === 'tag' && b.type !== 'tag') {
@ -2104,8 +2105,8 @@ export function sortEntitiesList(entities, forceSearch) {
// If we have search sorting, we take scores and use those
if (isSearch) {
const aScore = entitiesFilter.getScore(FILTER_TYPES.SEARCH, `${a.type}.${a.id}`);
const bScore = entitiesFilter.getScore(FILTER_TYPES.SEARCH, `${b.type}.${b.id}`);
const aScore = filterHelper.getScore(FILTER_TYPES.SEARCH, `${a.type}.${a.id}`);
const bScore = filterHelper.getScore(FILTER_TYPES.SEARCH, `${b.type}.${b.id}`);
return (aScore - bScore);
}