Skip to content
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
58 changes: 41 additions & 17 deletions dashboard/src/views/extension/useExtensionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment on lines +330 to +339
Copy link
Contributor

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 数组上操作(如果需要避免原地修改,可以先做一个浅拷贝)。

推荐实现:

  const sortPluginsByName = (plugins) => {
    return [...plugins].sort((a, b) => {
      const nameA = String(a?.name ?? "");
      const nameB = String(b?.name ?? "");
      return nameA.localeCompare(nameB, undefined, {
        sensitivity: "base",
      });
    });
  };

如果 sortPluginsByName 的调用方之前依赖它保留原数组引用(即就地修改 plugins),那你可以去掉展开操作,直接调用 plugins.sort(...)。这种情况下,将函数体更新为 plugins.sort(...),并可选择返回 plugins 以便链式调用。

Original comment in English

suggestion (performance): Consider simplifying sorting logic by relying on stable Array.prototype.sort instead 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/post map calls and index tracking, keeping just the localeCompare-based comparator and operating directly on the plugins array (or a shallow copy if you want to avoid mutation).

Suggested implementation:

  const sortPluginsByName = (plugins) => {
    return [...plugins].sort((a, b) => {
      const nameA = String(a?.name ?? "");
      const nameB = String(b?.name ?? "");
      return nameA.localeCompare(nameB, undefined, {
        sensitivity: "base",
      });
    });
  };

If callers of sortPluginsByName previously depended on it preserving the original array reference (i.e., mutating plugins in place), you can drop the spread and simply call plugins.sort(...) instead. In that case, update the body to plugins.sort(...) and optionally return plugins for chaining.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了提高代码的可读性和可维护性,可以考虑将过滤逻辑重构一下。通过将所有需要搜索的字段放入一个数组中,然后使用 some 方法来检查是否至少有一个字段匹配搜索条件,可以避免重复的 toLowerCase().includes() 调用,也让未来增删搜索字段变得更加容易。

      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]);
});

// 过滤后的插件市场数据(带搜索)
Expand Down