perf: reduce repeated collection traversal

This commit is contained in:
2026-06-05 19:11:43 +08:00
parent 13893bc3a9
commit a1fd24a5f6
10 changed files with 90 additions and 32 deletions
+7 -4
View File
@@ -13,7 +13,7 @@ import {
VideoCameraOutlined,
} from "@ant-design/icons";
import type { ReactNode } from "react";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import "../../styles/pages/more.css";
import type { WebImageWorkbenchTool, WebViewKey } from "../../types";
@@ -144,9 +144,12 @@ function MorePage({ onSelectView, onOpenImageTool }: MorePageProps) {
return t.category === filter;
});
const recentTools = recentIds
.map((id) => tools.find((t) => t.id === id))
.filter((t): t is MoreTool => Boolean(t) && (t?.ready ?? false));
const toolById = useMemo(() => new Map(tools.map((tool) => [tool.id, tool])), []);
const recentTools = recentIds.reduce<MoreTool[]>((acc, id) => {
const tool = toolById.get(id);
if (tool?.ready) acc.push(tool);
return acc;
}, []);
const groupedTools = filteredTools.reduce<Record<ToolCategory, MoreTool[]>>((acc, t) => {
if (!acc[t.category]) acc[t.category] = [];