Skip to content

Commit 6fcaaf1

Browse files
committed
refactor: replace isSending with isBusy in Chat components and update session store for improved state management
1 parent bb15595 commit 6fcaaf1

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

src/components/ChatView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function ChatView() {
2626
const currentEvents = activeConversationId
2727
? events[activeConversationId] || []
2828
: [];
29-
const { interrupt, isSending, beginPendingConversation, handleSendMessage } =
29+
const { interrupt, isBusy, beginPendingConversation, handleSendMessage } =
3030
useSendMessage();
3131
const streamingMessages = useEventStreamStore((state) =>
3232
activeConversationId ? state.streaming[activeConversationId] : undefined,
@@ -97,7 +97,7 @@ export function ChatView() {
9797
onStopStreaming={() =>
9898
activeConversationId && interrupt(activeConversationId)
9999
}
100-
disabled={isSending}
100+
isBusy={isBusy}
101101
/>
102102
</div>
103103
</div>

src/components/chat/ChatCompose.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ interface ChatComposeProps {
99
onInputChange: (value: string) => void;
1010
onSendMessage: (text: string, attachments: MediaAttachment[]) => void;
1111
onStopStreaming: () => void;
12-
disabled: boolean;
12+
isBusy: boolean;
1313
}
1414

1515
export function ChatCompose({
1616
inputValue,
1717
onInputChange,
1818
onSendMessage,
1919
onStopStreaming,
20-
disabled,
20+
isBusy,
2121
}: ChatComposeProps) {
2222
return (
2323
<div className="border-t bg-background px-2">
@@ -26,7 +26,8 @@ export function ChatCompose({
2626
onInputChange={onInputChange}
2727
onSendMessage={onSendMessage}
2828
onStopStreaming={onStopStreaming}
29-
disabled={disabled}
29+
disabled={isBusy}
30+
isLoading={isBusy}
3031
/>
3132
<div className="flex flex-wrap items-center">
3233
<Sandbox />

src/hooks/useCodex/useConversationEvents.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { invoke, listen } from "@/lib/tauri-proxy";
22
import { useEffect, useRef } from "react";
33
import { ConversationId } from "@/bindings/ConversationId";
44
import { CodexEvent } from "@/types/chat";
5+
import { useSessionStore } from "@/stores/useSessionStore";
56

67
interface EventHandlers {
78
isConversationReady?: boolean;
@@ -35,6 +36,7 @@ export function useConversationEvents(
3536
{ isConversationReady = false, ...handlers }: EventHandlers,
3637
) {
3738
const handlersRef = useRef<EventHandlers>(handlers);
39+
const setIsBusy = useSessionStore((state) => state.setIsBusy);
3840

3941
useEffect(() => {
4042
handlersRef.current = handlers;
@@ -63,6 +65,11 @@ export function useConversationEvents(
6365
}
6466

6567
currentHandlers.onAnyEvent?.(event);
68+
const busyOff =
69+
msg.type === "error" ||
70+
msg.type === "task_complete" ||
71+
msg.type === "turn_aborted";
72+
setIsBusy(!busyOff);
6673

6774
switch (msg.type) {
6875
case "task_started":
@@ -160,6 +167,7 @@ export function useConversationEvents(
160167
params: { subscriptionId },
161168
});
162169
}
170+
setIsBusy(false);
163171
};
164-
}, [conversationId, isConversationReady]);
172+
}, [conversationId, isConversationReady, setIsBusy]);
165173
}

src/hooks/useCodex/useSendMessage.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useState } from "react";
21
import { invoke } from "@/lib/tauri-proxy";
32
import { InputItem } from "@/bindings/InputItem";
43
import { useBuildNewConversationParams } from "@/hooks/useBuildNewConversationParams";
@@ -9,9 +8,11 @@ import { useCodexStore } from "@/stores/useCodexStore";
98
import { useEventStore } from "@/stores/useEventStore";
109
import { useConversation } from "./useConversation";
1110
import { useActiveConversationStore } from "@/stores/useActiveConversationStore";
11+
import { useSessionStore } from "@/stores/useSessionStore";
1212

1313
export function useSendMessage() {
14-
const [isSending, setIsSending] = useState(false);
14+
const isBusy = useSessionStore((state) => state.isBusy);
15+
const setIsBusy = useSessionStore((state) => state.setIsBusy);
1516
const buildNewConversationParams = useBuildNewConversationParams();
1617
const { cwd } = useCodexStore();
1718
const { clearEvents } = useEventStore();
@@ -21,7 +22,7 @@ export function useSendMessage() {
2122
);
2223

2324
const sendMessage = async (conversationId: string, items: InputItem[]) => {
24-
setIsSending(true);
25+
setIsBusy(true);
2526
try {
2627
await invoke("send_user_message", {
2728
params: {
@@ -30,8 +31,9 @@ export function useSendMessage() {
3031
},
3132
});
3233
markConversationReady();
33-
} finally {
34-
setIsSending(false);
34+
} catch (error) {
35+
setIsBusy(false);
36+
throw error;
3537
}
3638
};
3739

@@ -69,7 +71,9 @@ export function useSendMessage() {
6971
attachments,
7072
);
7173
console.log("sendMessage params:", params);
72-
sendMessage(currentConversationId, params.items);
74+
void sendMessage(currentConversationId, params.items).catch((error) => {
75+
console.error("Failed to send message:", error);
76+
});
7377
};
7478

7579
const handleCreateConversation = async (
@@ -100,7 +104,7 @@ export function useSendMessage() {
100104
return {
101105
sendMessage,
102106
interrupt,
103-
isSending,
107+
isBusy,
104108
handleCreateConversation,
105109
handleSendMessage,
106110
beginPendingConversation,

src/stores/useSessionStore.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import { create } from "zustand";
22

33
interface SessionState {
44
isInitializing: boolean;
5-
isSending: boolean;
5+
isBusy: boolean;
66
}
77

88
interface SessionActions {
99
setIsInitializing: (value: boolean) => void;
10-
setIsSending: (value: boolean) => void;
10+
setIsBusy: (value: boolean) => void;
1111
reset: () => void;
1212
}
1313

1414
export const useSessionStore = create<SessionState & SessionActions>()(
1515
(set) => ({
1616
isInitializing: false,
17-
isSending: false,
17+
isBusy: false,
1818
setIsInitializing: (value) => set({ isInitializing: value }),
19-
setIsSending: (value) => set({ isSending: value }),
20-
reset: () => set({ isInitializing: false, isSending: false }),
19+
setIsBusy: (value) => set({ isBusy: value }),
20+
reset: () => set({ isInitializing: false, isBusy: false }),
2121
}),
2222
);

0 commit comments

Comments
 (0)