Skip to content

Commit 6391258

Browse files
committed
get stargazer from API
1 parent 61ca0e5 commit 6391258

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

src/views/Home.vue

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const mobileMenuOpen = ref<boolean>(false);
2222
const activeOS = ref<string>('arch');
2323
const setupStep = ref<number>(0);
2424
const isScrolled = ref<boolean>(false);
25+
const starCount = ref<string>('160+');
2526
2627
// --- CATPPUCCIN THEME LOGIC ---
2728
const catppuccinThemes = ['latte', 'frappe', 'macchiato', 'mocha'] as const;
@@ -66,6 +67,38 @@ const startTyping = () => {
6667
}, typingSpeed);
6768
};
6869
70+
const fetchGithubStars = async () => {
71+
const REPO = 'LotusInputMethod/fcitx5-lotus';
72+
const CACHE_KEY = 'lotus_stars_cache';
73+
const CACHE_TIME_KEY = 'lotus_stars_timestamp';
74+
const TWO_HOURS = 2 * 60 * 60 * 1000;
75+
76+
try {
77+
const cachedStars = localStorage.getItem(CACHE_KEY);
78+
const lastFetch = localStorage.getItem(CACHE_TIME_KEY);
79+
const now = Date.now();
80+
81+
if (cachedStars && lastFetch && now - parseInt(lastFetch) < TWO_HOURS) {
82+
starCount.value = cachedStars;
83+
return;
84+
}
85+
86+
const response = await fetch(`https://api.github.com/repos/${REPO}`);
87+
if (!response.ok) throw new Error('GitHub API rate limit or error');
88+
89+
const data = await response.json();
90+
const count = data.stargazers_count.toLocaleString();
91+
92+
starCount.value = count;
93+
localStorage.setItem(CACHE_KEY, count);
94+
localStorage.setItem(CACHE_TIME_KEY, now.toString());
95+
} catch (error) {
96+
console.error('Lỗi khi lấy star từ GitHub:', error);
97+
const oldCache = localStorage.getItem(CACHE_KEY);
98+
if (oldCache) starCount.value = oldCache;
99+
}
100+
};
101+
69102
onMounted(() => {
70103
const savedTheme = localStorage.getItem('catppuccin-theme');
71104
if (savedTheme && catppuccinThemes.includes(savedTheme as CatppuccinTheme)) {
@@ -82,6 +115,7 @@ onMounted(() => {
82115
document.documentElement.setAttribute('data-theme', currentTheme.value);
83116
window.addEventListener('scroll', handleScroll);
84117
startTyping();
118+
fetchGithubStars();
85119
});
86120
87121
onUnmounted(() => window.removeEventListener('scroll', handleScroll));
@@ -362,7 +396,7 @@ const copyToClipboard = async (text: string | undefined): Promise<void> => {
362396
</div>
363397
<div class="hero-stats">
364398
<div class="stat-item">
365-
<strong>160+</strong>
399+
<strong>{{ starCount }}</strong>
366400
<span>Stars</span>
367401
</div>
368402
<div class="stat-item">

0 commit comments

Comments
 (0)