import React, { useState, useEffect } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
const GROWTH_STAGES = ['🌱', '🌿', '🌳'];
const FOCUS_THRESHOLD = 25 * 60 * 1000;
function App() {
const [forest, setForest] = useState([]);
const [focusStartTime, setFocusStartTime] = useState(null);
const [currentTree, setCurrentTree] = useState(GROWTH_STAGES[0]);
const [isFocusing, setIsFocusing] = useState(false);
useEffect(() => {
const savedForest = localStorage.getItem('forest');
if (savedForest) {
setForest(JSON.parse(savedForest));
}
}, []);
useEffect(() => {
let interval;
if (isFocusing) {
interval = setInterval(() => {
const elapsedTime = Date.now() - focusStartTime;
const stageIndex = Math.min(
Math.floor(elapsedTime / FOCUS_THRESHOLD),
GROWTH_STAGES.length - 1
);
setCurrentTree(GROWTH_STAGES[stageIndex]);
if (elapsedTime >= FOCUS_THRESHOLD * (GROWTH_STAGES.length - 1)) {
handleStopFocus();
}
}, 1000);
}
return () => clearInterval(interval);
}, [isFocusing, focusStartTime]);
const handleStartFocus = () => {
setIsFocusing(true);
setFocusStartTime(Date.now());
setCurrentTree(GROWTH_STAGES[0]);
};
const handleStopFocus = () => {
setIsFocusing(false);
const newForest = [...forest, currentTree];
setForest(newForest);
localStorage.setItem('forest', JSON.stringify(newForest));
setCurrentTree(GROWTH_STAGES[0]);
};
return (
<div className="container">
<h1>🌲 Focus Forest 🌲</h1>
<div className="forest">
{forest.map((tree, index) => (
<span key={index} className="tree">{tree}</span>
))}
</div>
<div className="current-tree">
{isFocusing && <span className="tree">{currentTree}</span>}
</div>
<div className="controls">
{!isFocusing ? (
<button onClick={handleStartFocus}>Start Focus</button>
) : (
<button onClick={handleStopFocus}>Stop Focus</button>
)}
</div>
<p className="info">
Focus for 25 minutes to grow a tree. The longer you focus, the bigger your tree grows!
</p>
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank" rel="noopener noreferrer">View Source</a>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") { client(); }
async function server(request: Request): Promise<Response> {
return new Response(`
<html>
<head>
<title>Focus Forest</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>${css}</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="${import.meta.url}"></script>