@@ -4,7 +4,7 @@ import { HISTORY_SORT_BY_VALUES } from '../../../constants'
4
4
5
5
const state = {
6
6
historyCacheSorted : [ ] ,
7
- useAscendingOrder : HISTORY_SORT_BY_VALUES . DateAddedNewest ,
7
+ sortOrder : HISTORY_SORT_BY_VALUES . DateAddedNewest ,
8
8
9
9
// Vuex doesn't support Maps, so we have to use an object here instead
10
10
// TODO: switch to a Map during the Pinia migration
@@ -21,7 +21,7 @@ const getters = {
21
21
} ,
22
22
23
23
getHistorySortOrder ( state ) {
24
- return state . useAscendingOrder
24
+ return state . sortOrder
25
25
}
26
26
}
27
27
@@ -116,7 +116,7 @@ const mutations = {
116
116
} ,
117
117
118
118
setHistorySortOrder ( state , order ) {
119
- state . useAscendingOrder = order
119
+ state . sortOrder = order
120
120
} ,
121
121
122
122
setHistoryCacheById ( state , historyCacheById ) {
@@ -134,7 +134,19 @@ const mutations = {
134
134
state . historyCacheSorted . splice ( i , 1 )
135
135
}
136
136
137
- state . historyCacheSorted . unshift ( record )
137
+ // Correct position to add according to the sort order
138
+ const insertIndex = state . historyCacheSorted . findIndex ( ( currentRecord ) =>
139
+ state . sortOrder === HISTORY_SORT_BY_VALUES . DateAddedOldest
140
+ ? record . timeWatched < currentRecord . timeWatched
141
+ : record . timeWatched > currentRecord . timeWatched
142
+ )
143
+
144
+ if ( insertIndex === - 1 ) {
145
+ state . historyCacheSorted . push ( record )
146
+ } else {
147
+ state . historyCacheSorted . splice ( insertIndex , 0 , record )
148
+ }
149
+
138
150
vueSet ( state . historyCacheById , record . videoId , record )
139
151
} ,
140
152
@@ -177,7 +189,7 @@ const mutations = {
177
189
178
190
sortHistory ( state ) {
179
191
state . historyCacheSorted . sort ( ( a , b ) =>
180
- state . useAscendingOrder === HISTORY_SORT_BY_VALUES . DateAddedOldest
192
+ state . sortOrder === HISTORY_SORT_BY_VALUES . DateAddedOldest
181
193
? a . timeWatched - b . timeWatched
182
194
: b . timeWatched - a . timeWatched
183
195
)
0 commit comments