-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.tsx
More file actions
135 lines (119 loc) · 3.56 KB
/
Copy pathindex.tsx
File metadata and controls
135 lines (119 loc) · 3.56 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
import React, { useState, useEffect } from 'react';
import { RxGithubLogo } from "react-icons/rx";
import { VscVscode } from "react-icons/vsc";
import {
GITHUB_MAIN_REPO_SLUG,
GITHUB_MAIN_REPO_URL,
VSCODE_MARKETPLACE_EXTENSION_ID,
VSCODE_MARKETPLACE_URL,
} from '@site/src/constants';
import styles from './styles.module.css';
// Number formatting function
function formatNumber(num: number): string {
if (num < 10000) {
return num.toLocaleString();
}
if (num >= 1000000) {
const truncated = Math.floor((num / 1000000) * 10) / 10;
return truncated.toFixed(1) + "M";
}
const truncated = Math.floor((num / 1000) * 10) / 10;
return truncated.toFixed(1) + "k";
}
// GitHub Stars API
async function getGitHubStars() {
try {
const res = await fetch(`https://api.github.com/repos/${GITHUB_MAIN_REPO_SLUG}`);
const data = await res.json();
if (typeof data.stargazers_count !== "number") {
console.error("GitHub API: Invalid stargazers count");
return null;
}
return formatNumber(data.stargazers_count);
} catch (error) {
console.error("Error fetching GitHub stars:", error);
return null;
}
}
// VS Code Downloads API
async function getVSCodeDownloads() {
try {
const res = await fetch("https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery", {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json;api-version=7.1-preview.1",
},
body: JSON.stringify({
filters: [
{
criteria: [
{
filterType: 7,
value: VSCODE_MARKETPLACE_EXTENSION_ID,
},
],
},
],
flags: 914,
}),
});
const data = await res.json();
const statistics = data?.results?.[0]?.extensions?.[0]?.statistics;
if (!statistics) {
console.error("VSCode API: Missing statistics in response");
return null;
}
const installStat = statistics.find((stat: { statisticName: string; value: number }) => stat.statisticName === "install");
if (!installStat) {
console.error("VSCode API: Install count not found");
return null;
}
return installStat.value;
} catch (error) {
console.error("Error fetching VSCode downloads:", error);
return null;
}
}
export default function GitHubInstallButtons(): React.JSX.Element {
const [stars, setStars] = useState<string | null>(null);
const [downloads, setDownloads] = useState<string | null>(null);
useEffect(() => {
// Fetch live data
getGitHubStars().then(count => {
if (count) setStars(count);
});
getVSCodeDownloads().then(count => {
if (count) setDownloads(formatNumber(count));
});
}, []);
return (
<div className={styles.container}>
{/* GitHub Button */}
<a
href={GITHUB_MAIN_REPO_URL}
target="_blank"
rel="noopener noreferrer"
className={styles.githubButton}
title="GitHub Repository"
>
<RxGithubLogo className={styles.icon} />
{stars && <span>{stars}</span>}
</a>
{/* Install Button */}
<a
href={VSCODE_MARKETPLACE_URL}
target="_blank"
rel="noopener noreferrer"
className={styles.installButton}
title="Install VS Code Extension"
>
<VscVscode className={styles.icon} />
<span>
Install <span className={styles.separator}>·</span>
</span>
{downloads && <span>{downloads}</span>}
</a>
</div>
);
}