-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: add plugin sorting by name and improve search filtering logic #5559
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -327,26 +327,50 @@ export const useExtensionPage = () => { | |
| return data; | ||
| }); | ||
|
|
||
| const sortPluginsByName = (plugins) => { | ||
| return plugins | ||
| .map((plugin, index) => ({ plugin, index })) | ||
| .sort((a, b) => { | ||
| const nameA = String(a.plugin?.name ?? ""); | ||
| const nameB = String(b.plugin?.name ?? ""); | ||
| const nameCompare = nameA.localeCompare(nameB, undefined, { | ||
| sensitivity: "base", | ||
| }); | ||
| if (nameCompare !== 0) { | ||
| return nameCompare; | ||
| } | ||
| return a.index - b.index; | ||
| }) | ||
| .map((item) => item.plugin); | ||
| }; | ||
|
|
||
| // 通过搜索过滤插件 | ||
| const filteredPlugins = computed(() => { | ||
| if (!pluginSearch.value) { | ||
| return filteredExtensions.value; | ||
| const plugins = filteredExtensions.value; | ||
| let filtered = plugins; | ||
|
|
||
| if (pluginSearch.value) { | ||
| const search = pluginSearch.value.toLowerCase(); | ||
| filtered = plugins.filter((plugin) => { | ||
| const pluginName = (plugin.name ?? "").toLowerCase(); | ||
| const pluginDesc = (plugin.desc ?? "").toLowerCase(); | ||
| const pluginAuthor = (plugin.author ?? "").toLowerCase(); | ||
| const supportPlatforms = Array.isArray(plugin.support_platforms) | ||
| ? plugin.support_platforms.join(" ").toLowerCase() | ||
| : ""; | ||
| const astrbotVersion = (plugin.astrbot_version ?? "").toLowerCase(); | ||
|
|
||
| return ( | ||
| pluginName.includes(search) || | ||
| pluginDesc.includes(search) || | ||
| pluginAuthor.includes(search) || | ||
| supportPlatforms.includes(search) || | ||
| astrbotVersion.includes(search) | ||
| ); | ||
| }); | ||
|
Comment on lines
+354
to
+370
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为了提高代码的可读性和可维护性,可以考虑将过滤逻辑重构一下。通过将所有需要搜索的字段放入一个数组中,然后使用 filtered = plugins.filter((plugin) => {
const fieldsToSearch = [
plugin.name,
plugin.desc,
plugin.author,
Array.isArray(plugin.support_platforms)
? plugin.support_platforms.join(" ")
: "",
plugin.astrbot_version,
];
return fieldsToSearch.some((field) =>
(field ?? "").toLowerCase().includes(search)
);
}); |
||
| } | ||
|
|
||
| const search = pluginSearch.value.toLowerCase(); | ||
| return filteredExtensions.value.filter((plugin) => { | ||
| const supportPlatforms = Array.isArray(plugin.support_platforms) | ||
| ? plugin.support_platforms.join(" ").toLowerCase() | ||
| : ""; | ||
| const astrbotVersion = (plugin.astrbot_version ?? "").toLowerCase(); | ||
| return ( | ||
| plugin.name?.toLowerCase().includes(search) || | ||
| plugin.desc?.toLowerCase().includes(search) || | ||
| plugin.author?.toLowerCase().includes(search) || | ||
| supportPlatforms.includes(search) || | ||
| astrbotVersion.includes(search) | ||
| ); | ||
| }); | ||
|
|
||
| return sortPluginsByName([...filtered]); | ||
| }); | ||
|
|
||
| // 过滤后的插件市场数据(带搜索) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (performance): 考虑利用稳定的
Array.prototype.sort来简化排序逻辑,而不是先映射成带索引的对象再排序。现代 JS 引擎(主流浏览器和 Node 12+)都保证了
Array.prototype.sort的稳定性,因此你可以依赖这种稳定性,而无需在排序过程中额外携带原始索引。这样就可以去掉排序前后的map调用和索引追踪,只保留基于localeCompare的比较器,并直接在plugins数组上操作(如果需要避免原地修改,可以先做一个浅拷贝)。推荐实现:
如果
sortPluginsByName的调用方之前依赖它保留原数组引用(即就地修改plugins),那你可以去掉展开操作,直接调用plugins.sort(...)。这种情况下,将函数体更新为plugins.sort(...),并可选择返回plugins以便链式调用。Original comment in English
suggestion (performance): Consider simplifying sorting logic by relying on stable
Array.prototype.sortinstead of mapping to index-tagged objects.Modern JS engines (evergreen browsers and Node 12+) guarantee a stable
Array.prototype.sort, so you can rely on that stability instead of carrying the original index through the sort. That lets you drop the pre/postmapcalls and index tracking, keeping just thelocaleCompare-based comparator and operating directly on thepluginsarray (or a shallow copy if you want to avoid mutation).Suggested implementation:
If callers of
sortPluginsByNamepreviously depended on it preserving the original array reference (i.e., mutatingpluginsin place), you can drop the spread and simply callplugins.sort(...)instead. In that case, update the body toplugins.sort(...)and optionally returnpluginsfor chaining.