|
| 1 | +<template> |
| 2 | + <table> |
| 3 | + <thead> |
| 4 | + <tr> |
| 5 | + <th>{{ labels.language }}</th> |
| 6 | + <th>{{ labels.github }}</th> |
| 7 | + <th>{{ labels.lastCommit }}</th> |
| 8 | + <th>{{ labels.last90Days }}</th> |
| 9 | + </tr> |
| 10 | + </thead> |
| 11 | + <tbody> |
| 12 | + <tr v-for="({ lang, owner, repo, url, count, date }, index) in merged"> |
| 13 | + <td> |
| 14 | + <a v-if="url" :href="url" target="_blank"> |
| 15 | + {{ lang }} <OutboundLink /> |
| 16 | + </a> |
| 17 | + <template v-else> |
| 18 | + {{ lang }} |
| 19 | + </template> |
| 20 | + </td> |
| 21 | + <td> |
| 22 | + <a :href="`https://github.com/${owner}/${repo}/`" target="_blank">{{ owner }}/{{ repo }} <OutboundLink /></a> |
| 23 | + </td> |
| 24 | + <template v-if="showLoadButton"> |
| 25 | + <td v-if="index === 0" colspan="2" :rowspan="merged.length" class="load-cell"> |
| 26 | + <button @click="load">{{ labels.loadDetails }}</button> |
| 27 | + </td> |
| 28 | + </template> |
| 29 | + <template v-else> |
| 30 | + <td>{{ date }}</td> |
| 31 | + <td> |
| 32 | + {{ count }}{{ count === 100 ? '+' : '' }} |
| 33 | + <template v-if="typeof count === 'number'"> |
| 34 | + {{ labels.commits }} |
| 35 | + </template> |
| 36 | + </td> |
| 37 | + </template> |
| 38 | + </tr> |
| 39 | + </tbody> |
| 40 | + </table> |
| 41 | +</template> |
| 42 | + |
| 43 | +<script> |
| 44 | +import { labels, repos } from './translations-data.js' |
| 45 | +
|
| 46 | +const getLastDate = async ({ owner, repo, branch }) => { |
| 47 | + const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/branches/${branch}`) |
| 48 | +
|
| 49 | + const data = await response.json() |
| 50 | +
|
| 51 | + const dateTime = data.commit.commit.committer.date |
| 52 | +
|
| 53 | + return dateTime.split('T')[0] |
| 54 | +} |
| 55 | +
|
| 56 | +const DATE_90_DAYS_AGO = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000).toJSON().replace(/\.\d*/, '') |
| 57 | +
|
| 58 | +const commitCount = async ({ owner, repo }) => { |
| 59 | + const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/commits?since=${DATE_90_DAYS_AGO}&per_page=100`) |
| 60 | +
|
| 61 | + const data = await response.json() |
| 62 | +
|
| 63 | + return data.length |
| 64 | +} |
| 65 | +
|
| 66 | +export default { |
| 67 | + name: 'translations', |
| 68 | +
|
| 69 | + data () { |
| 70 | + const dates = {} |
| 71 | +
|
| 72 | + for (const { lang } of repos) { |
| 73 | + dates[lang] = null |
| 74 | + } |
| 75 | +
|
| 76 | + return { |
| 77 | + showLoadButton: true, |
| 78 | + dates, |
| 79 | + counts: { ...dates }, |
| 80 | + labels, |
| 81 | + repos |
| 82 | + } |
| 83 | + }, |
| 84 | +
|
| 85 | + computed: { |
| 86 | + merged () { |
| 87 | + return this.repos.map(repo => ({ |
| 88 | + ...repo, |
| 89 | + date: this.dates[repo.lang], |
| 90 | + count: this.counts[repo.lang] |
| 91 | + })) |
| 92 | + } |
| 93 | + }, |
| 94 | +
|
| 95 | + mounted () { |
| 96 | + let stats = null |
| 97 | +
|
| 98 | + try { |
| 99 | + stats = JSON.parse(sessionStorage.getItem('translation-stats')) |
| 100 | + } catch { |
| 101 | + } |
| 102 | +
|
| 103 | + if (!stats) { |
| 104 | + return |
| 105 | + } |
| 106 | +
|
| 107 | + const { date, counts, dates } = stats |
| 108 | +
|
| 109 | + if (!date || date + 3 * 60 * 60 * 1000 < Date.now()) { |
| 110 | + return |
| 111 | + } |
| 112 | +
|
| 113 | + this.dates = dates |
| 114 | + this.counts = counts |
| 115 | + this.showLoadButton = false |
| 116 | + }, |
| 117 | +
|
| 118 | + methods: { |
| 119 | + async load () { |
| 120 | + this.showLoadButton = false |
| 121 | +
|
| 122 | + await Promise.all(this.repos.map(this.loadRepo)) |
| 123 | +
|
| 124 | + this.saveStats() |
| 125 | + }, |
| 126 | +
|
| 127 | + async loadRepo (repo) { |
| 128 | + await this.loadLastCommit(repo) |
| 129 | +
|
| 130 | + return this.loadCommitCount(repo) |
| 131 | + }, |
| 132 | +
|
| 133 | + async loadLastCommit (repo) { |
| 134 | + const { lang } = repo |
| 135 | +
|
| 136 | + this.dates[lang] = labels.loading |
| 137 | +
|
| 138 | + let date = '-' |
| 139 | +
|
| 140 | + try { |
| 141 | + date = await getLastDate(repo) |
| 142 | + } catch { |
| 143 | + } |
| 144 | +
|
| 145 | + this.dates[lang] = date |
| 146 | + }, |
| 147 | +
|
| 148 | + async loadCommitCount (repo) { |
| 149 | + const { lang } = repo |
| 150 | +
|
| 151 | + const date = this.dates[lang] |
| 152 | + let count = 0 |
| 153 | +
|
| 154 | + if (date === '-') { |
| 155 | + count = '-' |
| 156 | + } else if (date > DATE_90_DAYS_AGO) { |
| 157 | + this.counts[lang] = labels.loading |
| 158 | +
|
| 159 | + try { |
| 160 | + count = await commitCount(repo) |
| 161 | + } catch { |
| 162 | + count = '-' |
| 163 | + } |
| 164 | + } |
| 165 | +
|
| 166 | + this.counts[lang] = count |
| 167 | + }, |
| 168 | +
|
| 169 | + saveStats () { |
| 170 | + const data = { |
| 171 | + date: Date.now(), |
| 172 | + counts: this.counts, |
| 173 | + dates: this.dates |
| 174 | + } |
| 175 | +
|
| 176 | + // GitHub limits request rates, so we store the stats in sessionStorage |
| 177 | + sessionStorage.setItem('translation-stats', JSON.stringify(data)) |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +</script> |
| 182 | + |
| 183 | +<style lang="scss" scoped> |
| 184 | +@import "@theme/styles/_settings.scss"; |
| 185 | +
|
| 186 | +.load-cell { |
| 187 | + background: #f6f8fa; |
| 188 | + text-align: center; |
| 189 | +
|
| 190 | + button { |
| 191 | + padding: 4px 20px; |
| 192 | + height: 50px; |
| 193 | + border-radius: 9999px; |
| 194 | + font-size: 1.05em; |
| 195 | + font-weight: 600; |
| 196 | + letter-spacing: 0.1em; |
| 197 | + min-width: 8em; |
| 198 | + color: #fff; |
| 199 | + background-color: $green; |
| 200 | + box-sizing: border-box; |
| 201 | + border: 1px solid currentColor; |
| 202 | + appearance: none; |
| 203 | + cursor: pointer; |
| 204 | + } |
| 205 | +} |
| 206 | +</style> |
0 commit comments