( modelComparisons: ModelComparison[], viewOptions: ViewOptions, favoriteModels: FavoriteModels, hiddenModels: HiddenModels, )
| 4 | import { FavoriteModels, HiddenModels } from "./useModelPreferences"; |
| 5 | |
| 6 | export const useModelFiltering = ( |
| 7 | modelComparisons: ModelComparison[], |
| 8 | viewOptions: ViewOptions, |
| 9 | favoriteModels: FavoriteModels, |
| 10 | hiddenModels: HiddenModels, |
| 11 | ) => { |
| 12 | const [searchQuery, setSearchQuery] = useState(""); |
| 13 | |
| 14 | // 搜索和过滤逻辑 |
| 15 | const filteredModels = useMemo(() => { |
| 16 | let filtered = modelComparisons.filter((model) => { |
| 17 | // 搜索过滤 |
| 18 | if (searchQuery) { |
| 19 | const query = searchQuery.toLowerCase(); |
| 20 | const matchesName = model.modelName.toLowerCase().includes(query); |
| 21 | const matchesVendor = model.vendor.toLowerCase().includes(query); |
| 22 | const matchesTags = model.tags.some((tag) => tag.toLowerCase().includes(query)); |
| 23 | |
| 24 | if (!matchesName && !matchesVendor && !matchesTags) { |
| 25 | return false; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // 只显示收藏的模型 |
| 30 | if (viewOptions.onlyFavorites && !favoriteModels[model.modelName]) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | // 隐藏已隐藏的模型 |
| 35 | if (viewOptions.hideHiddenModels && hiddenModels[model.modelName]) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | // 按厂商过滤 |
| 40 | if (viewOptions.filterVendor && viewOptions.filterVendor !== model.vendor) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | // 按供应商数量过滤 |
| 45 | const providerCount = model.providers.length; |
| 46 | switch (viewOptions.providerCountFilter) { |
| 47 | case "all": |
| 48 | // 不过滤 |
| 49 | break; |
| 50 | case "single": |
| 51 | if (providerCount !== 1) return false; |
| 52 | break; |
| 53 | case "multiple": |
| 54 | if (providerCount <= 1) return false; |
| 55 | break; |
| 56 | default: |
| 57 | // 具体数字 |
| 58 | if (typeof viewOptions.providerCountFilter === "number") { |
| 59 | if (viewOptions.providerCountFilter === 11) { |
| 60 | // 11个或更多(当maxProviderCount > 10时) |
| 61 | if (providerCount < 11) return false; |
| 62 | } else { |
| 63 | // 恰好指定数量 |
no outgoing calls
no test coverage detected