Skip to content

Commit 4ca675c

Browse files
committed
feat: enhance homepage with subscriber count and GitHub star button
1 parent 7c34e73 commit 4ca675c

1 file changed

Lines changed: 133 additions & 9 deletions

File tree

website/src/pages/index.astro

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import HomeLayout from "@layouts/HomeLayout.astro";
33
import { keystatic } from "@lib/keystatic/client";
44
import invariant from "tiny-invariant";
5+
import { Icon } from "astro-icon/components"; // Assuming astro-icon is installed and Icon can be imported like this
56
67
const i18n = await keystatic.singletons.homepage.read({
78
resolveLinkedFiles: true,
@@ -31,20 +32,29 @@ invariant(i18n, "No homepage content found");
3132
<input
3233
type="email"
3334
placeholder={i18n.waitlist.inputPlaceholder}
34-
class="border border-btcgray-300 p-3 rounded mb-4 focus:ring-2 focus:ring-brand-500 outline-none"
35+
class="border border-btcgray-300 p-3 rounded mb-1 focus:ring-2 focus:ring-brand-500 outline-none"
3536
/>
37+
<p class="text-sm text-btcgray-600">
38+
&rarr; <span id="subscriber-count-value">4</span>
39+
{i18n.waitlist.numberSuffix}
40+
</p>
3641
<button
3742
type="submit"
38-
class="bg-btcgray-800 text-white p-3 rounded hover:bg-btcgray-700 hover:cursor-pointer disabled:cursor-not-allowed"
43+
class="bg-btcgray-800 mt-4 text-white p-3 rounded hover:bg-btcgray-700 hover:cursor-pointer disabled:cursor-not-allowed"
3944
>
4045
{i18n.waitlist.buttonText}
4146
</button>
47+
<a
48+
href="https://github.com/devflowinc/threshold"
49+
target="_blank"
50+
rel="noopener noreferrer"
51+
id="star-repo-button"
52+
class="mt-4 flex items-center justify-center bg-white text-btcgray-800 border border-btcgray-800 p-3 rounded hover:bg-btcgray-100 hover:cursor-pointer w-full"
53+
>
54+
<Icon name="simple-icons:github" class="w-5 h-5 mr-2" />
55+
<span id="star-repo-text">Star Us | 4</span>
56+
</a>
4257
</form>
43-
<p class="mt-2
44-
text-sm
45-
text-btcgray-600">
46-
&rarr; 1,070 {i18n.waitlist.numberSuffix}
47-
</p>
4858
</section>
4959

5060
<section class="flex-1">
@@ -78,6 +88,10 @@ invariant(i18n, "No homepage content found");
7888
const form = document.querySelector("form");
7989
const emailInput = document.querySelector('input[type="email"]');
8090
const submitButton = document.querySelector('button[type="submit"]');
91+
const starRepoButtonText = document.getElementById("star-repo-text");
92+
const subscriberCountValue = document.getElementById(
93+
"subscriber-count-value"
94+
);
8195

8296
if (form && emailInput && submitButton) {
8397
const htmlEmailInput = emailInput as HTMLInputElement;
@@ -120,18 +134,50 @@ invariant(i18n, "No homepage content found");
120134
email: email,
121135
name: name,
122136
status: "enabled",
123-
lists: [1],
137+
lists: [3],
124138
}),
125139
}
126140
);
127141

128-
if (response.ok || response.status === 201 || response.status === 409) {
142+
if (response.ok) {
129143
htmlSubmitButton.textContent = "Subscribed!";
130144
htmlSubmitButton.classList.remove(
131145
"bg-btcgray-800",
132146
"hover:bg-btcgray-700"
133147
);
134148
htmlSubmitButton.classList.add("bg-green-500");
149+
150+
// incrase the subscriber count by 1
151+
const currentCount = parseInt(
152+
subscriberCountValue?.textContent || "0",
153+
10
154+
);
155+
if (subscriberCountValue) {
156+
subscriberCountValue.textContent = (
157+
currentCount + 1
158+
).toLocaleString();
159+
}
160+
} else if (response.status === 409) {
161+
// If the email already exists, we can handle it gracefully
162+
htmlSubmitButton.textContent = "Already Subscribed";
163+
htmlSubmitButton.disabled = false;
164+
htmlSubmitButton.classList.remove(
165+
"bg-btcgray-800",
166+
"hover:bg-btcgray-700",
167+
"bg-green-500"
168+
);
169+
htmlSubmitButton.classList.add("bg-yellow-500");
170+
171+
setTimeout(() => {
172+
if (htmlSubmitButton.textContent === "Already Subscribed") {
173+
htmlSubmitButton.textContent = originalButtonText;
174+
htmlSubmitButton.classList.remove("bg-yellow-500");
175+
htmlSubmitButton.classList.add(
176+
"bg-btcgray-800",
177+
"hover:bg-btcgray-700"
178+
);
179+
}
180+
}, 3000);
135181
} else {
136182
const errorData = await response.json();
137183
console.error("Failed to subscribe:", response.status, errorData);
@@ -181,4 +227,82 @@ invariant(i18n, "No homepage content found");
181227
} else {
182228
console.error("Form, email input, or submit button not found.");
183229
}
230+
231+
async function fetchListmonkSubscriberCount() {
232+
if (!subscriberCountValue) {
233+
console.error("Subscriber count value element not found.");
234+
return;
235+
}
236+
const apiKey = import.meta.env.PUBLIC_LISTMONK_API_KEY;
237+
const apiUsername = "skeptrune-api-key";
238+
239+
if (!apiKey) {
240+
console.error(
241+
"PUBLIC_LISTMONK_API_KEY is not set in environment variables."
242+
);
243+
return;
244+
}
245+
246+
try {
247+
const response = await fetch(
248+
"https://listmonk.skeptrune.com/api/lists/3",
249+
{
250+
method: "GET",
251+
headers: {
252+
Authorization: `Basic ${btoa(`${apiUsername}:${apiKey}`)}`,
253+
},
254+
}
255+
);
256+
257+
if (response.ok) {
258+
const data = await response.json();
259+
if (
260+
data &&
261+
data.data &&
262+
typeof data.data.subscriber_count === "number"
263+
) {
264+
subscriberCountValue.textContent =
265+
data.data.subscriber_count.toLocaleString();
266+
} else {
267+
console.error(
268+
"Failed to parse subscriber count from Listmonk API response:",
269+
data
270+
);
271+
}
272+
} else {
273+
console.error(
274+
"Failed to fetch Listmonk subscriber count:",
275+
response.status
276+
);
277+
}
278+
} catch (error) {
279+
console.error("Error fetching Listmonk subscriber count:", error);
280+
}
281+
}
282+
283+
async function fetchGitHubStars() {
284+
if (!starRepoButtonText) {
285+
console.error("Star repo button text element not found.");
286+
return;
287+
}
288+
try {
289+
const response = await fetch(
290+
"https://api.github.com/repos/devflowinc/threshold"
291+
);
292+
if (response.ok) {
293+
const data = await response.json();
294+
const starCount = data.stargazers_count;
295+
starRepoButtonText.textContent = `Star Us | ${starCount}`;
296+
} else {
297+
console.error("Failed to fetch GitHub stars:", response.status);
298+
}
299+
} catch (error) {
300+
console.error("Error fetching GitHub stars:", error);
301+
}
302+
}
303+
304+
document.addEventListener("astro:page-load", () => {
305+
fetchGitHubStars();
306+
fetchListmonkSubscriberCount();
307+
});
184308
</script>

0 commit comments

Comments
 (0)