Skip to content

Commit 8c7d7b9

Browse files
committed
feat: improve new chat button UX with loading state
- Add isCreatingChat state to track new chat creation progress - Update New Chat button to show "Creating..." text and disabled state during creation - Pass loading state through component hierarchy (App -> Chatbox -> ChatMessagesDisplay) - Prevent multiple simultaneous new chat creation attempts This provides better user feedback when creating new conversations and prevents accidental duplicate clicks during the async creation process.
1 parent e14f2a4 commit 8c7d7b9

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

src/components/ChatMessagesDisplay.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ interface ChatMessagesDisplayProps {
5555
onCompletion: () => Promise<void>;
5656
onRegenerate: () => Promise<void>;
5757
onClearSystem: () => void;
58+
isCreatingChat: boolean;
5859
}
5960

6061
export function ChatMessagesDisplay({
@@ -66,6 +67,7 @@ export function ChatMessagesDisplay({
6667
onCompletion,
6768
onRegenerate,
6869
onClearSystem,
70+
isCreatingChat,
6971
}: ChatMessagesDisplayProps) {
7072
return (
7173
<ChatMessageList>
@@ -153,10 +155,10 @@ export function ChatMessagesDisplay({
153155
variant="secondary"
154156
size="sm"
155157
className="m-2"
156-
disabled={showGenerating}
158+
disabled={showGenerating || isCreatingChat}
157159
onClick={onNewChat}
158160
>
159-
<Tr>New Chat</Tr>
161+
{isCreatingChat ? "Creating..." : <Tr>New Chat</Tr>}
160162
</Button>
161163
)}
162164
{chatStore.develop_mode && chatStore.history.length > 0 && (

src/pages/App.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ interface AppContextType {
6161
) => void;
6262
handleNewChatStore: () => Promise<void>;
6363
handleNewChatStoreWithOldOne: (chatStore: ChatStore) => Promise<void>;
64+
isCreatingChat: boolean;
6465
}
6566

6667
interface AppChatStoreContextType {
@@ -164,13 +165,20 @@ export function App() {
164165
);
165166

166167
const handleNewChatStoreWithOldOne = async (chatStore: ChatStore) => {
167-
const newKey = await (await db).add(STORAGE_NAME, newChatStore(chatStore));
168-
setSelectedChatIndex(newKey as number);
169-
setAllChatStoreIndexes(await (await db).getAllKeys(STORAGE_NAME));
170-
toast({
171-
title: "New chat created",
172-
description: `Current API Endpoint: ${chatStore.apiEndpoint}`,
173-
});
168+
setIsCreatingChat(true);
169+
try {
170+
const newKey = await (
171+
await db
172+
).add(STORAGE_NAME, newChatStore(chatStore));
173+
setSelectedChatIndex(newKey as number);
174+
setAllChatStoreIndexes(await (await db).getAllKeys(STORAGE_NAME));
175+
toast({
176+
title: "New chat created",
177+
description: `Current API Endpoint: ${chatStore.apiEndpoint}`,
178+
});
179+
} finally {
180+
setIsCreatingChat(false);
181+
}
174182
};
175183
const handleNewChatStore = async () => {
176184
let currentChatStore = await getChatStoreByIndex(selectedChatIndex);
@@ -210,6 +218,7 @@ export function App() {
210218
};
211219

212220
const [showImportDialog, setShowImportDialog] = useState(false);
221+
const [isCreatingChat, setIsCreatingChat] = useState(false);
213222
// if there are any params in URL, show the alert dialog to import configure
214223
useEffect(() => {
215224
const run = async () => {
@@ -421,15 +430,14 @@ export function App() {
421430
setCallingTools,
422431
handleNewChatStore,
423432
handleNewChatStoreWithOldOne,
433+
isCreatingChat,
424434
}}
425435
>
426436
<SidebarProvider defaultOpen={true}>
427437
<Sidebar>
428438
<SidebarHeader>
429-
<Button onClick={handleNewChatStore}>
430-
<span>
431-
<Tr>New Chat</Tr>
432-
</span>
439+
<Button onClick={handleNewChatStore} disabled={isCreatingChat}>
440+
<span>{isCreatingChat ? "Creating..." : <Tr>New Chat</Tr>}</span>
433441
</Button>
434442
</SidebarHeader>
435443
<SidebarContent>

src/pages/Chatbox.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default function ChatBOX() {
1818
handleNewChatStore,
1919
callingTools,
2020
setCallingTools,
21+
isCreatingChat,
2122
} = useContext(AppContext);
2223
const { langCode, setLangCode } = useContext(langCodeContext);
2324
const { chatStore, setChatStore } = useContext(AppChatStoreContext);
@@ -169,6 +170,7 @@ export default function ChatBOX() {
169170
onCompletion={handleComplete}
170171
onRegenerate={handleRegenerate}
171172
onClearSystem={handleClearSystem}
173+
isCreatingChat={isCreatingChat}
172174
/>
173175
<div id="message-end" ref={messagesEndRef as any}></div>
174176
</div>

0 commit comments

Comments
 (0)