Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

TodoApp.tsx

280 lines9.4 KB

1import { Link } from '@tanstack/solid-router'
2import { For, Show, createSignal } from 'solid-js'
3import type { JSX } from 'solid-js'
4import type { Collection } from '@tanstack/solid-db'
5import type { SelectConfig, SelectTodo } from '../db/validation'
6​
7interface TodoAppProps {
8 todos: Array<SelectTodo>
9 configData: Array<SelectConfig>
10 todoCollection: Collection<SelectTodo>
11 configCollection: Collection<SelectConfig>
12 title: string
14​
15export function TodoApp(props: TodoAppProps) {
16 const [newTodo, setNewTodo] = createSignal(``)
17​
18 // Define a type-safe helper function to get config values
19 const getConfigValue = (key: string): string => {
20 for (const config of props.configData) {
21 if (config.key === key) {
22 return config.value
23 }
24 }
25 return ``
26 }
27​
28 // Define a helper function to update config values
29 const setConfigValue = (key: string, value: string): void => {
30 for (const config of props.configData) {
31 if (config.key === key) {
32 props.configCollection.update(config.id, (draft) => {
33 draft.value = value
34 })
35 return
36 }
37 }
38​
39 // If the config doesn't exist yet, create it
40 props.configCollection.insert({
41 id: Math.round(Math.random() * 1000000),
42 key,
43 value,
44 created_at: new Date(),
45 updated_at: new Date(),
46 })
47 }
48​
49 const backgroundColor = getConfigValue(`backgroundColor`)
50​
51 // Function to generate a complementary color
52 const getComplementaryColor = (hexColor: string): string => {
53 // Default to a nice blue if no color is provided
54 if (!hexColor) return `#3498db`
55​
56 // Remove the hash if it exists
57 const color = hexColor.replace(`#`, ``)
58​
59 // Convert hex to RGB
60 const r = parseInt(color.substr(0, 2), 16)
61 const g = parseInt(color.substr(2, 2), 16)
62 const b = parseInt(color.substr(4, 2), 16)
63​
64 // Calculate complementary color (inverting the RGB values)
65 const compR = 255 - r
66 const compG = 255 - g
67 const compB = 255 - b
68​
69 // Convert back to hex
70 const compHex =
71 `#` +
72 ((1 << 24) + (compR << 16) + (compG << 8) + compB).toString(16).slice(1)
73​
74 // Calculate brightness of the background
75 const brightness = r * 0.299 + g * 0.587 + b * 0.114
76​
77 // If the complementary color doesn't have enough contrast, adjust it
78 const compBrightness = compR * 0.299 + compG * 0.587 + compB * 0.114
79 const brightnessDiff = Math.abs(brightness - compBrightness)
80​
81 if (brightnessDiff < 128) {
82 // Not enough contrast, use a more vibrant alternative
83 if (brightness > 128) {
84 // Dark color for light background
85 return `#8e44ad` // Purple
86 } else {
87 // Light color for dark background
88 return `#f1c40f` // Yellow
89 }
90 }
91​
92 return compHex
93 }
94​
95 const titleColor = getComplementaryColor(backgroundColor)
96​
97 const handleColorChange: JSX.CustomEventHandlersCamelCase<HTMLInputElement>[`onInput`] =
98 (e) => {
99 const newColor = e.target.value
100 setConfigValue(`backgroundColor`, newColor)
101 }
102​
103 const handleSubmit: JSX.CustomEventHandlersCamelCase<HTMLFormElement>[`onSubmit`] =
104 (e) => {
105 e.preventDefault()
106 if (!newTodo().trim()) return
107​
108 props.todoCollection.insert({
109 text: newTodo(),
110 completed: false,
111 id: Math.round(Math.random() * 1000000),
112 created_at: new Date(),
113 updated_at: new Date(),
114 })
115 setNewTodo(``)
116 }
117​
118 const activeTodos = props.todos.filter((todo) => !todo.completed)
119 const completedTodos = props.todos.filter((todo) => todo.completed)
120​
121 return (
122 <div
123 class="min-h-screen flex items-start justify-center overflow-auto py-8"
124 style={{ 'background-color': backgroundColor }}
125 >
126 <div style={{ width: `550px` }} class="mx-auto relative">
127 <div class="text-center mb-8">
128 <h1 class="text-[70px] font-bold mb-4" style={{ color: titleColor }}>
129 {props.title}
130 </h1>
131​
132 {/* Navigation */}
133 <div class="flex justify-center gap-4 mb-4">
134 <Link
135 to="/"
136 class="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600 transition-colors"
137 >
138 ← Home
139 </Link>
140 <Link
141 to="/query"
142 class="px-4 py-2 bg-green-700 text-white rounded hover:bg-green-800 transition-colors"
143 >
144 Query
145 </Link>
146 <Link
147 to="/electric"
148 class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors"
149 >
150 Electric
151 </Link>
152 <Link
153 to="/trailbase"
154 class="px-4 py-2 bg-purple-600 text-white rounded hover:bg-purple-700 transition-colors"
155 >
156 TrailBase
157 </Link>
158 </div>
159 </div>
160​
161 <div class="mb-4 flex justify-end">
162 <div class="flex items-center">
163 <label
164 for="colorPicker"
165 class="mr-2 text-sm font-medium text-gray-700"
166 style={{ color: titleColor }}
167 >
168 Background Color:
169 </label>
170 <input
171 type="color"
172 id="colorPicker"
173 value={backgroundColor}
174 onInput={handleColorChange}
175 class="cursor-pointer border border-gray-300 rounded"
176 />
177 </div>
178 </div>
179​
180 <div class="bg-white shadow-[0_2px_4px_0_rgba(0,0,0,0.2),0_25px_50px_0_rgba(0,0,0,0.1)] relative">
181 <form onSubmit={handleSubmit} class="relative">
182 <Show when={props.todos.length > 0}>
183 <button
184 type="button"
185 class="absolute left-0 w-12 h-full text-[30px] text-[#e6e6e6] hover:text-[#4d4d4d]"
186 onClick={() => {
187 const allCompleted =
188 completedTodos.length === props.todos.length
189 const todosToToggle = allCompleted
190 ? completedTodos
191 : activeTodos
192 props.todoCollection.update(
193 todosToToggle.map((todo) => todo.id),
194 (drafts) => {
195 drafts.forEach(
196 (draft) => (draft.completed = !allCompleted),
197 )
198 },
199 )
200 }}
201 >
202 ❯
203 </button>
204 </Show>
205 <input
206 type="text"
207 value={newTodo()}
208 onChange={(e) => setNewTodo(e.target.value)}
209 placeholder="What needs to be done?"
210 class="w-full py-4 pl-[60px] pr-4 text-2xl font-light border-none shadow-[inset_0_-2px_1px_rgba(0,0,0,0.03)] box-border"
211 style={{
212 background: `rgba(0, 0, 0, 0.003)`,
213 }}
214 />
215 </form>
216​
217 <Show when={props.todos.length > 0}>
218 <>
219 <ul class="my-0 mx-0 p-0 list-none">
220 <For each={props.todos}>
221 {(todo) => (
222 <li class="relative border-b border-[#ededed] last:border-none group">
223 <div class="flex items-center h-[58px] pl-[60px]">
224 <input
225 type="checkbox"
226 checked={todo.completed}
227 onChange={() =>
228 props.todoCollection.update(todo.id, (draft) => {
229 draft.completed = !draft.completed
230 })
231 }
232 class="absolute left-[12px] top-0 bottom-0 my-auto h-[40px] w-[40px] cursor-pointer"
233 />
234 <label
235 class={`block leading-[1.2] py-[15px] px-[15px] text-2xl transition-colors ${
236 todo.completed ? `text-[#d9d9d9] line-through` : ``
237 }`}
238 >
239 {todo.text}
240 </label>
241 <button
242 onClick={() => {
243 props.todoCollection.delete(todo.id)
244 }}
245 class="hidden group-hover:block absolute right-[10px] w-[40px] h-[40px] my-auto top-0 bottom-0 text-[30px] text-[#cc9a9a] hover:text-[#af5b5e] transition-colors"
246 >
247 ×
248 </button>
249 </div>
250 </li>
251 )}
252 </For>
253 </ul>
254​
255 <footer class="text-[14px] text-[#777] py-[10px] px-[15px] h-[40px] relative border-t border-[#e6e6e6] flex justify-between items-center">
256 <span class="text-[inherit]">
257 {activeTodos.length}
258 {` `}
259 {activeTodos.length === 1 ? `item` : `items`} left
260 </span>
261 {completedTodos.length > 0 && (
262 <button
263 onClick={() => {
264 props.todoCollection.delete(
265 completedTodos.map((todo) => todo.id),
266 )
267 }}
268 class="text-inherit hover:underline"
269 >
270 Clear completed
271 </button>
272 )}
273 </footer>
274 </>
275 </Show>
276 </div>
277 </div>
278 </div>
279 )