From 276b2572a3c693f26b7b3c719b1c28ed167a6244 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 13 Jun 2026 22:15:32 +0800 Subject: [PATCH] fix(providers): scope preset search to provider names only The preset search text also included websiteUrl and the shared category label, producing imprecise matches: a single category term matched the whole group, and URL fragments like "com"/"api" matched nearly everything. Restrict the search text to the display name and raw name; category labels are still used for rendering. --- .../forms/ProviderPresetSelector.tsx | 29 +++--------- .../ProviderPresetSelector.test.tsx | 45 +++++-------------- 2 files changed, 15 insertions(+), 59 deletions(-) diff --git a/src/components/providers/forms/ProviderPresetSelector.tsx b/src/components/providers/forms/ProviderPresetSelector.tsx index c24c5a87e..29ca675f6 100644 --- a/src/components/providers/forms/ProviderPresetSelector.tsx +++ b/src/components/providers/forms/ProviderPresetSelector.tsx @@ -63,19 +63,9 @@ export function getPresetDisplayName( export function getPresetSearchText( entry: PresetEntry, - presetCategoryLabels: Record, t: PresetTranslator, ): string { - const presetCategory = entry.preset.category ?? "others"; - const categoryLabel = - presetCategoryLabels[presetCategory] ?? String(t("providerPreset.other")); - - return [ - getPresetDisplayName(entry.preset, t), - entry.preset.name, - entry.preset.websiteUrl, - categoryLabel, - ] + return [getPresetDisplayName(entry.preset, t), entry.preset.name] .join(" ") .toLowerCase(); } @@ -83,7 +73,6 @@ export function getPresetSearchText( export function filterPresetEntries( entries: PresetEntry[], query: string, - presetCategoryLabels: Record, t: PresetTranslator, ): PresetEntry[] { const normalizedQuery = query.trim().toLowerCase(); @@ -92,9 +81,7 @@ export function filterPresetEntries( } return entries.filter((entry) => - getPresetSearchText(entry, presetCategoryLabels, t).includes( - normalizedQuery, - ), + getPresetSearchText(entry, t).includes(normalizedQuery), ); } @@ -117,7 +104,6 @@ export function sortPresetEntries( export interface PresetVisibilityOptions { query: string; sortMode: PresetSortMode; - presetCategoryLabels: Record; t: PresetTranslator; } @@ -125,13 +111,9 @@ export function getVisiblePresetEntries( entries: PresetEntry[], options: PresetVisibilityOptions, ): PresetEntry[] { - const { query, sortMode, presetCategoryLabels, t } = options; + const { query, sortMode, t } = options; - return sortPresetEntries( - filterPresetEntries(entries, query, presetCategoryLabels, t), - sortMode, - t, - ); + return sortPresetEntries(filterPresetEntries(entries, query, t), sortMode, t); } interface ProviderPresetSelectorProps { @@ -165,10 +147,9 @@ export function ProviderPresetSelector({ getVisiblePresetEntries(presetEntries, { query: searchQuery, sortMode, - presetCategoryLabels, t, }), - [presetEntries, presetCategoryLabels, searchQuery, sortMode, t], + [presetEntries, searchQuery, sortMode, t], ); const getCategoryHint = (): ReactNode => { diff --git a/tests/components/ProviderPresetSelector.test.tsx b/tests/components/ProviderPresetSelector.test.tsx index 394aaa798..005161d2a 100644 --- a/tests/components/ProviderPresetSelector.test.tsx +++ b/tests/components/ProviderPresetSelector.test.tsx @@ -153,52 +153,28 @@ describe("ProviderPresetSelector pure helpers", () => { ); }); - it("拼接显示名、原始名称、URL、分类 label,并统一 lower-case", () => { - const searchText = getPresetSearchText( - presetEntries[1], - presetCategoryLabels, - t, - ); + it("仅拼接显示名与原始名称、统一 lower-case,不含 URL 或分类 label", () => { + const searchText = getPresetSearchText(presetEntries[1], t); expect(searchText).toContain("alpha 本地名"); expect(searchText).toContain("alpha raw"); - expect(searchText).toContain("https://alpha.example.com/v1"); - expect(searchText).toContain("官方"); + expect(searchText).not.toContain("example.com"); + expect(searchText).not.toContain("官方"); expect(searchText).toBe(searchText.toLowerCase()); }); it("空 query 返回原数组,非空 query 大小写不敏感匹配", () => { + expect(filterPresetEntries(presetEntries, " ", t)).toBe(presetEntries); expect( - filterPresetEntries(presetEntries, " ", presetCategoryLabels, t), - ).toBe(presetEntries); - expect( - getIds( - filterPresetEntries( - presetEntries, - "ALPHA 本地名", - presetCategoryLabels, - t, - ), - ), + getIds(filterPresetEntries(presetEntries, "ALPHA 本地名", t)), ).toEqual(["alpha"]); }); - it("支持通过 URL 和分类 label 搜索", () => { + it("不再通过 URL 或分类 label 搜索(仅匹配名称)", () => { expect( - getIds( - filterPresetEntries( - presetEntries, - "cn-gateway.example.com", - presetCategoryLabels, - t, - ), - ), - ).toEqual(["beta"]); - expect( - getIds( - filterPresetEntries(presetEntries, "聚合", presetCategoryLabels, t), - ), - ).toEqual(["gamma"]); + getIds(filterPresetEntries(presetEntries, "cn-gateway.example.com", t)), + ).toEqual([]); + expect(getIds(filterPresetEntries(presetEntries, "聚合", t))).toEqual([]); }); it("支持 A-Z 排序、original 副本恢复原顺序,并且 getVisible 先 filter 再 sort", () => { @@ -222,7 +198,6 @@ describe("ProviderPresetSelector pure helpers", () => { getVisiblePresetEntries(presetEntries, { query: "a", sortMode: nameAscMode, - presetCategoryLabels, t, }), ),