Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[23.2] Fix ToolSearch bug for StaticToolPanelViews #17213

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 9 additions & 55 deletions client/src/components/Panels/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const UNSECTIONED_SECTION = {
name: "Unsectioned Tools",
description: "Tools that don't appear under any section in the unsearched panel",
};
const VALID_SECTION_TYPES = ["edam_operations", "edam_topics"]; // other than `default`

/** These are keys used to order/sort results in `ToolSearch`.
* The value for each is the sort order, higher number = higher rank.
Expand All @@ -39,13 +38,14 @@ export interface ToolSearchKeys {
startsWith?: number;
/** query contains matches `Tool.name + Tool.description` */
combined?: number;
/** property matches */
/** `Tool.name + Tool.description` contains at least
* `MINIMUM_WORD_MATCH` words from query
*/
wordMatch?: number;
}

interface SearchMatch {
id: string;
sections: (string | undefined)[];
order: number;
}

Expand Down Expand Up @@ -296,19 +296,15 @@ export function searchToolsByKeys(
if (!usesDL) {
if (actualValue.match(queryValue)) {
// if string.match() returns true, matching tool found
matchedTools.push({ id: tool.id, sections: getPanelSectionsForTool(tool, panelView), order });
matchedTools.push({ id: tool.id, order });
break;
} else if (
key === "combined" &&
keys.wordMatch !== undefined &&
wordMatches.length >= MINIMUM_WORD_MATCH
) {
// we are looking at combined name+description, and there are enough word matches
matchedTools.push({
id: tool.id,
sections: getPanelSectionsForTool(tool, panelView),
order: keys.wordMatch,
});
matchedTools.push({ id: tool.id, order: keys.wordMatch });
break;
}
} else if (usesDL) {
Expand All @@ -324,11 +320,7 @@ export function searchToolsByKeys(
if (foundTerm && (!closestTerm || (closestTerm && foundTerm.length < closestTerm.length))) {
closestTerm = foundTerm;
}
matchedTools.push({
id: tool.id,
sections: getPanelSectionsForTool(tool, panelView),
order,
});
matchedTools.push({ id: tool.id, order });
break;
}
}
Expand Down Expand Up @@ -357,12 +349,11 @@ export function createSortedResultObject(
) {
const idResults: string[] = [];
// creating a sectioned results object ({section_id: [tool ids], ...}), keeping
// track of the best version of each tool, and also sorting by indexed order of keys
// track unique ids of each tool, and also sorting by indexed order of keys
const resultPanel = orderBy(matchedTools, ["order"], ["desc"]).reduce(
(acc: Record<string, Tool | ToolSection>, match: SearchMatch) => {
// we either found specific section(s) for tool, or we need to search all sections
const sections: (string | undefined)[] =
match.sections.length !== 0 ? match.sections : Object.keys(currentPanel);
// we need to search all sections in panel for this tool id
const sections = Object.keys(currentPanel);
for (const section of sections) {
let toolAdded = false;
const existingPanelItem = section ? currentPanel[section] : undefined;
Expand Down Expand Up @@ -408,43 +399,6 @@ export function createSortedResultObject(
return { idResults, resultPanel };
}

/**
* Gets the section(s) a tool belongs to for a given panelView.
* Unless view=`default`, all other views must be of format `class:view_type`,
* where `Tool` object has a `view_name` key containing an array of section ids.
* e.g.: view = `ontology:edam_operations` => `Tool.edam_operations = [section ids]`.
*
* Therefore, this would not handle the case where view = `ontology:edam_merged`, since
* Tool.edam_merged is not a valid key, and we would just return `[uncategorized]`.
*
* Just prevents looking through whole panel to find section id for tool,
* we still end up doing that (in `createSortedResultObject`) in case we return [] here
* @param tool
* @param panelView
* @returns Array of section ids
*/
function getPanelSectionsForTool(tool: Tool, panelView: string) {
if (panelView === "default" && tool.panel_section_id) {
if (tool.panel_section_id.startsWith("tool_")) {
return [tool.panel_section_id.split("tool_")[1]];
} else {
return [tool.panel_section_id];
}
} else if (panelView !== "default") {
const sectionType = panelView.split(":")[1] as keyof Tool;
if (
sectionType &&
VALID_SECTION_TYPES.includes(sectionType as string) &&
(tool[sectionType] as string[])?.length !== 0
) {
return tool[sectionType] as string[];
} else {
return ["uncategorized"];
}
}
return [];
}

/**
*
* @param query
Expand Down
Loading