|
2 | 2 | import HomeLayout from "@layouts/HomeLayout.astro"; |
3 | 3 | import { keystatic } from "@lib/keystatic/client"; |
4 | 4 | import invariant from "tiny-invariant"; |
| 5 | +import { Icon } from "astro-icon/components"; // Assuming astro-icon is installed and Icon can be imported like this |
5 | 6 |
|
6 | 7 | const i18n = await keystatic.singletons.homepage.read({ |
7 | 8 | resolveLinkedFiles: true, |
@@ -31,20 +32,29 @@ invariant(i18n, "No homepage content found"); |
31 | 32 | <input |
32 | 33 | type="email" |
33 | 34 | 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" |
35 | 36 | /> |
| 37 | + <p class="text-sm text-btcgray-600"> |
| 38 | + → <span id="subscriber-count-value">4</span> |
| 39 | + {i18n.waitlist.numberSuffix} |
| 40 | + </p> |
36 | 41 | <button |
37 | 42 | 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" |
39 | 44 | > |
40 | 45 | {i18n.waitlist.buttonText} |
41 | 46 | </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> |
42 | 57 | </form> |
43 | | - <p class="mt-2 |
44 | | - text-sm |
45 | | - text-btcgray-600"> |
46 | | - → 1,070 {i18n.waitlist.numberSuffix} |
47 | | - </p> |
48 | 58 | </section> |
49 | 59 |
|
50 | 60 | <section class="flex-1"> |
@@ -78,6 +88,10 @@ invariant(i18n, "No homepage content found"); |
78 | 88 | const form = document.querySelector("form"); |
79 | 89 | const emailInput = document.querySelector('input[type="email"]'); |
80 | 90 | 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 | + ); |
81 | 95 |
|
82 | 96 | if (form && emailInput && submitButton) { |
83 | 97 | const htmlEmailInput = emailInput as HTMLInputElement; |
@@ -120,18 +134,50 @@ invariant(i18n, "No homepage content found"); |
120 | 134 | email: email, |
121 | 135 | name: name, |
122 | 136 | status: "enabled", |
123 | | - lists: [1], |
| 137 | + lists: [3], |
124 | 138 | }), |
125 | 139 | } |
126 | 140 | ); |
127 | 141 |
|
128 | | - if (response.ok || response.status === 201 || response.status === 409) { |
| 142 | + if (response.ok) { |
129 | 143 | htmlSubmitButton.textContent = "Subscribed!"; |
130 | 144 | htmlSubmitButton.classList.remove( |
131 | 145 | "bg-btcgray-800", |
132 | 146 | "hover:bg-btcgray-700" |
133 | 147 | ); |
134 | 148 | 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); |
135 | 181 | } else { |
136 | 182 | const errorData = await response.json(); |
137 | 183 | console.error("Failed to subscribe:", response.status, errorData); |
@@ -181,4 +227,82 @@ invariant(i18n, "No homepage content found"); |
181 | 227 | } else { |
182 | 228 | console.error("Form, email input, or submit button not found."); |
183 | 229 | } |
| 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 | + }); |
184 | 308 | </script> |
0 commit comments