Line data Source code
1 : import { computed, ref, watch } from 'vue'
2 :
3 : import { st } from '@/i18n'
4 : import { getSettingInfo } from '@comfyorg/settings-core/settingStore'
5 : import { useSettingStore } from '@/platform/settings/settingStore'
6 : import type {
7 : ISettingGroup,
8 : SettingParams
9 : } from '@comfyorg/settings-core/types'
10 : import type { TreeNode } from '@/types/treeExplorerTypes'
11 : import { normalizeI18nKey } from '@/utils/formatUtil'
12 : import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
13 :
14 : export type SettingTreeNode = TreeNode<SettingParams>
15 :
16 : interface SearchableNavItem {
17 : key: string
18 : label: string
19 : }
20 :
21 107 : export function useSettingSearch() {
22 107 : const settingStore = useSettingStore()
23 107 : const { shouldRenderVueNodes } = useVueFeatureFlags()
24 :
25 107 : const searchQuery = ref<string>('')
26 107 : const filteredSettingIds = ref<string[]>([])
27 107 : const matchedNavItemKeys = ref<Set<string>>(new Set())
28 107 : const searchInProgress = ref<boolean>(false)
29 :
30 107 : watch(searchQuery, () => (searchInProgress.value = true))
31 :
32 : /**
33 : * Settings categories that contains at least one setting in search results.
34 : */
35 107 : const searchResultsCategories = computed<Set<string>>(() => {
36 116 : return new Set(
37 : filteredSettingIds.value.map(
38 : (id) => getSettingInfo(settingStore.settingsById[id]).category
39 : )
40 : )
41 : })
42 :
43 : /**
44 : * Check if the search query is empty
45 : */
46 107 : const queryIsEmpty = computed(() => searchQuery.value.length === 0)
47 :
48 : /**
49 : * Check if we're in search mode
50 : */
51 107 : const inSearch = computed(
52 : () => !queryIsEmpty.value && !searchInProgress.value
53 : )
54 :
55 : /**
56 : * Handle search functionality
57 : */
58 107 : const handleSearch = (query: string, navItems?: SearchableNavItem[]) => {
59 9 : matchedNavItemKeys.value = new Set()
60 :
61 9 : if (!query) {
62 1 : filteredSettingIds.value = []
63 1 : return
64 : }
65 :
66 8 : const queryLower = query.toLocaleLowerCase()
67 : const allSettings = Object.values(settingStore.settingsById)
68 8 : const filteredSettings = allSettings.filter((setting) => {
69 : // Filter out hidden and deprecated settings, just like in normal settings tree
70 1239 : if (
71 : setting.type === 'hidden' ||
72 : setting.deprecated ||
73 : (shouldRenderVueNodes.value && setting.hideInVueNodes)
74 : ) {
75 370 : return false
76 : }
77 :
78 869 : const idLower = setting.id.toLowerCase()
79 869 : const nameLower = setting.name.toLowerCase()
80 869 : const translatedName = st(
81 : `settings.${normalizeI18nKey(setting.id)}.name`,
82 : setting.name
83 : ).toLocaleLowerCase()
84 869 : const info = getSettingInfo(setting)
85 869 : const translatedCategory = st(
86 : `settingsCategories.${normalizeI18nKey(info.category)}`,
87 : info.category
88 : ).toLocaleLowerCase()
89 869 : const translatedSubCategory = st(
90 : `settingsCategories.${normalizeI18nKey(info.subCategory)}`,
91 : info.subCategory
92 : ).toLocaleLowerCase()
93 :
94 1239 : return (
95 : idLower.includes(queryLower) ||
96 : nameLower.includes(queryLower) ||
97 : translatedName.includes(queryLower) ||
98 : translatedCategory.includes(queryLower) ||
99 : translatedSubCategory.includes(queryLower)
100 : )
101 : })
102 :
103 8 : if (navItems) {
104 8 : for (const item of navItems) {
105 77 : if (
106 : item.key.toLocaleLowerCase().includes(queryLower) ||
107 : item.label.toLocaleLowerCase().includes(queryLower)
108 : ) {
109 4 : matchedNavItemKeys.value.add(item.key)
110 : }
111 : }
112 : }
113 :
114 8 : filteredSettingIds.value = filteredSettings.map((x) => x.id)
115 8 : searchInProgress.value = false
116 : }
117 :
118 : /**
119 : * Get search results grouped by category
120 : */
121 107 : const getSearchResults = (
122 : activeCategory: SettingTreeNode | null
123 : ): ISettingGroup[] => {
124 8 : const groupedSettings = new Map<
125 : string,
126 : { category: string; settings: SettingParams[] }
127 : >()
128 :
129 8 : filteredSettingIds.value.forEach((id) => {
130 8 : const setting = settingStore.settingsById[id]
131 8 : const info = getSettingInfo(setting)
132 8 : const groupKey =
133 : activeCategory === null
134 : ? `${info.category}/${info.subCategory}`
135 : : info.subCategory
136 :
137 8 : if (activeCategory === null || activeCategory.label === info.category) {
138 8 : let group = groupedSettings.get(groupKey)
139 8 : if (!group) {
140 8 : group = {
141 : category: info.category,
142 : settings: []
143 : }
144 8 : groupedSettings.set(groupKey, group)
145 : }
146 8 : group.settings.push(setting)
147 : }
148 : })
149 :
150 8 : return [...groupedSettings].map(([key, { category, settings }]) => ({
151 : label: activeCategory === null ? key.split('/')[1] : key,
152 : ...(activeCategory === null ? { category } : {}),
153 : settings
154 : }))
155 : }
156 :
157 107 : return {
158 : searchQuery,
159 : filteredSettingIds,
160 : matchedNavItemKeys,
161 : searchInProgress,
162 : searchResultsCategories,
163 : queryIsEmpty,
164 : inSearch,
165 : handleSearch,
166 : getSearchResults
167 : }
168 : }
|