@@ -140,8 +140,8 @@ function formatCurrency(value: number): string {
140
140
<script is:inline >
141
141
const goal = 12000;
142
142
const cacheKey = 'ddevSponsorshipData';
143
- const cacheTTL = 24 * 60 * 60 * 1000 // 1 day in ms
144
- const now = Date.now() ;
143
+ const cacheTTL = 12 * 60 * 60 * 1000; // 12 hours in ms
144
+ const apiUrl = 'https://raw.githubusercontent.com/ddev/sponsorship-data/refs/heads/main/data/all-sponsorships.json' ;
145
145
146
146
function formatCurrency(value) {
147
147
return new Intl.NumberFormat('en-US', {
@@ -152,27 +152,56 @@ function formatCurrency(value: number): string {
152
152
}).format(value);
153
153
}
154
154
155
- function update(data) {
156
- const income = data.total_monthly_average_income;
157
- const percentage = Math.min((income / goal) * 100, 100).toFixed(0);
158
- document.querySelector('.ddev-sponsor-us-banner__raised').textContent = `Raised: ${formatCurrency(income)}`;
159
- document.querySelector('.ddev-sponsor-us-banner__goal').textContent = `Goal: ${formatCurrency(goal)}`;
160
- document.querySelector('.ddev-sponsor-us-banner__percent').textContent = `${percentage}% of monthly goal`;
161
- document.querySelector('.ddev-sponsor-us-banner__fill').style.width = `${percentage}%`;
155
+ function updateUI(data) {
156
+ try {
157
+ const income = data.total_monthly_average_income;
158
+ const percentage = Math.min((income / goal) * 100, 100).toFixed(0);
159
+
160
+ document.querySelector('.ddev-sponsor-us-banner__raised').textContent = `Raised: ${formatCurrency(income)}`;
161
+ document.querySelector('.ddev-sponsor-us-banner__goal').textContent = `Goal: ${formatCurrency(goal)}`;
162
+ document.querySelector('.ddev-sponsor-us-banner__percent').textContent = `${percentage}% of monthly goal`;
163
+ document.querySelector('.ddev-sponsor-us-banner__fill').style.width = `${percentage}%`;
164
+ } catch (error) {
165
+ console.error('Error updating UI:', error);
166
+ }
167
+ }
168
+
169
+ async function fetchSponsorshipData() {
170
+ try {
171
+ const response = await fetch(apiUrl);
172
+ if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
173
+
174
+ const data = await response.json();
175
+ const cacheData = {
176
+ timestamp: Date.now(),
177
+ data: data
178
+ };
179
+
180
+ localStorage.setItem(cacheKey, JSON.stringify(cacheData));
181
+ updateUI(data);
182
+ } catch (error) {
183
+ console.error('Failed to fetch sponsorship data:', error);
184
+ }
162
185
}
163
186
164
- const cached = localStorage.getItem(cacheKey);
165
- if (cached) {
166
- const parsed = JSON.parse(cached);
167
- if (now - parsed.timestamp < cacheTTL) {
168
- update(parsed.data);
187
+ function loadSponsorshipData() {
188
+ const cached = localStorage.getItem(cacheKey);
189
+ const now = Date.now();
190
+
191
+ if (cached) {
192
+ try {
193
+ const parsed = JSON.parse(cached);
194
+ if (now - parsed.timestamp < cacheTTL) {
195
+ updateUI(parsed.data);
196
+ return;
197
+ }
198
+ } catch (error) {
199
+ console.error('Error parsing cached data:', error);
200
+ }
169
201
}
170
- } else {
171
- fetch('https://raw.githubusercontent.com/ddev/sponsorship-data/refs/heads/main/data/all-sponsorships.json')
172
- .then((res) => res.json())
173
- .then((data) => {
174
- localStorage.setItem(cacheKey, JSON.stringify({ timestamp: now, data }));
175
- update(data);
176
- });
202
+
203
+ fetchSponsorshipData();
177
204
}
205
+
206
+ loadSponsorshipData();
178
207
</script >
0 commit comments