-
Notifications
You must be signed in to change notification settings - Fork 17
/
store.js
125 lines (111 loc) · 3.94 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const persist = require('vuex-persistedstate');
const Vue = require('vue/dist/vue');
const Vuex = require('vuex');
const { REPOSITORY_COLORS, THEMES } = require('./constants');
Vue.use(Vuex);
const findRepository = (repository, repositories) => {
return repositories.findIndex(candidate => candidate.path === repository.path);
}
const addRepository = (repository, repositories) => {
let index = findRepository(repository, repositories);
if (index > -1) {
repositories.splice(index, 1);
}
repository.color = Math.floor(Math.random() * REPOSITORY_COLORS.length);
repositories.push(repository);
return repositories;
}
const getAccessScore = (repository) => {
// Score the repository based on how frequently and how recently it was accessed.
// Start with the accessCount and divide by number of weeks of inactivity.
// Note: we use weeks + 1 so score decays immediately and we don't overly boost recent items.
if (!repository.accessCount || !repository.accessTime) return 0
let score = repository.accessCount
let weeksInactive = Date.now() - repository.accessTime / (7 * 24 * 60 * 60 * 1000);
return score / (weeksInactive + 1)
}
const store = new Vuex.Store({
state: {
activeRepository: null,
repositories: [],
showCommitDialog: false,
theme: THEMES[0]
},
mutations: {
activateRepository (state, repository) {
let repositories = state.repositories;
let index = findRepository(repository, repositories);
if (index === -1) return;
state.activeRepository = repository;
repository = repositories[index];
repository.accessCount = isNaN(repository.accessCount) ? 1 : repository.accessCount + 1;
repository.accessTime = Date.now();
Vue.set(state.repositories, index, repository);
},
addRepository (state, repository) {
state.repositories = addRepository(repository, state.repositories);
},
removeRepository (state, repository) {
let repositories = state.repositories;
let index = findRepository(repository, repositories);
if (index === -1) return;
repository = repositories[index];
repository.removed = true;
Vue.set(state.repositories, index, repository);
let activeRepository = state.activeRepository;
if (activeRepository && activeRepository.path === repository.path) {
state.activeRepository = null;
}
},
showCommitDialog (state, show) {
state.showCommitDialog = show;
},
setRepositories (state, found) {
let repositories = state.repositories;
for (let repository of found) {
if (findRepository(repository, repositories) > -1) continue;
repositories = addRepository(repository, repositories);
}
state.repositories = repositories;
},
setStatusCount (state, { count, repository }) {
let repositories = state.repositories;
let index = findRepository(repository, repositories);
if (index === -1) return;
repository = repositories[index];
repository.statusCount = count;
Vue.set(state.repositories, index, repository);
},
setTheme (state, theme) {
state.theme = theme;
}
},
getters: {
repositories: state => {
let repositories = state.repositories;
// Filter out deleted repos
repositories = repositories.filter(repository => !repository.removed)
// Normalize repositories to always contain the basic properties we expect
return state.repositories.map(repository => Object.assign({
accessCount: 0,
accessTime: 0,
statusCount: 0
}, repository));
},
sortedRepositories: (state, getters) => {
return getters.repositories.sort((a, b) => {
let aScore = getAccessScore(a);
let bScore = getAccessScore(b);
if (aScore !== bScore) {
return bScore - aScore;
}
return a.name.localeCompare(b.name);
})
}
},
plugins: [
persist()
]
});
module.exports = store