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 = {};
for (let d = new Date(oneYearAgo); d <= today; d.setDate(d.getDate() + 1)) {
data[d.toISOString().split('T')[0]] = 0;
}
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 />);
}