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 productivity app will create a forest as users maintain focus.
// We'll use emojis to represent trees at different growth stages.
// The app will use client-side storage to persist the forest state.
/** @jsxImportSource https://esm.sh/react */
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; // 25 minutes in milliseconds
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>