MCPcopy Create free account
hub / github.com/TanStack/db / TodoList

Function TodoList

examples/electron/offline-first/src/components/TodoList.tsx:12–193  ·  view source on GitHub ↗
({ collection, executor }: TodoListProps)

Source from the content-addressed store, hash-verified

10}
11
12export 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) {

Callers

nothing calls this directly

Calls 12

createTodoActionsFunction · 0.90
useLiveQueryFunction · 0.90
orderByMethod · 0.80
fromMethod · 0.80
addTodoMethod · 0.80
toggleTodoMethod · 0.80
deleteTodoMethod · 0.80
handleAddTodoFunction · 0.50
addEventListenerMethod · 0.45
removeEventListenerMethod · 0.45
getPendingCountMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected