@@ -70,6 +70,72 @@ export function mapDavShareeToSharee(sharee) {
70
70
}
71
71
}
72
72
73
+ /**
74
+ * Sorts addressbooks by rules:
75
+ * 1. First group: Default personal addressbook ("contacts")
76
+ * 2. Second group: Recently used (based on lastUsedAddressBooks) go second, ordered from newest to oldest
77
+ * 3. Third group: Writeable enabled contacts, sorted by the amount of contacts they have, from more to less
78
+ * 4. Fourth group: Read-only enabled contacts, sorted by the amount of contacts
79
+ * 5. Fifth group: Disabled contacts, sorted by the amount of contacts
80
+ *
81
+ * @param {Array } addressbooks
82
+ * @return {Array }
83
+ */
84
+ export function sortAddressbooks ( addressbooks ) {
85
+ const lastUsed = lastUsedAddressBooks ( addressbooks )
86
+
87
+ return addressbooks
88
+ . slice ( )
89
+ . sort ( ( a , b ) => {
90
+ const getContactCount = ( ab ) => Object . keys ( ab . contacts || { } ) . length
91
+
92
+ const getPriorityGroup = ( ab ) => {
93
+ if ( ab . id === 'contacts' ) return 0
94
+ if ( ab . enabled === false ) return 4
95
+ if ( lastUsed . includes ( ab . id ) ) return 1
96
+ if ( ab . readOnly ) return 3
97
+ return 2
98
+ }
99
+
100
+ const groupA = getPriorityGroup ( a )
101
+ const groupB = getPriorityGroup ( b )
102
+
103
+ // First, sort by priority group
104
+ if ( groupA !== groupB ) {
105
+ return groupA - groupB
106
+ }
107
+
108
+ // Within the same group, apply specific sorting rules
109
+ if ( groupA === 0 ) {
110
+ return 0
111
+ } else if ( groupA === 1 ) {
112
+ // Sort by position in lastUsed array (newest first)
113
+ return lastUsed . indexOf ( a . id ) - lastUsed . indexOf ( b . id )
114
+ } else { // Groups 2, 3, 4 - sort by contact count (descending)
115
+ const countA = getContactCount ( a )
116
+ const countB = getContactCount ( b )
117
+
118
+ if ( countA !== countB ) {
119
+ return countB - countA
120
+ }
121
+
122
+ // If contact counts are equal, sort alphabetically by ID as tiebreaker
123
+ return a . id . localeCompare ( b . id )
124
+ }
125
+ } )
126
+ }
127
+
128
+ /**
129
+ *
130
+ */
131
+ function lastUsedAddressBooks ( ) {
132
+ const accesses = JSON . parse ( localStorage . getItem ( 'addressbook-accesses' ) || '{}' )
133
+
134
+ return Object . entries ( accesses )
135
+ . sort ( ( [ , a ] , [ , b ] ) => new Date ( b ) - new Date ( a ) )
136
+ . map ( ( [ id ] ) => id )
137
+ }
138
+
73
139
const mutations = {
74
140
75
141
/**
@@ -218,6 +284,14 @@ const mutations = {
218
284
sharee . writeable = ! sharee . writeable
219
285
} ,
220
286
287
+ /**
288
+ * Needed to track indirect state changes for addressbook sorting
289
+ *
290
+ * @param state
291
+ */
292
+ resortAddressbooks ( state ) {
293
+ state . addressbooks = sortAddressbooks ( state . addressbooks )
294
+ } ,
221
295
}
222
296
223
297
const getters = {
@@ -246,6 +320,8 @@ const actions = {
246
320
context . commit ( 'addAddressbook' , addressbook )
247
321
} )
248
322
323
+ context . commit ( 'resortAddressbooks' )
324
+
249
325
return addressbooks
250
326
} ,
251
327
0 commit comments