11import { useAsyncState } from '@vueuse/core'
22import { defineStore } from 'pinia'
3+ import { ref } from 'vue'
34
45import {
56 mapInputFileToAssetItem ,
@@ -79,8 +80,13 @@ function mapHistoryToAssets(historyItems: any[]): AssetItem[] {
7980 )
8081}
8182
83+ const BATCH_SIZE = 200
84+
8285export const useAssetsStore = defineStore ( 'assets' , ( ) => {
83- const maxHistoryItems = 200
86+ const historyOffset = ref ( 0 )
87+ const hasMoreHistory = ref ( true )
88+ const isLoadingMore = ref ( false )
89+ const allHistoryItems = ref < AssetItem [ ] > ( [ ] )
8490
8591 const fetchInputFiles = isCloud
8692 ? fetchInputFilesFromCloud
@@ -99,23 +105,66 @@ export const useAssetsStore = defineStore('assets', () => {
99105 }
100106 } )
101107
102- const fetchHistoryAssets = async ( ) : Promise < AssetItem [ ] > => {
103- const history = await api . getHistory ( maxHistoryItems )
104- return mapHistoryToAssets ( history . History )
108+ const fetchHistoryAssets = async ( loadMore = false ) : Promise < AssetItem [ ] > => {
109+ if ( ! loadMore ) {
110+ historyOffset . value = 0
111+ hasMoreHistory . value = true
112+ allHistoryItems . value = [ ]
113+ }
114+
115+ const history = await api . getHistory ( BATCH_SIZE , {
116+ offset : historyOffset . value
117+ } )
118+ const newAssets = mapHistoryToAssets ( history . History )
119+
120+ if ( loadMore ) {
121+ const existingIds = new Set ( allHistoryItems . value . map ( ( item ) => item . id ) )
122+ const uniqueNewAssets = newAssets . filter (
123+ ( item ) => ! existingIds . has ( item . id )
124+ )
125+ allHistoryItems . value = [ ...allHistoryItems . value , ...uniqueNewAssets ]
126+ } else {
127+ allHistoryItems . value = newAssets
128+ }
129+
130+ hasMoreHistory . value = newAssets . length === BATCH_SIZE
131+ historyOffset . value += newAssets . length
132+
133+ return allHistoryItems . value
105134 }
106135
107- const {
108- state : historyAssets ,
109- isLoading : historyLoading ,
110- error : historyError ,
111- execute : updateHistory
112- } = useAsyncState ( fetchHistoryAssets , [ ] , {
113- immediate : false ,
114- resetOnExecute : false ,
115- onError : ( err ) => {
136+ const historyAssets = ref < AssetItem [ ] > ( [ ] )
137+ const historyLoading = ref ( false )
138+ const historyError = ref < unknown > ( null )
139+
140+ const updateHistory = async ( ) => {
141+ historyLoading . value = true
142+ historyError . value = null
143+ try {
144+ const assets = await fetchHistoryAssets ( false )
145+ historyAssets . value = assets
146+ } catch ( err ) {
116147 console . error ( 'Error fetching history assets:' , err )
148+ historyError . value = err
149+ } finally {
150+ historyLoading . value = false
117151 }
118- } )
152+ }
153+
154+ const loadMoreHistory = async ( ) => {
155+ if ( ! hasMoreHistory . value || isLoadingMore . value ) return
156+
157+ isLoadingMore . value = true
158+ try {
159+ const updatedAssets = await fetchHistoryAssets ( true )
160+ historyAssets . value = updatedAssets
161+ } catch ( err ) {
162+ console . error ( 'Error loading more history:' , err )
163+ historyError . value = err
164+ } finally {
165+ isLoadingMore . value = false
166+ }
167+ }
119168
120169 return {
121170 // States
@@ -125,9 +174,12 @@ export const useAssetsStore = defineStore('assets', () => {
125174 historyLoading,
126175 inputError,
127176 historyError,
177+ hasMoreHistory,
178+ isLoadingMore,
128179
129180 // Actions
130181 updateInputs,
131- updateHistory
182+ updateHistory,
183+ loadMoreHistory
132184 }
133185} )
0 commit comments