({ collection, executor }: TodoListProps)
| 10 | } |
| 11 | |
| 12 | export function TodoList({ collection, executor }: TodoListProps) { |
| 13 | const [inputText, setInputText] = useState('') |
| 14 | const [isOnline, setIsOnline] = useState(navigator.onLine) |
| 15 | const [pendingCount, setPendingCount] = useState(0) |
| 16 | const [error, setError] = useState<string | null>(null) |
| 17 | const [actions] = useState(() => createTodoActions(executor)) |
| 18 | |
| 19 | // Monitor network status |
| 20 | useEffect(() => { |
| 21 | const handleOnline = () => { |
| 22 | setIsOnline(true) |
| 23 | executor.notifyOnline() |
| 24 | } |
| 25 | const handleOffline = () => setIsOnline(false) |
| 26 | |
| 27 | window.addEventListener('online', handleOnline) |
| 28 | window.addEventListener('offline', handleOffline) |
| 29 | return () => { |
| 30 | window.removeEventListener('online', handleOnline) |
| 31 | window.removeEventListener('offline', handleOffline) |
| 32 | } |
| 33 | }, [executor]) |
| 34 | |
| 35 | // Poll pending mutation count |
| 36 | useEffect(() => { |
| 37 | const interval = setInterval(() => { |
| 38 | setPendingCount(executor.getPendingCount()) |
| 39 | }, 100) |
| 40 | return () => clearInterval(interval) |
| 41 | }, [executor]) |
| 42 | |
| 43 | // Query all todos sorted by creation date |
| 44 | const { data: todos = [], isLoading } = useLiveQuery((query) => |
| 45 | query |
| 46 | .from({ todo: collection }) |
| 47 | .orderBy(({ todo }) => todo.createdAt, 'desc'), |
| 48 | ) |
| 49 | |
| 50 | const handleAddTodo = useCallback(() => { |
| 51 | const text = inputText.trim() |
| 52 | if (!text) return |
| 53 | try { |
| 54 | setError(null) |
| 55 | actions.addTodo(text) |
| 56 | setInputText('') |
| 57 | } catch (err) { |
| 58 | setError(err instanceof Error ? err.message : 'Failed to add todo') |
| 59 | } |
| 60 | }, [inputText, actions]) |
| 61 | |
| 62 | const handleKeyDown = useCallback( |
| 63 | (e: React.KeyboardEvent) => { |
| 64 | if (e.key === 'Enter') handleAddTodo() |
| 65 | }, |
| 66 | [handleAddTodo], |
| 67 | ) |
| 68 | |
| 69 | if (isLoading) { |
nothing calls this directly
no test coverage detected