-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgithub-stats.js
More file actions
163 lines (141 loc) · 5.15 KB
/
Copy pathgithub-stats.js
File metadata and controls
163 lines (141 loc) · 5.15 KB
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
// Widget: GitHub Stats - Fast cached version
window.DevMeWidgets = window.DevMeWidgets || {};
window.DevMeWidgets['github-stats'] = {
id: 'github-stats',
name: 'GitHub Stats',
description: 'Shows your GitHub repository statistics',
requires: ['githubUsername'],
refreshInterval: null,
username: null,
render() {
return `
<div class="box" data-widget="github-stats">
<div class="section-title">
GitHub Stats
<span class="cache-indicator" id="gh-cache-indicator" style="display:none;font-size:10px;opacity:0.6;margin-left:8px"></span>
</div>
<div class="stats-container">
<div class="stat-item" id="gh-repos">
<span class="stat-label">Public Repos</span>
<span class="stat-value">--</span>
</div>
<div class="stat-item" id="gh-stars">
<span class="stat-label">Total Stars</span>
<span class="stat-value">--</span>
</div>
<div class="stat-item" id="gh-forks">
<span class="stat-label">Total Forks</span>
<span class="stat-value">--</span>
</div>
<div class="stat-item" id="gh-top-language">
<span class="stat-label">Top Language</span>
<span class="stat-value">--</span>
</div>
</div>
</div>
`;
},
async init(container, config) {
this.username = config?.profile?.githubUsername;
if (!this.username) return;
const cacheKey = `github-stats-${this.username}`;
// Try sync cache first (instant)
if (window.widgetCache) {
const cached = window.widgetCache.getSync(cacheKey);
if (cached) {
this.updateUI(cached.data);
if (cached.isStale) {
this.refreshBackground(cacheKey);
}
} else {
this.fetchAndCache(cacheKey);
}
} else {
await this.fetchData();
}
// Auto-refresh every 5 minutes
this.refreshInterval = setInterval(() => {
this.refreshBackground(cacheKey);
}, 5 * 60 * 1000);
},
async fetchAndCache(cacheKey) {
try {
const data = await this.fetchFromAPI();
this.updateUI(data);
if (window.widgetCache) {
await window.widgetCache.set(cacheKey, data, 5 * 60 * 1000);
}
} catch (error) {
console.error('GitHub fetch failed:', error);
this.showError();
}
},
async refreshBackground(cacheKey) {
try {
const data = await this.fetchFromAPI();
this.updateUI(data);
if (window.widgetCache) {
await window.widgetCache.set(cacheKey, data, 5 * 60 * 1000);
}
} catch (error) {
console.warn('GitHub background refresh failed:', error);
}
},
async fetchFromAPI() {
const [userRes, reposRes] = await Promise.all([
fetch(`https://api.github.com/users/${this.username}`),
fetch(`https://api.github.com/users/${this.username}/repos?per_page=100`)
]);
if (!userRes.ok) throw new Error('GitHub user not found');
const userData = await userRes.json();
const reposData = await reposRes.json();
let totalStars = 0, totalForks = 0;
const languages = {};
reposData.forEach(repo => {
totalStars += repo.stargazers_count || 0;
totalForks += repo.forks_count || 0;
if (repo.language) {
languages[repo.language] = (languages[repo.language] || 0) + 1;
}
});
const topLanguage = Object.entries(languages)
.sort((a, b) => b[1] - a[1])
.map(([lang]) => lang)[0] || 'None';
return {
publicRepos: userData.public_repos || 0,
totalStars,
totalForks,
topLanguage
};
},
async fetchData() {
try {
const data = await this.fetchFromAPI();
this.updateUI(data);
} catch (error) {
this.showError();
}
},
updateUI(stats) {
this.updateStat('gh-repos', stats.publicRepos);
this.updateStat('gh-stars', stats.totalStars);
this.updateStat('gh-forks', stats.totalForks);
this.updateStat('gh-top-language', stats.topLanguage);
},
updateStat(id, value) {
const el = document.querySelector(`#${id} .stat-value`);
if (el) el.textContent = value;
},
showError() {
this.updateStat('gh-repos', '--');
this.updateStat('gh-stars', '--');
this.updateStat('gh-forks', '--');
this.updateStat('gh-top-language', '--');
},
destroy() {
if (this.refreshInterval) {
clearInterval(this.refreshInterval);
this.refreshInterval = null;
}
}
};