Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
// This approach will fetch data from the specified endpoint, process it to create a GitHub-style contribution chart,
// and render it using React. We'll use the fetch API to get the data, process it to create a heatmap-like structure,
// and then use CSS Grid to render the chart with varying shades of green based on the number of vals created each day.
/** @jsxImportSource https://esm.sh/react */
import React, { useState, useEffect } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [contributionData, setContributionData] = useState({});
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch('https://ejfox-allvals.web.val.run')
.then(response => response.json())
.then(data => {
const processedData = processContributionData(data.vals);
setContributionData(processedData);
setLoading(false);
})
.catch(error => {
console.error('Error fetching data:', error);
setLoading(false);
});
}, []);
const processContributionData = (vals) => {
const today = new Date();
const oneYearAgo = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
const data = {};
// Initialize all days in the past year with 0 contributions
for (let d = new Date(oneYearAgo); d <= today; d.setDate(d.getDate() + 1)) {
data[d.toISOString().split('T')[0]] = 0;
}
// Count contributions for each day
vals.forEach(val => {
const date = new Date(val.createdAt).toISOString().split('T')[0];
if (date in data) {
data[date]++;
}
});
return data;
};
const getColorIntensity = (count) => {
if (count === 0) return '#ebedf0';
if (count < 5) return '#9be9a8';
if (count < 10) return '#40c463';
if (count < 15) return '#30a14e';
return '#216e39';
};
const renderContributionGrid = () => {
const weeks = [];
const days = Object.keys(contributionData).sort();
for (let i = 0; i < days.length; i += 7) {
const week = days.slice(i, i + 7);
weeks.push(
<div key={i} className="contribution-week">
{week.map(day => (
<div
key={day}
className="contribution-day"
style={{ backgroundColor: getColorIntensity(contributionData[day]) }}
title={`${contributionData[day]} contributions on ${day}`}
/>
))}
</div>
);
}
return <div className="contribution-grid">{weeks}</div>;
};
return (
<div className="container">
<h1>Val Contribution Chart</h1>
{loading ? (
<p>Loading...</p>
) : (
<>
{renderContributionGrid()}
<p className="source-link">
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank" rel="noopener noreferrer">
View Source
</a>
</p>
</>
)}
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
ejfox-valcontributionchart.web.val.run
September 1, 2024