Open History to see the last commit
1import { useEffect, useRef, useState } from 'react'
2import { createTodos } from './db/todos'
3import { TodoList } from './components/TodoList'
4import './App.css'
5
6type TodosState = ReturnType<typeof createTodos>
7
8export function App() {
9 const [todosState, setTodosState] = useState<TodosState | null>(null)
10 const [error, setError] = useState<string | null>(null)
11 const initRef = useRef(false)
12
13 useEffect(() => {
14 // Prevent double-initialization from React StrictMode.
15 // The offline executor acquires a Web Lock for leadership — disposing
16 // and re-creating it in the StrictMode mount→cleanup→mount cycle
17 // leaves a ghost lock that blocks the second executor from becoming leader.
18 if (initRef.current) return
19 initRef.current = true
20
21 try {
22 const state = createTodos()
23 setTodosState(state)
24 } catch (err) {
25 console.error('Failed to initialize:', err)
26 setError(err instanceof Error ? err.message : String(err))
27 }
28 }, [])
29
30 if (error) {
31 return (
32 <div className="container">
33 <h1>Initialization Error</h1>
34 <p style={{ color: '#ef4444' }}>{error}</p>
35 </div>
36 )
37 }
38
39 if (!todosState) {
40 return (
41 <div className="container">
42 <p>Loading...</p>
43 </div>
44 )
45 }
46
47 return (
48 <TodoList
49 collection={todosState.collection}
50 executor={todosState.executor}
51 />
52 )
53}