Skip to content

Commit

Permalink
feat: add new chat button in header
Browse files Browse the repository at this point in the history
  • Loading branch information
sunls24 committed Dec 9, 2024
1 parent 075e22c commit 129482a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 15 deletions.
25 changes: 25 additions & 0 deletions components/chat-new.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use client";
import React from "react";
import { ListPlus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { emitter, mittKey } from "@/lib/mitt";
import { toast } from "sonner";
import { useChatStore } from "@/lib/store/chat";

function ChatNew() {
const newSession = useChatStore((state) => state.newSession);

function onCreate() {
emitter.emit(mittKey.STOP_LOADING);
toast.success("已创建新的聊天");
setTimeout(newSession);
}

return (
<Button variant="ghost" size="icon" onClick={onCreate}>
<ListPlus strokeWidth={1.4} />
</Button>
);
}

export default ChatNew;
1 change: 1 addition & 0 deletions components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function Chat() {
});

const { append: topicAppend, setMessages: topicSetMessages } = useChat({
streamProtocol: "text",
onFinish(msg) {
checkAutoTopic(() => updateCurrentTopic(trimTopic(msg.content)));
},
Expand Down
6 changes: 5 additions & 1 deletion components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Button } from "@/components/ui/button";
import HeaderTopic from "@/components/header-topic";
import ChatList from "@/components/dialog/chat-list";
import Settings from "@/components/settings/settings";
import { Separator } from "@/components/ui/separator";
import ChatNew from "@/components/chat-new";

function Header() {
return (
Expand All @@ -12,10 +14,12 @@ function Header() {
<ChatList
trigger={
<Button variant="ghost" size="icon">
<GalleryVertical strokeWidth={1.5} size={20} />
<GalleryVertical strokeWidth={1.6} size={20} />
</Button>
}
/>
<Separator orientation="vertical" className="h-1/2" />
<ChatNew />
<span className="flex-1" />
<Settings
trigger={
Expand Down
4 changes: 2 additions & 2 deletions lib/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ export const useChatStore = create<ChatStore>()(
set({ sessions: get().sessions });
},

checkAutoTopic(newTopic: () => void) {
checkAutoTopic(cb: () => void) {
const currentSession = get().currentSession();
if (
currentSession.topic !== DEFAULT_TOPIC ||
currentSession.messages.length < AUTO_TOPIC_LENGTH
) {
return;
}
newTopic();
cb();
},

resetSession() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"react-textarea-autosize": "^8.5.5",
"remark-breaks": "3.0.3",
"remark-gfm": "3.0.1",
"sonner": "^1.7.0",
"sonner": "^1.7.1",
"tailwind-merge": "^2.5.5",
"zod": "^3.23.8",
"zustand": "^5.0.2"
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 129482a

Please sign in to comment.