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

useTodos.ts

156 lines4.1 KB

1import { useCallback, useEffect, useState } from 'react'
2import type { RefObject } from 'react'
3import type { RpcStub } from 'capnweb'
4import type {
5 ChatApi,
6 ChatNotification,
7 ClaudeMode,
8 TodoItem,
9} from '../../chat-server/chat-api'
10import type { ChatNotifier } from '@/lib/chat-notifier'
11​
12function formatError(error: unknown) {
13 if (error instanceof Error) return error.message
14 if (typeof error === 'object' && error !== null && 'message' in error) {
15 return String(error.message)
16 }
17 return String(error)
19​
20export function useTodos(
21 api: RpcStub<ChatApi> | null,
22 apiRef: RefObject<RpcStub<ChatApi> | null>,
23 notifier: ChatNotifier | null,
24 isConnected: boolean,
25 isJoined: boolean,
26) {
27 const getApi = useCallback(() => apiRef.current ?? api, [api, apiRef])
28 const [todos, setTodos] = useState<Array<TodoItem>>([])
29 const [claudeMode, setClaudeMode] = useState<ClaudeMode>('passive')
30 const [error, setError] = useState<string | null>(null)
31​
32 useEffect(() => {
33 if (!notifier) return
34​
35 return notifier.addHandler((notification: ChatNotification) => {
36 if (notification.type === 'todos_updated' && notification.todos) {
37 setTodos(notification.todos)
38 }
39 if (
40 notification.type === 'claude_mode_changed' &&
41 notification.claudeMode
42 ) {
43 setClaudeMode(notification.claudeMode)
44 }
45 })
46 }, [notifier])
47​
48 useEffect(() => {
49 const activeApi = getApi()
50 if (!activeApi || !isConnected || !isJoined) {
51 if (!isJoined) {
52 setTodos([])
53 setClaudeMode('passive')
54 }
55 return
56 }
57​
58 let cancelled = false
59​
60 const sync = async () => {
61 try {
62 const state = await activeApi.getChatState()
63 if (cancelled) return
64 setTodos(state.todos)
65 setClaudeMode(state.claudeMode)
66 setError(null)
67 } catch (err) {
68 if (!cancelled) {
69 console.error('Error syncing todos:', formatError(err), err)
70 setError(formatError(err))
71 }
72 }
73 }
74​
75 void sync()
76​
77 return () => {
78 cancelled = true
79 }
80 }, [getApi, isConnected, isJoined])
81​
82 const addTodo = useCallback(
83 async (text: string) => {
84 const activeApi = getApi()
85 if (!activeApi || !isJoined) {
86 return { success: false as const, error: 'Not joined' }
87 }
88​
89 try {
90 const result = await activeApi.addTodo(text)
91 setTodos(result.todos)
92 setError(null)
93 return { success: true as const, todo: result.todo }
94 } catch (err) {
95 const message = formatError(err)
96 console.error('Error adding todo:', message, err)
97 setError(message)
98 return { success: false as const, error: message }
99 }
100 },
101 [getApi, isJoined],
102 )
103​
104 const removeTodo = useCallback(
105 async (id: string) => {
106 const activeApi = getApi()
107 if (!activeApi || !isJoined) {
108 return { success: false as const, error: 'Not joined' }
109 }
110​
111 try {
112 const result = await activeApi.removeTodo(id)
113 setTodos(result.todos)
114 setError(null)
115 return { success: result.success }
116 } catch (err) {
117 const message = formatError(err)
118 console.error('Error removing todo:', message, err)
119 setError(message)
120 return { success: false as const, error: message }
121 }
122 },
123 [getApi, isJoined],
124 )
125​
126 const updateClaudeMode = useCallback(
127 async (mode: ClaudeMode) => {
128 const activeApi = getApi()
129 if (!activeApi || !isJoined) {
130 return { success: false as const, error: 'Not joined' }
131 }
132​
133 try {
134 const result = await activeApi.setClaudeMode(mode)
135 setClaudeMode(result.mode)
136 setError(null)
137 return { success: true as const, mode: result.mode }
138 } catch (err) {
139 const message = formatError(err)
140 console.error('Error setting Claude mode:', message, err)
141 setError(message)
142 return { success: false as const, error: message }
143 }
144 },
145 [getApi, isJoined],
146 )
147​
148 return {
149 todos,
150 claudeMode,
151 error,
152 addTodo,
153 removeTodo,
154 setClaudeMode: updateClaudeMode,
155 }