mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
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.
This commit is contained in:
@@ -63,19 +63,9 @@ export function getPresetDisplayName(
|
||||
|
||||
export function getPresetSearchText(
|
||||
entry: PresetEntry,
|
||||
presetCategoryLabels: Record<string, string>,
|
||||
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<string, string>,
|
||||
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<string, string>;
|
||||
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 => {
|
||||
|
||||
@@ -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,
|
||||
}),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user