Skip to content

Commit 3da1628

Browse files
koki-developclaude
andcommitted
refactor: Extract cat logic to Cat class and improve message ID generation
- Create Cat class in src/lib/cat.ts with async response method - Refactor App.tsx to use Cat class instead of inline logic - Replace incremental ID generation with Date.now() to prevent duplicates - Improve input handling with trim() and functional state updates 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bb06d15 commit 3da1628

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/App.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,41 @@ import { ChatHistory } from "./components/ChatHistory";
55
import { InputField } from "./components/InputField";
66
import { Spinner } from "./components/Spinner";
77
import type { Message } from "./components/types";
8+
import { Cat } from "./lib/cat";
9+
10+
const cat = new Cat();
811

912
export const App: React.FC = () => {
1013
const [messages, setMessages] = useState<Message[]>([]);
1114
const [input, setInput] = useState("");
1215
const [isLoading, setIsLoading] = useState(false);
1316

14-
const handleSubmit = () => {
17+
const handleSubmit = async () => {
1518
if (input.trim() === "" || isLoading) return;
1619

1720
const newMessage: Message = {
18-
id: messages.length + 1,
19-
text: input,
21+
id: Date.now(),
22+
text: input.trim(),
2023
sender: "user",
2124
};
2225

23-
setMessages([...messages, newMessage]);
26+
setMessages((prev) => [...prev, newMessage]);
2427
setInput("");
2528
setIsLoading(true);
2629

27-
setTimeout(() => {
28-
const response: Message = {
29-
id: messages.length + 2,
30-
text: "ニャー",
31-
sender: "cat",
32-
};
33-
setMessages((prev) => [...prev, response]);
34-
setIsLoading(false);
35-
}, 500);
30+
await cat
31+
.response(newMessage.text)
32+
.then((response) => {
33+
const message: Message = {
34+
id: Date.now(),
35+
text: response,
36+
sender: "cat",
37+
};
38+
setMessages((prev) => [...prev, message]);
39+
})
40+
.finally(() => {
41+
setIsLoading(false);
42+
});
3643
};
3744

3845
return (

src/lib/cat.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Cat {
2+
async response(_message: string): Promise<string> {
3+
await new Promise((resolve) => setTimeout(resolve, 500));
4+
return "ニャー";
5+
}
6+
}

0 commit comments

Comments
 (0)