Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { ref, watch } from 'vue'
3 :
4 : import type { TaskItemImpl } from '@/stores/queueStore'
5 : import { useQueueStore } from '@/stores/queueStore'
6 : import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore'
7 :
8 2355 : const getAddedAssetCount = (task: TaskItemImpl): number => {
9 26 : const count = task.previewableOutputsCount ?? task.outputsCount
10 26 : if (typeof count === 'number') {
11 26 : return Math.max(count, 0)
12 : }
13 :
14 26 : return task.previewOutput ? 1 : 0
15 : }
16 :
17 2355 : export const useAssetsSidebarBadgeStore = defineStore(
18 : 'assetsSidebarBadge',
19 : () => {
20 12 : const queueStore = useQueueStore()
21 12 : const sidebarTabStore = useSidebarTabStore()
22 :
23 12 : const unseenAddedAssetsCount = ref(0)
24 12 : const countedHistoryAssetsByJobId = ref(new Map<string, number>())
25 12 : const hasInitializedHistory = ref(false)
26 :
27 12 : const markCurrentHistoryAsSeen = () => {
28 12 : countedHistoryAssetsByJobId.value = new Map(
29 : queueStore.historyTasks.map((task) => [
30 : task.jobId,
31 : getAddedAssetCount(task)
32 : ])
33 : )
34 : }
35 :
36 12 : watch(
37 : () =>
38 : [
39 : queueStore.historyTasks,
40 : queueStore.hasFetchedHistorySnapshot
41 : ] as const,
42 : ([historyTasks, hasFetchedHistorySnapshot]) => {
43 23 : if (!hasFetchedHistorySnapshot) {
44 10 : return
45 : }
46 :
47 14 : if (!hasInitializedHistory.value) {
48 12 : hasInitializedHistory.value = true
49 12 : markCurrentHistoryAsSeen()
50 12 : return
51 : }
52 :
53 1 : const isAssetsTabOpen = sidebarTabStore.activeSidebarTabId === 'assets'
54 1 : const previousCountedAssetsByJobId = countedHistoryAssetsByJobId.value
55 1 : const nextCountedAssetsByJobId = new Map<string, number>()
56 :
57 3 : for (const task of historyTasks) {
58 0 : const jobId = task.jobId
59 0 : if (!jobId) {
60 0 : continue
61 : }
62 :
63 0 : const countedAssets = previousCountedAssetsByJobId.get(jobId) ?? 0
64 0 : const currentAssets = getAddedAssetCount(task)
65 0 : const hasSeenJob = previousCountedAssetsByJobId.has(jobId)
66 :
67 0 : if (!isAssetsTabOpen && !hasSeenJob) {
68 0 : unseenAddedAssetsCount.value += currentAssets
69 0 : } else if (!isAssetsTabOpen && currentAssets > countedAssets) {
70 0 : unseenAddedAssetsCount.value += currentAssets - countedAssets
71 : }
72 :
73 0 : nextCountedAssetsByJobId.set(
74 : jobId,
75 : Math.max(countedAssets, currentAssets)
76 : )
77 : }
78 :
79 1 : countedHistoryAssetsByJobId.value = nextCountedAssetsByJobId
80 : },
81 : { immediate: true }
82 : )
83 :
84 12 : watch(
85 : () => sidebarTabStore.activeSidebarTabId,
86 : (activeSidebarTabId) => {
87 7 : if (activeSidebarTabId !== 'assets') {
88 7 : return
89 : }
90 :
91 0 : unseenAddedAssetsCount.value = 0
92 0 : markCurrentHistoryAsSeen()
93 : }
94 : )
95 :
96 12 : return {
97 : unseenAddedAssetsCount
98 : }
99 : }
100 : )
|