|
| 1 | +/* Copyright AGNTCY Contributors (https://github.com/agntcy) */ |
| 2 | +/* SPDX-License-Identifier: Apache-2.0 */ |
| 3 | + |
| 4 | +/* RepoBeats-style GitHub activity panel (data from slim-repobeats-data.js). */ |
| 5 | +document$.subscribe(function () { |
| 6 | + document.querySelectorAll("[data-slim-repobeats]").forEach(function (root) { |
| 7 | + if (root.dataset.slimRepobeatsInit === "true") { |
| 8 | + return; |
| 9 | + } |
| 10 | + root.dataset.slimRepobeatsInit = "true"; |
| 11 | + |
| 12 | + var repo = root.dataset.repo || "agntcy/slim"; |
| 13 | + var panel = root.querySelector(".slim-repobeats"); |
| 14 | + if (!panel) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + var data = window.__SLIM_REPOBEATS_DATA__; |
| 19 | + if (!data) { |
| 20 | + showRepobeatsError(panel, repo); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if (data.embedId) { |
| 25 | + panel.innerHTML = |
| 26 | + '<img class="slim-repobeats__embed" src="https://repobeats.axiom.co/api/embed/' + |
| 27 | + encodeURIComponent(data.embedId) + |
| 28 | + '.svg" alt="GitHub repository metrics" decoding="async" loading="lazy" width="814" height="318" />'; |
| 29 | + panel.removeAttribute("aria-busy"); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + renderRepobeatsPanel(panel, data); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +function showRepobeatsError(panel, repo) { |
| 38 | + panel.innerHTML = |
| 39 | + '<p class="slim-repobeats__error">Could not load repository metrics. ' + |
| 40 | + '<a href="https://github.com/' + |
| 41 | + repo + |
| 42 | + '/pulse" target="_blank" rel="noopener noreferrer">View activity on GitHub</a>.</p>'; |
| 43 | + panel.removeAttribute("aria-busy"); |
| 44 | +} |
| 45 | + |
| 46 | +function renderRepobeatsPanel(panel, data) { |
| 47 | + var recentCommits = data.recentCommits; |
| 48 | + if (!recentCommits && data.commitBars && data.commitBars.length) { |
| 49 | + recentCommits = data.commitBars.slice(-5).reduce(function (sum, value) { |
| 50 | + return sum + value; |
| 51 | + }, 0); |
| 52 | + } |
| 53 | + |
| 54 | + panel.innerHTML = |
| 55 | + renderSummaryRow( |
| 56 | + "commits", |
| 57 | + recentCommits + " Contributions in the Last 30 Days", |
| 58 | + data.commitBars || [], |
| 59 | + "rgba(236, 72, 153, 1)" |
| 60 | + ) + |
| 61 | + renderSummaryRow( |
| 62 | + "prs", |
| 63 | + data.openPrs + " Pull Requests Opened", |
| 64 | + data.commitBars || [], |
| 65 | + "rgba(123, 88, 201, 1)" |
| 66 | + ) + |
| 67 | + '<div class="slim-repobeats__split">' + |
| 68 | + renderSplitCard( |
| 69 | + "issues", |
| 70 | + "Issues", |
| 71 | + data.issuesOpened + " opened", |
| 72 | + data.issuesClosed + " closed", |
| 73 | + data.issueWeeks || [], |
| 74 | + "rgba(64, 108, 196, 1)", |
| 75 | + "rgba(34, 197, 94, 1)" |
| 76 | + ) + |
| 77 | + renderSplitCard( |
| 78 | + "pulls", |
| 79 | + "Pull Requests", |
| 80 | + data.prsOpened + " opened", |
| 81 | + data.prsMerged + " merged", |
| 82 | + data.prWeeks || [], |
| 83 | + "rgba(123, 88, 201, 1)", |
| 84 | + "rgba(34, 197, 94, 1)" |
| 85 | + ) + |
| 86 | + "</div>" + |
| 87 | + renderContributors(data.contributors || [], data.repo || "agntcy/slim"); |
| 88 | + panel.removeAttribute("aria-busy"); |
| 89 | +} |
| 90 | + |
| 91 | +function renderSummaryRow(kind, title, values, color) { |
| 92 | + return ( |
| 93 | + '<div class="slim-repobeats__row slim-repobeats__row--' + |
| 94 | + kind + |
| 95 | + '">' + |
| 96 | + '<div class="slim-repobeats__row-head">' + |
| 97 | + '<span class="slim-repobeats__icon slim-repobeats__icon--' + |
| 98 | + kind + |
| 99 | + '" style="color:' + |
| 100 | + color + |
| 101 | + '"></span>' + |
| 102 | + '<span class="slim-repobeats__title">' + |
| 103 | + escapeHtml(title) + |
| 104 | + "</span>" + |
| 105 | + "</div>" + |
| 106 | + renderBars(values, color) + |
| 107 | + "</div>" |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function renderSplitCard(kind, label, opened, closed, values, openColor, closedColor) { |
| 112 | + return ( |
| 113 | + '<div class="slim-repobeats__card slim-repobeats__card--' + |
| 114 | + kind + |
| 115 | + '">' + |
| 116 | + '<div class="slim-repobeats__card-head">' + |
| 117 | + '<span class="slim-repobeats__card-label">' + |
| 118 | + escapeHtml(label) + |
| 119 | + "</span>" + |
| 120 | + "</div>" + |
| 121 | + '<div class="slim-repobeats__card-stats">' + |
| 122 | + '<span class="slim-repobeats__card-stat" style="color:' + |
| 123 | + openColor + |
| 124 | + '">' + |
| 125 | + escapeHtml(opened) + |
| 126 | + "</span>" + |
| 127 | + '<span class="slim-repobeats__card-stat" style="color:' + |
| 128 | + closedColor + |
| 129 | + '">' + |
| 130 | + escapeHtml(closed) + |
| 131 | + "</span>" + |
| 132 | + "</div>" + |
| 133 | + renderBars(values, openColor, "slim-repobeats__bars--compact") + |
| 134 | + "</div>" |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +function renderContributors(contributors, repo) { |
| 139 | + var maxVisible = 12; |
| 140 | + |
| 141 | + if (!contributors.length) { |
| 142 | + return ( |
| 143 | + '<div class="slim-repobeats__contributors">' + |
| 144 | + '<span class="slim-repobeats__contributors-label">Top contributors</span>' + |
| 145 | + '<img class="slim-repobeats__contrib" src="https://contrib.rocks/image?repo=' + |
| 146 | + encodeURIComponent(repo) + |
| 147 | + "&max=" + |
| 148 | + maxVisible + |
| 149 | + "&columns=" + |
| 150 | + maxVisible + |
| 151 | + '" alt="Top contributors to ' + |
| 152 | + repo + |
| 153 | + '" decoding="async" loading="lazy" />' + |
| 154 | + "</div>" |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + var items = contributors |
| 159 | + .slice(0, maxVisible) |
| 160 | + .map(function (contributor) { |
| 161 | + return ( |
| 162 | + '<img class="slim-repobeats__avatar" src="' + |
| 163 | + escapeHtml(contributor.avatar_url) + |
| 164 | + '" alt="' + |
| 165 | + escapeHtml(contributor.login) + |
| 166 | + '" title="' + |
| 167 | + escapeHtml(contributor.login) + |
| 168 | + '" loading="lazy" width="32" height="32" />' |
| 169 | + ); |
| 170 | + }) |
| 171 | + .join(""); |
| 172 | + |
| 173 | + return ( |
| 174 | + '<div class="slim-repobeats__contributors">' + |
| 175 | + '<span class="slim-repobeats__contributors-label">Top contributors</span>' + |
| 176 | + '<div class="slim-repobeats__contributor-list">' + |
| 177 | + items + |
| 178 | + "</div>" + |
| 179 | + "</div>" |
| 180 | + ); |
| 181 | +} |
| 182 | + |
| 183 | +function renderBars(values, color, extraClass) { |
| 184 | + var bars = normalizeBars(values) |
| 185 | + .map(function (level) { |
| 186 | + return ( |
| 187 | + '<span class="slim-repobeats__bar" style="--level:' + |
| 188 | + level + |
| 189 | + ";--bar-color:" + |
| 190 | + color + |
| 191 | + '"></span>' |
| 192 | + ); |
| 193 | + }) |
| 194 | + .join(""); |
| 195 | + |
| 196 | + return ( |
| 197 | + '<div class="slim-repobeats__bars' + |
| 198 | + (extraClass ? " " + extraClass : "") + |
| 199 | + '" aria-hidden="true">' + |
| 200 | + bars + |
| 201 | + "</div>" |
| 202 | + ); |
| 203 | +} |
| 204 | + |
| 205 | +function normalizeBars(values) { |
| 206 | + var max = 0; |
| 207 | + values.forEach(function (value) { |
| 208 | + if (value > max) { |
| 209 | + max = value; |
| 210 | + } |
| 211 | + }); |
| 212 | + if (!max) { |
| 213 | + return values.map(function () { |
| 214 | + return 0.08; |
| 215 | + }); |
| 216 | + } |
| 217 | + return values.map(function (value) { |
| 218 | + return Math.max(0.08, value / max); |
| 219 | + }); |
| 220 | +} |
| 221 | + |
| 222 | +function escapeHtml(value) { |
| 223 | + return String(value) |
| 224 | + .replace(/&/g, "&") |
| 225 | + .replace(/</g, "<") |
| 226 | + .replace(/>/g, ">") |
| 227 | + .replace(/"/g, """); |
| 228 | +} |
0 commit comments