aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/components/github/picker.vue
blob: d7027f9fee59b5c3940ca02f00775abe55675af0 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<template>
  <loader :inline="true" :load="initialize" @loaded="onInitialized">
    <template v-slot:loading>
      <spinner />
    </template>

    <button v-if="!isGitHubAuthorized()" @click="login">
      <i class="fab fa-github" /> Login with GitHub
    </button>

    <button v-else-if="!isAppInstalled()" @click="install">
      <i class="fab fa-github" /> Add Repository
    </button>

    <span v-else>
      <selector
        id="repo"
        label="Repository"
        :choices="repositoryChoices"
        v-model.number="repoId"
      />

      <spinner v-if="loadingBranches" />
      <selector
        v-else
        v-model="branchName"
        id="branch"
        label="Branch"
        :choices="branchChoices"
      />

      <spinner v-if="loadingKeyboard" />
      <validation-errors
        v-if="loadKeyboardError"
        :title="loadKeyboardError.name"
        :errors="loadKeyboardError.errors"
        :otherRepoOrBranchAvailable="repositoryChoices.length > 0 || branchChoices.length > 0"
        @dismiss="clearSelection"
      />
    </span>
  </loader>
</template>

<script>
import find from 'lodash/find'
import map from 'lodash/map'

import github from './api'
import * as storage from './storage'
import InvalidRepo from './invalid-repo.vue'
import ValidationErrors from './validation-errors.vue'
import Loader from '../loader.vue'
import Selector from '../selector.vue'
import Spinner from '../spinner.vue'

export default {
  name: 'GithubPicker',
  components: { InvalidRepo, Loader, Selector, Spinner, ValidationErrors },
  emits: ['select'],
  data() {
    return {
      repoId: null,
      branchName: null,
      branches: [],
      loadingBranches: false,
      loadingKeyboard: false,
      loadKeyboardError: null
    }
  },
  created() {
    github.on('authentication-failed', () => {
      github.beginLoginFlow()
    })
    github.on('repo-validation-error', err => {
      this.loadKeyboardError = err
      this.loadingKeyboard = false
    })
  },
  watch: {
    repoId(value) {
      this.branchName = null
      if (value) {
        storage.setPersistedRepository(value)
        this.loadBranches()
      }
    },
    branchName(value) {
      if (value) {
        storage.setPersistedBranch(this.repoId, value)
        this.loadKeyboard()
      }
    }
  },
  methods: {
    initialize() {
      // TODO: figure out the Vue equivalent of Higher Order Components so that
      // I can use lifecycle hooks properly.
      return github.init()
    },
    onInitialized() {
      const selectedRepository = storage.getPersistedRepository()
      const repositories = github.repositories || []

      if (repositories.length === 1) {
        this.repoId = repositories[0].id
        this.loadBranches()
      } else if (find(repositories, { id: selectedRepository })) {
        this.repoId = selectedRepository
        this.loadBranches()
      }
    },
    isGitHubAuthorized() {
      return github.isGitHubAuthorized()
    },
    isAppInstalled() {
      return github.isAppInstalled()
    },
    login() {
      github.beginLoginFlow()
    },
    install() {
      github.beginInstallAppFlow()
    },
    getRepositories() {
      return github.repositories
    },
    async loadBranches() {
      this.loadingBranches = true
      this.branches = []

      const repository = find(github.repositories, { id: this.repoId })
      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.branchName
      const previousBranch = storage.getPersistedBranch(this.repoId)
      const onlyBranch = branches.length === 1 ? branches[0].name : null

      for (let branch of [onlyBranch, currentBranch, previousBranch, defaultBranch]) {
        if (available.includes(branch)) {
          this.branchName = branch
          break
        }
      }
    },
    async loadKeyboard() {
      const available = this.getRepositories()
      const repository = find(available, { id: this.repoId })?.full_name
      const branch = this.branchName

      this.loadingKeyboard = true
      this.loadKeyboardError = null

      const response = await github.fetchLayoutAndKeymap(repository, branch)

      this.loadingKeyboard = false

      this.$emit('select', { github: { repository, branch }, ...response })
    },
    clearSelection() {
      this.branchName = null
      this.loadKeyboardError = null
    }
  },
  computed: {
    repositoryChoices() {
      return this.getRepositories().map(repo => ({
        id: repo.id,
        name: repo.full_name
      }))
    },
    branchChoices() {
      return this.branches.map(branch => ({
        id: branch.name,
        name: branch.name
      }))
    }
  }
}
</script>