Line data Source code
1 : import { partition } from 'es-toolkit'
2 : import { computed, ref, watch } from 'vue'
3 :
4 : import type { AssetInfo } from '@comfyorg/ingest-types'
5 :
6 : type SectionId = 'media' | 'models'
7 :
8 : interface AssetSection {
9 : id: SectionId
10 : labelKey: string
11 : items: AssetInfo[]
12 : }
13 :
14 1 : export function useAssetSections(items: () => AssetInfo[]) {
15 1 : const sections = computed(() => {
16 1 : const [models, media] = partition(items(), (a) => a.model)
17 : const allSections: AssetSection[] = [
18 : {
19 : id: 'media',
20 : labelKey: 'shareWorkflow.mediaLabel',
21 : items: media
22 : },
23 : {
24 : id: 'models',
25 : labelKey: 'shareWorkflow.modelsLabel',
26 : items: models
27 : }
28 : ]
29 1 : return allSections.filter((s) => s.items.length > 0)
30 : })
31 :
32 1 : const expandedSectionId = ref<SectionId | null>(null)
33 :
34 1 : function getDefaultExpandedSection(
35 : availableSections: AssetSection[]
36 : ): SectionId | null {
37 1 : if (availableSections.length === 0) return null
38 1 : return (
39 : availableSections.find((s) => s.id === 'media')?.id ??
40 : availableSections[0].id
41 : )
42 : }
43 :
44 1 : watch(
45 : sections,
46 : (availableSections) => {
47 : const hasExpanded = availableSections.some(
48 : (s) => s.id === expandedSectionId.value
49 : )
50 1 : if (hasExpanded) return
51 1 : expandedSectionId.value = getDefaultExpandedSection(availableSections)
52 : },
53 : { immediate: true }
54 : )
55 :
56 0 : function onSectionOpenChange(sectionId: SectionId, open: boolean) {
57 0 : if (open) {
58 0 : expandedSectionId.value = sectionId
59 0 : return
60 : }
61 0 : if (expandedSectionId.value === sectionId) {
62 0 : expandedSectionId.value = null
63 : }
64 : }
65 :
66 1 : return {
67 : sections,
68 : expandedSectionId,
69 : onSectionOpenChange
70 : }
71 : }
|