Line data Source code
1 : import { computed, ref } from 'vue'
2 : import type { Ref } from 'vue'
3 :
4 : import { getOutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataSchema'
5 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
6 : import { resolveOutputAssetItems } from '@/platform/assets/utils/outputAssetUtil'
7 : import { getOutputKey } from '@/platform/assets/utils/outputKeyUtil'
8 :
9 : export type OutputStackListItem = {
10 : id: string
11 : asset: AssetItem
12 : isChild?: boolean
13 : }
14 :
15 : type UseOutputStacksOptions = {
16 : assets: Ref<AssetItem[]>
17 : }
18 :
19 84 : export function useOutputStacks({ assets }: UseOutputStacksOptions) {
20 84 : const expandedStackJobIds = ref<Set<string>>(new Set())
21 84 : const stackChildrenByJobId = ref<Record<string, AssetItem[]>>({})
22 84 : const loadingStackJobIds = ref<Set<string>>(new Set())
23 :
24 84 : const assetItems = computed<OutputStackListItem[]>(() => {
25 2 : const items: OutputStackListItem[] = []
26 :
27 2 : for (const asset of assets.value) {
28 6 : const jobId = getStackJobId(asset)
29 6 : items.push({
30 : id: asset.id,
31 : asset
32 : })
33 :
34 6 : if (!jobId || !expandedStackJobIds.value.has(jobId)) {
35 6 : continue
36 : }
37 :
38 6 : const children = stackChildrenByJobId.value[jobId] ?? []
39 6 : for (const child of children) {
40 0 : items.push({
41 : id: child.id,
42 : asset: child,
43 : isChild: true
44 : })
45 : }
46 : }
47 :
48 2 : return items
49 : })
50 :
51 84 : const selectableAssets = computed(() =>
52 : assetItems.value.map((item) => item.asset)
53 : )
54 :
55 12 : function getStackJobId(asset: AssetItem): string | null {
56 : const metadata = getOutputAssetMetadata(asset.user_metadata)
57 12 : return metadata?.jobId ?? null
58 : }
59 :
60 6 : function isStackExpanded(asset: AssetItem): boolean {
61 6 : const jobId = getStackJobId(asset)
62 6 : if (!jobId) return false
63 6 : return expandedStackJobIds.value.has(jobId)
64 : }
65 :
66 0 : async function toggleStack(asset: AssetItem) {
67 0 : const jobId = getStackJobId(asset)
68 0 : if (!jobId) return
69 :
70 0 : if (expandedStackJobIds.value.has(jobId)) {
71 0 : const next = new Set(expandedStackJobIds.value)
72 0 : next.delete(jobId)
73 0 : expandedStackJobIds.value = next
74 0 : return
75 : }
76 :
77 0 : if (!stackChildrenByJobId.value[jobId]?.length) {
78 0 : if (loadingStackJobIds.value.has(jobId)) {
79 0 : return
80 : }
81 0 : const nextLoading = new Set(loadingStackJobIds.value)
82 0 : nextLoading.add(jobId)
83 0 : loadingStackJobIds.value = nextLoading
84 :
85 0 : const children = await resolveStackChildren(asset)
86 :
87 0 : const afterLoading = new Set(loadingStackJobIds.value)
88 0 : afterLoading.delete(jobId)
89 0 : loadingStackJobIds.value = afterLoading
90 :
91 0 : if (!children.length) {
92 0 : return
93 : }
94 :
95 0 : stackChildrenByJobId.value = {
96 : ...stackChildrenByJobId.value,
97 : [jobId]: children
98 : }
99 : }
100 :
101 0 : const nextExpanded = new Set(expandedStackJobIds.value)
102 0 : nextExpanded.add(jobId)
103 0 : expandedStackJobIds.value = nextExpanded
104 : }
105 :
106 0 : async function resolveStackChildren(asset: AssetItem): Promise<AssetItem[]> {
107 0 : const metadata = getOutputAssetMetadata(asset.user_metadata)
108 0 : if (!metadata) {
109 0 : return []
110 : }
111 :
112 0 : const excludeOutputKey =
113 : getOutputKey({
114 : nodeId: metadata.nodeId,
115 : subfolder: metadata.subfolder,
116 : filename: asset.name
117 : }) ?? undefined
118 :
119 0 : try {
120 0 : return await resolveOutputAssetItems(metadata, {
121 : createdAt: asset.created_at,
122 : excludeOutputKey
123 : })
124 : } catch (error) {
125 0 : console.error('Failed to resolve stack children:', error)
126 0 : return []
127 : }
128 : }
129 :
130 84 : return {
131 : assetItems,
132 : selectableAssets,
133 : isStackExpanded,
134 : toggleStack
135 : }
136 : }
|