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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<template>
<button v-if="!authorized()" @click="login">
<i class="fab fa-github" /> Login with GitHub
</button>
<span v-else>
<selector
id="repo"
label="Repository"
:choices="repositoryChoices"
v-model="repo"
/>
<spinner v-if="loadingBranches" />
<selector
v-else
v-model="branch"
id="branch"
label="Branch"
:choices="branchChoices"
/>
<spinner v-if="loadingKeyboard" />
<invalid-repo
v-if="loadKeyboardError === 'InvalidRepoError'"
:otherRepoOrBranchAvailable="repositoryChoices.length > 0 || branchChoices.length > 0"
@dismiss="clearSelection"
/>
</span>
</template>
<script>
import find from 'lodash/find'
import map from 'lodash/map'
import * as github from './api'
import * as storage from './storage'
import InvalidRepo from './invalid-repo.vue'
import Selector from '../selector.vue'
import Spinner from '../spinner.vue'
export default {
name: 'GithubPicker',
components: { InvalidRepo, Selector, Spinner },
emits: ['select'],
data() {
return {
repo: null,
branch: null,
branches: [],
loadingBranches: false,
loadingKeyboard: false,
loadKeyboardError: null
}
},
mounted() {
if (!this.authorized()) {
return
}
const selectedRepository = storage.getPersistedRepository()
if (github.repositories.length === 1) {
this.repo = github.repositories[0].full_name
this.loadBranches()
} else if (selectedRepository && github.repositories.find(repo => repo.full_name === selectedRepository)) {
this.repo = selectedRepository
this.loadBranches()
}
},
watch: {
repo(value) {
storage.setPersistedRepository(value)
if (value) {
this.loadBranches()
}
},
branch(value) {
storage.setPersistedBranch(value)
if (value) {
this.loadKeyboard()
}
}
},
methods: {
authorized() {
return github.isGitHubAuthorized()
},
login() {
github.beginLoginFlow()
},
getRepositories() {
return github.repositories
},
async loadBranches() {
this.loadingBranches = true
this.branches = []
const repository = github.repositories.find(repo => repo.full_name === this.repo)
const branches = await github.fetchRepoBranches(repository)
this.loadingBranches = false
this.branches = branches
const available = map(branches, 'name')
const defaultBranch = repository.default_branch
const currentBranch = this.branch
const previousBranch = storage.getPersistedBranch()
const onlyBranch = branches.length === 1 ? branches[0].name : null
for (let branch of [onlyBranch, currentBranch, previousBranch, defaultBranch]) {
if (available.includes(branch)) {
this.branch = branch
break
}
}
},
async loadKeyboard() {
const available = this.getRepositories()
const repository = find(available, { full_name: this.repo })?.full_name
const branch = this.branch
this.loadingKeyboard = true
this.loadKeyboardError = null
const response = await github.fetchLayoutAndKeymap(repository, branch)
this.loadingKeyboard = false
if (response.error) {
this.loadKeyboardError = response.error
return
}
this.$emit('select', { github: { repository, branch }, ...response })
},
clearSelection() {
this.branch = null
this.loadKeyboardError = null
}
},
computed: {
repositoryChoices() {
return this.getRepositories().map(repo => ({
id: repo.full_name,
name: repo.full_name
}))
},
branchChoices() {
return this.branches.map(branch => ({
id: branch.name,
name: branch.name
}))
}
}
}
</script>
|