|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { useLiveQuery } from '@tanstack/react-db' |
| 3 | +import type { Collection } from '@tanstack/db' |
| 4 | +import type { PersistedTodo } from '~/db/persisted-todos' |
| 5 | + |
| 6 | +interface PersistedTodoDemoProps { |
| 7 | + collection: Collection<PersistedTodo, string> |
| 8 | +} |
| 9 | + |
| 10 | +export function PersistedTodoDemo({ collection }: PersistedTodoDemoProps) { |
| 11 | + const [newTodoText, setNewTodoText] = useState(``) |
| 12 | + const [error, setError] = useState<string | null>(null) |
| 13 | + |
| 14 | + const { data: todoList = [] } = useLiveQuery((q) => |
| 15 | + q |
| 16 | + .from({ todo: collection }) |
| 17 | + .orderBy(({ todo }) => todo.createdAt, `desc`), |
| 18 | + ) |
| 19 | + |
| 20 | + const handleAddTodo = () => { |
| 21 | + if (!newTodoText.trim()) return |
| 22 | + |
| 23 | + try { |
| 24 | + setError(null) |
| 25 | + const now = new Date().toISOString() |
| 26 | + collection.insert({ |
| 27 | + id: crypto.randomUUID(), |
| 28 | + text: newTodoText.trim(), |
| 29 | + completed: false, |
| 30 | + createdAt: now, |
| 31 | + updatedAt: now, |
| 32 | + }) |
| 33 | + setNewTodoText(``) |
| 34 | + } catch (err) { |
| 35 | + setError(err instanceof Error ? err.message : `Failed to add todo`) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const handleToggleTodo = (id: string) => { |
| 40 | + try { |
| 41 | + setError(null) |
| 42 | + collection.update(id, (draft) => { |
| 43 | + draft.completed = !draft.completed |
| 44 | + draft.updatedAt = new Date().toISOString() |
| 45 | + }) |
| 46 | + } catch (err) { |
| 47 | + setError(err instanceof Error ? err.message : `Failed to toggle todo`) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const handleDeleteTodo = (id: string) => { |
| 52 | + try { |
| 53 | + setError(null) |
| 54 | + collection.delete(id) |
| 55 | + } catch (err) { |
| 56 | + setError(err instanceof Error ? err.message : `Failed to delete todo`) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + const handleKeyPress = (e: React.KeyboardEvent) => { |
| 61 | + if (e.key === `Enter`) { |
| 62 | + handleAddTodo() |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className="max-w-2xl mx-auto p-6"> |
| 68 | + <div className="bg-white rounded-lg shadow-lg p-6"> |
| 69 | + <div className="flex items-center gap-3 mb-4"> |
| 70 | + <span className="text-2xl">🗃️</span> |
| 71 | + <div> |
| 72 | + <h2 className="text-2xl font-bold text-gray-900"> |
| 73 | + wa-sqlite OPFS Persistence Demo |
| 74 | + </h2> |
| 75 | + <p className="text-gray-600"> |
| 76 | + Collection data is persisted to SQLite via OPFS. Data survives |
| 77 | + page reloads without any server sync. |
| 78 | + </p> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + |
| 82 | + {/* Persistence indicator */} |
| 83 | + <div className="flex flex-wrap gap-4 mb-6 text-sm"> |
| 84 | + <div className="flex items-center gap-2 px-3 py-1 rounded-full bg-emerald-100 text-emerald-800"> |
| 85 | + <div className="w-2 h-2 rounded-full bg-emerald-500" /> |
| 86 | + SQLite OPFS Persistence Active |
| 87 | + </div> |
| 88 | + <div className="flex items-center gap-2 px-3 py-1 rounded-full bg-gray-100 text-gray-600"> |
| 89 | + {todoList.length} todo{todoList.length !== 1 ? `s` : ``} |
| 90 | + </div> |
| 91 | + </div> |
| 92 | + |
| 93 | + {/* Error display */} |
| 94 | + {error && ( |
| 95 | + <div className="mb-4 p-3 bg-red-100 border border-red-300 rounded-md"> |
| 96 | + <p className="text-red-700 text-sm">{error}</p> |
| 97 | + </div> |
| 98 | + )} |
| 99 | + |
| 100 | + {/* Add new todo */} |
| 101 | + <div className="flex gap-2 mb-6"> |
| 102 | + <input |
| 103 | + type="text" |
| 104 | + value={newTodoText} |
| 105 | + onChange={(e) => setNewTodoText(e.target.value)} |
| 106 | + onKeyPress={handleKeyPress} |
| 107 | + placeholder="Add a new todo..." |
| 108 | + className="flex-1 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 109 | + /> |
| 110 | + <button |
| 111 | + onClick={handleAddTodo} |
| 112 | + disabled={!newTodoText.trim()} |
| 113 | + className="px-4 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600 disabled:opacity-50 disabled:cursor-not-allowed" |
| 114 | + > |
| 115 | + Add |
| 116 | + </button> |
| 117 | + </div> |
| 118 | + |
| 119 | + {/* Todo list */} |
| 120 | + <div className="space-y-2"> |
| 121 | + {todoList.length === 0 ? ( |
| 122 | + <div className="text-center py-8 text-gray-500"> |
| 123 | + No todos yet. Add one above to get started! |
| 124 | + <br /> |
| 125 | + <span className="text-xs"> |
| 126 | + Try adding todos, then refresh the page to see them persist |
| 127 | + </span> |
| 128 | + </div> |
| 129 | + ) : ( |
| 130 | + todoList.map((todo) => ( |
| 131 | + <div |
| 132 | + key={todo.id} |
| 133 | + className="flex items-center gap-3 p-3 border border-gray-200 rounded-md hover:bg-gray-50" |
| 134 | + > |
| 135 | + <button |
| 136 | + onClick={() => handleToggleTodo(todo.id)} |
| 137 | + className={`w-5 h-5 rounded border-2 flex items-center justify-center ${ |
| 138 | + todo.completed |
| 139 | + ? `bg-green-500 border-green-500 text-white` |
| 140 | + : `border-gray-300 hover:border-green-400` |
| 141 | + }`} |
| 142 | + > |
| 143 | + {todo.completed && <span className="text-xs">✓</span>} |
| 144 | + </button> |
| 145 | + <span |
| 146 | + className={`flex-1 ${ |
| 147 | + todo.completed |
| 148 | + ? `line-through text-gray-500` |
| 149 | + : `text-gray-900` |
| 150 | + }`} |
| 151 | + > |
| 152 | + {todo.text} |
| 153 | + </span> |
| 154 | + <span className="text-xs text-gray-400"> |
| 155 | + {new Date(todo.createdAt).toLocaleDateString()} |
| 156 | + </span> |
| 157 | + <button |
| 158 | + onClick={() => handleDeleteTodo(todo.id)} |
| 159 | + className="px-2 py-1 text-red-600 hover:bg-red-50 rounded text-sm" |
| 160 | + > |
| 161 | + Delete |
| 162 | + </button> |
| 163 | + </div> |
| 164 | + )) |
| 165 | + )} |
| 166 | + </div> |
| 167 | + |
| 168 | + {/* Instructions */} |
| 169 | + <div className="mt-6 p-4 bg-gray-50 rounded-md"> |
| 170 | + <h3 className="font-medium text-gray-900 mb-2">Try this:</h3> |
| 171 | + <ol className="text-sm text-gray-600 space-y-1"> |
| 172 | + <li>1. Add some todos</li> |
| 173 | + <li>2. Refresh the page (Ctrl+R / Cmd+R)</li> |
| 174 | + <li>3. Your todos are still here - persisted in SQLite via OPFS!</li> |
| 175 | + <li> |
| 176 | + 4. This uses wa-sqlite with OPFSCoopSyncVFS in a Web Worker |
| 177 | + </li> |
| 178 | + </ol> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + ) |
| 183 | +} |
0 commit comments