Open History to see the last commit
1import React, { useState } from "react"
2import { Link } from "@tanstack/react-router"
3import type { FormEvent } from "react"
4import type { Collection } from "@tanstack/react-db"
5
6import type { SelectConfig, SelectTodo } from "@/db/validation"
7import { getComplementaryColor } from "@/lib/color"
8
9interface TodoAppProps {
10 todos: Array<SelectTodo>
11 configData: Array<SelectConfig>
12 todoCollection: Collection<SelectTodo>
13 configCollection: Collection<SelectConfig>
14 title: string
15}
16
17export function TodoApp({
18 todos,
19 configData,
20 todoCollection,
21 configCollection,
22 title,
23}: TodoAppProps) {
24 const [newTodo, setNewTodo] = useState(``)
25
26 // Define a type-safe helper function to get config values
27 const getConfigValue = (key: string): string | undefined => {
28 for (const config of configData) {
29 if (config.key === key) {
30 return config.value
31 }
32 }
33 return undefined
34 }
35
36 // Define a helper function to update config values
37 const setConfigValue = (key: string, value: string): void => {
38 for (const config of configData) {
39 if (config.key === key) {
40 configCollection.update(config.id, (draft) => {
41 draft.value = value
42 })
43 return
44 }
45 }
46
47 // If the config doesn't exist yet, create it
48 configCollection.insert({
49 id: Math.round(Math.random() * 1000000),
50 key,
51 value,
52 created_at: new Date(),
53 updated_at: new Date(),
54 })
55 }
56
57 const backgroundColor = getConfigValue(`backgroundColor`)
58 const titleColor = getComplementaryColor(backgroundColor)
59
60 const handleColorChange = (e: React.ChangeEvent<HTMLInputElement>) => {
61 const newColor = e.target.value
62 setConfigValue(`backgroundColor`, newColor)
63 }
64
65 const handleSubmit = (e: FormEvent) => {
66 e.preventDefault()
67 const todo = newTodo.trim()
68 setNewTodo(``)
69
70 if (todo) {
71 todoCollection.insert({
72 text: todo,
73 completed: false,
74 id: Math.round(Math.random() * 1000000),
75 created_at: new Date(),
76 updated_at: new Date(),
77 })
78 }
79 }
80
81 const activeTodos = todos.filter((todo) => !todo.completed)
82 const completedTodos = todos.filter((todo) => todo.completed)
83
84 return (
85 <main
86 className="h-dvh flex justify-center overflow-auto py-8"
87 style={{ backgroundColor }}
88 >
89 <div className="w-[550px]">
90 <h1
91 className="text-center text-[70px] font-bold mb-4"
92 style={{ color: titleColor }}
93 >
94 {title}
95 </h1>
96
97 <Navigation />
98
99 <div className="py-4 flex justify-end">
100 <div className="flex items-center">
101 <label
102 htmlFor="colorPicker"
103 className="mr-2 text-sm font-medium text-gray-700"
104 style={{ color: titleColor }}
105 >
106 Background Color:
107 </label>
108 <input
109 type="color"
110 id="colorPicker"
111 value={backgroundColor}
112 onChange={handleColorChange}
113 className="cursor-pointer border border-gray-300 rounded"
114 />
115 </div>
116 </div>
117
118 <div className="bg-white shadow-[0_2px_4px_0_rgba(0,0,0,0.2),0_25px_50px_0_rgba(0,0,0,0.1)] relative">
119 <form onSubmit={handleSubmit} className="relative">
120 <button
121 type="button"
122 className="absolute w-12 h-full text-[30px] text-[#e6e6e6] hover:text-[#4d4d4d]"
123 disabled={todos.length === 0}
124 onClick={() => {
125 const todosToToggle =
126 activeTodos.length > 0 ? activeTodos : completedTodos
127
128 todoCollection.update(
129 todosToToggle.map((todo) => todo.id),
130 (drafts) =>
131 drafts.forEach(
132 (draft) => (draft.completed = !draft.completed)
133 )
134 )
135 }}
136 >
137 ❯
138 </button>
139 <input
140 type="text"
141 value={newTodo}
142 onChange={(e) => setNewTodo(e.target.value)}
143 placeholder="What needs to be done?"
144 className="w-full h-[64px] pl-[60px] pr-4 text-2xl font-light border-none shadow-[inset_0_-2px_1px_rgba(0,0,0,0.03)] box-border"
145 />
146 </form>
147
148 <ul className="list-none">
149 {todos.map((todo) => (
150 <li
151 key={`todo-${todo.id}`}
152 className="relative border-b border-[#ededed] last:border-none group"
153 >
154 <div className="flex items-center h-[58px] pl-[60px] gap-1.2">
155 <input
156 type="checkbox"
157 checked={todo.completed}
158 onChange={() =>
159 todoCollection.update(todo.id, (draft) => {
160 draft.completed = !draft.completed
161 })
162 }
163 className="absolute left-[12px] size-[40px] cursor-pointer"
164 />
165 <label
166 className={`block p-[15px] text-2xl transition-colors ${todo.completed ? `text-[#d9d9d9] line-through` : ``}`}
167 >
168 {todo.text}
169 </label>
170 <button
171 onClick={() => todoCollection.delete(todo.id)}
172 className="hidden group-hover:block absolute right-[20px] text-[30px] text-[#cc9a9a] hover:text-[#af5b5e] transition-colors"
173 >
174 ×
175 </button>
176 </div>
177 </li>
178 ))}
179 </ul>
180
181 <footer className="text-[14px] text-[#777] px-[15px] h-[40px] border-t border-[#e6e6e6] flex justify-between items-center">
182 <span>
183 {`${activeTodos.length} ${activeTodos.length === 1 ? `item` : `items`} left`}
184 </span>
185
186 {completedTodos.length > 0 && (
187 <button
188 onClick={() =>
189 todoCollection.delete(completedTodos.map((todo) => todo.id))
190 }
191 className="hover:underline"
192 >
193 Clear completed
194 </button>
195 )}
196 </footer>
197 </div>
198 </div>
199 </main>
200 )
201}
202
203function Navigation() {
204 const style = `px-4 py-2 text-white rounded transition-colors`
205 const links = [
206 [`/query`, `Query`, `bg-green-700 hover:bg-green-800`],
207 [`/electric`, `Electric`, `bg-blue-500 hover:bg-blue-600`],
208 [`/trailbase`, `TrailBase`, `bg-purple-600 hover:bg-purple-700`],
209 ]
210
211 return (
212 <nav className="flex justify-center gap-4 mb-4">
213 <Link to="/" className={`${style} bg-gray-500 hover:bg-gray-600`}>
214 ← Home
215 </Link>
216
217 {links.map(([href, name, cls]) => (
218 <Link key={href} to={href} className={`${style} ${cls}`}>
219 {name}
220 </Link>
221 ))}
222 </nav>
223 )
224}