Skip to content

Commit 6f10c83

Browse files
committed
Fixed new history video sort order according to sort filter
1 parent 954f1fb commit 6f10c83

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/renderer/store/modules/history.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HISTORY_SORT_BY_VALUES } from '../../../constants'
44

55
const state = {
66
historyCacheSorted: [],
7-
useAscendingOrder: HISTORY_SORT_BY_VALUES.DateAddedNewest,
7+
sortOrder: HISTORY_SORT_BY_VALUES.DateAddedNewest,
88

99
// Vuex doesn't support Maps, so we have to use an object here instead
1010
// TODO: switch to a Map during the Pinia migration
@@ -21,7 +21,7 @@ const getters = {
2121
},
2222

2323
getHistorySortOrder(state) {
24-
return state.useAscendingOrder
24+
return state.sortOrder
2525
}
2626
}
2727

@@ -116,7 +116,7 @@ const mutations = {
116116
},
117117

118118
setHistorySortOrder(state, order) {
119-
state.useAscendingOrder = order
119+
state.sortOrder = order
120120
},
121121

122122
setHistoryCacheById(state, historyCacheById) {
@@ -134,7 +134,19 @@ const mutations = {
134134
state.historyCacheSorted.splice(i, 1)
135135
}
136136

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+
138150
vueSet(state.historyCacheById, record.videoId, record)
139151
},
140152

@@ -177,7 +189,7 @@ const mutations = {
177189

178190
sortHistory(state) {
179191
state.historyCacheSorted.sort((a, b) =>
180-
state.useAscendingOrder === HISTORY_SORT_BY_VALUES.DateAddedOldest
192+
state.sortOrder === HISTORY_SORT_BY_VALUES.DateAddedOldest
181193
? a.timeWatched - b.timeWatched
182194
: b.timeWatched - a.timeWatched
183195
)

0 commit comments

Comments
 (0)