Line data Source code
1 16 : <script setup lang="ts">
2 : import { unrefElement } from '@vueuse/core'
3 : import { PopoverRoot, PopoverTrigger } from 'reka-ui'
4 : import { ref, useTemplateRef } from 'vue'
5 : import { useI18n } from 'vue-i18n'
6 :
7 : import Button from '@comfyorg/design-system/button/Button'
8 : import PopoverContent from '@comfyorg/design-system/popover/PopoverContent'
9 : import { usePopoverViewportDismiss } from '@comfyorg/design-system/lib/popover/usePopoverViewportDismiss'
10 : import type {
11 : FilterOption,
12 : OwnershipFilterOption,
13 : OwnershipOption
14 : } from '@/platform/assets/types/filterTypes'
15 : import { cn } from '@comfyorg/tailwind-utils'
16 :
17 : import AsyncSearchInput from '@comfyorg/design-system/search-input/AsyncSearchInput'
18 : import { DROPDOWN_PANEL_CLASS } from './shared'
19 : import type { LayoutMode, SortOption } from './types'
20 :
21 8 : const { t } = useI18n()
22 :
23 : defineProps<{
24 : sortOptions: SortOption[]
25 : showOwnershipFilter?: boolean
26 : ownershipOptions?: OwnershipFilterOption[]
27 : showBaseModelFilter?: boolean
28 : baseModelOptions?: FilterOption[]
29 : candidateLabel?: string
30 : }>()
31 8 : const emit = defineEmits<{
32 : (e: 'search-enter'): void
33 : }>()
34 :
35 8 : const layoutMode = defineModel<LayoutMode>('layoutMode')
36 8 : const searchQuery = defineModel<string>('searchQuery')
37 8 : const sortSelected = defineModel<string>('sortSelected')
38 8 : const ownershipSelected = defineModel<OwnershipOption>('ownershipSelected', {
39 : default: 'all'
40 : })
41 8 : const baseModelSelected = defineModel<Set<string>>('baseModelSelected', {
42 : default: () => new Set()
43 : })
44 :
45 8 : const actionButtonStyle = cn(
46 : 'h-8 rounded-lg bg-zinc-500/20 outline-1 -outline-offset-1 outline-node-border transition-all duration-150'
47 : )
48 :
49 8 : const actionPopoverClass = cn(
50 : DROPDOWN_PANEL_CLASS,
51 : 'pointer-events-auto w-auto rounded-lg border-none bg-transparent p-0 pt-2 shadow-lg data-[state=closed]:animate-none data-[state=open]:animate-none'
52 : )
53 :
54 847 : const layoutSwitchItemStyle =
55 847 : 'size-6 flex rounded-sm transition-all duration-150 hover:scale-108 hover:text-foreground active:scale-95'
56 :
57 8 : const sortTriggerRef = useTemplateRef('sortTriggerRef')
58 8 : const isSortPopoverOpen = ref(false)
59 :
60 0 : function closeSortPopover() {
61 0 : isSortPopoverOpen.value = false
62 : }
63 :
64 0 : function handleSortSelected(item: SortOption) {
65 0 : sortSelected.value = item.id
66 0 : closeSortPopover()
67 : }
68 :
69 8 : const ownershipTriggerRef = useTemplateRef('ownershipTriggerRef')
70 8 : const isOwnershipPopoverOpen = ref(false)
71 :
72 0 : function closeOwnershipPopover() {
73 0 : isOwnershipPopoverOpen.value = false
74 : }
75 :
76 0 : function handleOwnershipSelected(item: OwnershipFilterOption) {
77 0 : ownershipSelected.value = item.value
78 0 : closeOwnershipPopover()
79 : }
80 :
81 8 : const baseModelTriggerRef = useTemplateRef('baseModelTriggerRef')
82 8 : const isBaseModelPopoverOpen = ref(false)
83 :
84 8 : usePopoverViewportDismiss(isSortPopoverOpen, () => unrefElement(sortTriggerRef))
85 8 : usePopoverViewportDismiss(isOwnershipPopoverOpen, () =>
86 : unrefElement(ownershipTriggerRef)
87 : )
88 8 : usePopoverViewportDismiss(isBaseModelPopoverOpen, () =>
89 : unrefElement(baseModelTriggerRef)
90 : )
91 :
92 0 : function toggleBaseModelSelection(item: FilterOption) {
93 0 : const current = new Set(baseModelSelected.value)
94 0 : baseModelSelected.value = current.has(item.value)
95 : ? new Set([...current].filter((v) => v !== item.value))
96 : : new Set([...current, item.value])
97 : }
98 :
99 1 : function handleSearchEnter(event: KeyboardEvent) {
100 1 : event.preventDefault()
101 1 : emit('search-enter')
102 : }
103 8 : </script>
104 :
105 : <template>
106 9 : <div class="flex gap-2 px-4 text-muted-foreground">
107 : <AsyncSearchInput
108 : v-model="searchQuery"
109 : :clear-label="$t('g.clear')"
110 : :placeholder="$t('g.searchPlaceholder', { subject: '' })"
111 : :accessible-label="$t('g.search')"
112 : autofocus
113 : :class="
114 : cn(
115 : actionButtonStyle,
116 : 'hover:outline-muted-foreground/80',
117 : 'focus-within:ring-0 focus-within:outline-ring/80'
118 : )
119 : "
120 : @enter="handleSearchEnter"
121 : />
122 : <span
123 : v-if="candidateLabel"
124 : role="status"
125 : aria-live="polite"
126 : aria-atomic="true"
127 : class="sr-only"
128 : >
129 : {{ t('widgets.uploadSelect.topResult', { result: candidateLabel }) }}
130 : </span>
131 :
132 : <PopoverRoot v-model:open="isSortPopoverOpen">
133 : <PopoverTrigger as-child>
134 : <Button
135 : ref="sortTriggerRef"
136 : :aria-label="t('assetBrowser.sortBy')"
137 : :title="t('assetBrowser.sortBy')"
138 : variant="textonly"
139 : size="icon"
140 : :class="
141 : cn(
142 : actionButtonStyle,
143 : 'hover:outline-muted-foreground active:scale-95'
144 : )
145 : "
146 : >
147 : <div
148 : v-if="sortSelected !== 'default'"
149 : class="absolute top-[-2px] left-[-2px] size-2 rounded-full bg-muted-foreground"
150 : />
151 : <i class="icon-[lucide--arrow-up-down]" />
152 : </Button>
153 : </PopoverTrigger>
154 : <PopoverContent align="start" :class="actionPopoverClass">
155 : <div
156 : :class="
157 : cn(
158 : 'flex min-w-32 flex-col gap-2 p-2',
159 : 'bg-node',
160 : 'rounded-lg outline -outline-offset-1 outline-node-border'
161 : )
162 : "
163 : >
164 0 : <Button
165 : v-for="item of sortOptions"
166 : :key="item.name"
167 : variant="textonly"
168 : size="unset"
169 : class="flex h-6 justify-between text-left"
170 : @click="handleSortSelected(item)"
171 : >
172 : <span>{{ item.name }}</span>
173 : <i v-if="sortSelected === item.id" class="icon-[lucide--check]" />
174 : </Button>
175 : </div>
176 : </PopoverContent>
177 : </PopoverRoot>
178 :
179 : <PopoverRoot
180 : v-if="showOwnershipFilter && ownershipOptions?.length"
181 : v-model:open="isOwnershipPopoverOpen"
182 : >
183 : <PopoverTrigger as-child>
184 : <Button
185 : ref="ownershipTriggerRef"
186 : :aria-label="t('assetBrowser.ownership')"
187 : :title="t('assetBrowser.ownership')"
188 : variant="textonly"
189 : size="icon"
190 : :class="
191 : cn(
192 : actionButtonStyle,
193 : 'hover:outline-muted-foreground active:scale-95'
194 : )
195 : "
196 : >
197 : <div
198 : v-if="ownershipSelected !== 'all'"
199 : class="absolute top-[-2px] left-[-2px] size-2 rounded-full bg-muted-foreground"
200 : />
201 : <i class="icon-[lucide--user]" />
202 : </Button>
203 : </PopoverTrigger>
204 : <PopoverContent align="start" :class="actionPopoverClass">
205 : <div
206 : :class="
207 : cn(
208 : 'flex min-w-32 flex-col gap-2 p-2',
209 : 'bg-node',
210 : 'rounded-lg outline -outline-offset-1 outline-node-border'
211 : )
212 : "
213 : >
214 0 : <Button
215 : v-for="item of ownershipOptions"
216 : :key="item.value"
217 : variant="textonly"
218 : size="unset"
219 : class="flex h-6 justify-between text-left"
220 : @click="handleOwnershipSelected(item)"
221 : >
222 : <span>{{ item.name }}</span>
223 : <i
224 : v-if="ownershipSelected === item.value"
225 : class="icon-[lucide--check]"
226 : />
227 : </Button>
228 : </div>
229 : </PopoverContent>
230 : </PopoverRoot>
231 :
232 : <PopoverRoot
233 : v-if="showBaseModelFilter && baseModelOptions?.length"
234 : v-model:open="isBaseModelPopoverOpen"
235 : >
236 : <PopoverTrigger as-child>
237 : <Button
238 : ref="baseModelTriggerRef"
239 : :aria-label="t('assetBrowser.baseModel')"
240 : :title="t('assetBrowser.baseModel')"
241 : variant="textonly"
242 : size="icon"
243 : :class="
244 : cn(
245 : actionButtonStyle,
246 : 'hover:outline-muted-foreground active:scale-95'
247 : )
248 : "
249 : >
250 : <div
251 : v-if="baseModelSelected.size > 0"
252 : class="absolute top-[-2px] left-[-2px] size-2 rounded-full bg-muted-foreground"
253 : />
254 : <i class="icon-[comfy--ai-model]" />
255 : </Button>
256 : </PopoverTrigger>
257 : <PopoverContent align="start" :class="actionPopoverClass">
258 : <div
259 : :class="
260 : cn(
261 : 'flex min-w-32 flex-col gap-2 p-2',
262 : 'bg-node',
263 : 'rounded-lg outline -outline-offset-1 outline-node-border'
264 : )
265 : "
266 : >
267 0 : <Button
268 : v-for="item of baseModelOptions"
269 : :key="item.value"
270 : variant="textonly"
271 : size="unset"
272 : class="flex h-6 justify-between text-left"
273 : @click="toggleBaseModelSelection(item)"
274 : >
275 : <span>{{ item.name }}</span>
276 : <i
277 : v-if="baseModelSelected.has(item.value)"
278 : class="icon-[lucide--check]"
279 : />
280 : </Button>
281 : <span class="h-0 w-full border-b" />
282 : <Button
283 : variant="textonly"
284 : size="unset"
285 : class="flex h-6 justify-between text-left"
286 : @click="baseModelSelected = new Set()"
287 : >
288 : {{ t('g.clearFilters') }}
289 : </Button>
290 : </div>
291 : </PopoverContent>
292 : </PopoverRoot>
293 :
294 : <div
295 : :class="
296 : cn(
297 : actionButtonStyle,
298 : 'flex items-center justify-center gap-1 p-1 hover:outline-muted-foreground'
299 : )
300 : "
301 : >
302 : <Button
303 : :aria-label="t('assetBrowser.listView')"
304 : :title="t('assetBrowser.listView')"
305 : variant="textonly"
306 : size="unset"
307 : :class="cn(layoutSwitchItemStyle, layoutMode === 'list' && 'bg-muted')"
308 : @click="layoutMode = 'list'"
309 : >
310 : <i class="icon-[lucide--list]" />
311 : </Button>
312 : <Button
313 : :aria-label="t('assetBrowser.gridView')"
314 : :title="t('assetBrowser.gridView')"
315 : variant="textonly"
316 : size="unset"
317 : :class="cn(layoutSwitchItemStyle, layoutMode === 'grid' && 'bg-muted')"
318 : @click="layoutMode = 'grid'"
319 : >
320 : <i class="icon-[lucide--layout-grid]" />
321 : </Button>
322 : </div>
323 : </div>
324 : </template>
|