mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-07-30 02:32:03 +08:00
chore: remove user store
This commit is contained in:
@ -1,15 +1,14 @@
|
|||||||
import { Menu, Popover } from "@headlessui/react";
|
import { Menu, Popover } from "@headlessui/react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { useChatStore, useMessageStore, useUserStore } from "../../store";
|
import { getAssistantById, useChatStore, useMessageStore } from "../../store";
|
||||||
import Icon from "../Icon";
|
import Icon from "../Icon";
|
||||||
|
|
||||||
const Header = () => {
|
const Header = () => {
|
||||||
const userStore = useUserStore();
|
|
||||||
const chatStore = useChatStore();
|
const chatStore = useChatStore();
|
||||||
const messageStore = useMessageStore();
|
const messageStore = useMessageStore();
|
||||||
const currentChat = chatStore.currentChat;
|
const currentChat = chatStore.currentChat;
|
||||||
const assistant = userStore.getAssistantById(currentChat?.assistantId)!;
|
const assistant = getAssistantById(currentChat?.assistantId)!;
|
||||||
|
|
||||||
const handleClearMessage = () => {
|
const handleClearMessage = () => {
|
||||||
messageStore.clearMessage((message) => message.chatId !== currentChat?.id);
|
messageStore.clearMessage((message) => message.chatId !== currentChat?.id);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import TextareaAutosize from "react-textarea-autosize";
|
import TextareaAutosize from "react-textarea-autosize";
|
||||||
import { useChatStore, useMessageStore, useUserStore } from "../../store";
|
import { localUser, useChatStore, useMessageStore } from "../../store";
|
||||||
import { generateUUID } from "../../utils";
|
import { generateUUID } from "../../utils";
|
||||||
import Icon from "../Icon";
|
import Icon from "../Icon";
|
||||||
|
|
||||||
@ -12,7 +12,6 @@ interface Props {
|
|||||||
|
|
||||||
const MessageTextarea = (props: Props) => {
|
const MessageTextarea = (props: Props) => {
|
||||||
const { disabled, sendMessage } = props;
|
const { disabled, sendMessage } = props;
|
||||||
const userStore = useUserStore();
|
|
||||||
const chatStore = useChatStore();
|
const chatStore = useChatStore();
|
||||||
const messageStore = useMessageStore();
|
const messageStore = useMessageStore();
|
||||||
const [value, setValue] = useState<string>("");
|
const [value, setValue] = useState<string>("");
|
||||||
@ -45,7 +44,7 @@ const MessageTextarea = (props: Props) => {
|
|||||||
messageStore.addMessage({
|
messageStore.addMessage({
|
||||||
id: generateUUID(),
|
id: generateUUID(),
|
||||||
chatId: chatStore.currentChat.id,
|
chatId: chatStore.currentChat.id,
|
||||||
creatorId: userStore.currentUser.id,
|
creatorId: localUser.id,
|
||||||
createdAt: Date.now(),
|
createdAt: Date.now(),
|
||||||
content: value,
|
content: value,
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { marked } from "marked";
|
import { marked } from "marked";
|
||||||
import { useUserStore } from "../../store";
|
import { localUser } from "../../store";
|
||||||
import { Message } from "../../types";
|
import { Message } from "../../types";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
@ -8,9 +8,7 @@ interface Props {
|
|||||||
|
|
||||||
const MessageView = (props: Props) => {
|
const MessageView = (props: Props) => {
|
||||||
const message = props.message;
|
const message = props.message;
|
||||||
const userStore = useUserStore();
|
const isCurrentUser = message.creatorId === localUser.id;
|
||||||
const currentUser = userStore.currentUser;
|
|
||||||
const isCurrentUser = message.creatorId === currentUser.id;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`w-full flex flex-row justify-start items-start my-4 ${isCurrentUser ? "justify-end pl-8 sm:pl-24" : "pr-8 sm:pr-24"}`}>
|
<div className={`w-full flex flex-row justify-start items-start my-4 ${isCurrentUser ? "justify-end pl-8 sm:pl-24" : "pr-8 sm:pr-24"}`}>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { defaultChat, getPromptOfAssistant, useChatStore, useMessageStore, useUserStore } from "../../store";
|
import { defaultChat, getAssistantById, getPromptOfAssistant, localUser, useChatStore, useMessageStore } from "../../store";
|
||||||
import { Chat, Message, UserRole } from "../../types";
|
import { Chat, Message, UserRole } from "../../types";
|
||||||
import { generateUUID } from "../../utils";
|
import { generateUUID } from "../../utils";
|
||||||
import Icon from "../Icon";
|
import Icon from "../Icon";
|
||||||
@ -10,7 +10,6 @@ import MessageTextarea from "./MessageTextarea";
|
|||||||
|
|
||||||
const ChatView = () => {
|
const ChatView = () => {
|
||||||
const chatStore = useChatStore();
|
const chatStore = useChatStore();
|
||||||
const userStore = useUserStore();
|
|
||||||
const messageStore = useMessageStore();
|
const messageStore = useMessageStore();
|
||||||
const [messageList, setMessageList] = useState<Message[]>([]);
|
const [messageList, setMessageList] = useState<Message[]>([]);
|
||||||
const [currentChat, setCurrentChat] = useState<Chat | null>(null);
|
const [currentChat, setCurrentChat] = useState<Chat | null>(null);
|
||||||
@ -18,11 +17,11 @@ const ChatView = () => {
|
|||||||
const chatViewRef = useRef<HTMLDivElement>(null);
|
const chatViewRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!userStore.getAssistantById(chatStore.currentChat.assistantId)) {
|
if (!getAssistantById(chatStore.currentChat.assistantId)) {
|
||||||
chatStore.setCurrentChat(defaultChat);
|
chatStore.setCurrentChat(defaultChat);
|
||||||
}
|
}
|
||||||
setCurrentChat(chatStore.currentChat);
|
setCurrentChat(chatStore.currentChat);
|
||||||
}, [chatStore, userStore]);
|
}, [chatStore]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setMessageList(messageStore.messageList.filter((message) => message.chatId === currentChat?.id));
|
setMessageList(messageStore.messageList.filter((message) => message.chatId === currentChat?.id));
|
||||||
@ -47,7 +46,7 @@ const ChatView = () => {
|
|||||||
|
|
||||||
setIsRequesting(true);
|
setIsRequesting(true);
|
||||||
const messageList = messageStore.getState().messageList.filter((message) => message.chatId === currentChat.id);
|
const messageList = messageStore.getState().messageList.filter((message) => message.chatId === currentChat.id);
|
||||||
const prompt = getPromptOfAssistant(userStore.getAssistantById(currentChat.assistantId)!);
|
const prompt = getPromptOfAssistant(getAssistantById(currentChat.assistantId)!);
|
||||||
const { data } = await axios.post<string>("/api/chat", {
|
const { data } = await axios.post<string>("/api/chat", {
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
@ -55,7 +54,7 @@ const ChatView = () => {
|
|||||||
content: prompt,
|
content: prompt,
|
||||||
},
|
},
|
||||||
...messageList.map((message) => ({
|
...messageList.map((message) => ({
|
||||||
role: message.creatorId === userStore.currentUser.id ? UserRole.User : UserRole.Assistant,
|
role: message.creatorId === localUser.id ? UserRole.User : UserRole.Assistant,
|
||||||
content: message.content,
|
content: message.content,
|
||||||
})),
|
})),
|
||||||
],
|
],
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { User, UserRole } from "../types";
|
import { Id, User, UserRole } from "../types";
|
||||||
|
|
||||||
// Assistant is a special user.
|
// Assistant is a special user.
|
||||||
export const assistantList: User[] = [
|
export const assistantList: User[] = [
|
||||||
@ -11,6 +11,11 @@ export const assistantList: User[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const getAssistantById = (id: Id) => {
|
||||||
|
const user = assistantList.find((user) => user.id === id);
|
||||||
|
return user || undefined;
|
||||||
|
};
|
||||||
|
|
||||||
// getPromptOfAssistant define the special prompt for each assistant.
|
// getPromptOfAssistant define the special prompt for each assistant.
|
||||||
export const getPromptOfAssistant = (assistant: User) => {
|
export const getPromptOfAssistant = (assistant: User) => {
|
||||||
if (assistant.id === "assistant-dba") {
|
if (assistant.id === "assistant-dba") {
|
||||||
|
@ -1,26 +1,9 @@
|
|||||||
import { create } from "zustand";
|
import { User, UserRole } from "../types";
|
||||||
import { assistantList } from ".";
|
|
||||||
import { Id, User, UserRole } from "../types";
|
|
||||||
|
|
||||||
const localUser: User = {
|
export const localUser: User = {
|
||||||
id: "local-user",
|
id: "local-user",
|
||||||
name: "Local user",
|
name: "Local user",
|
||||||
description: "",
|
description: "",
|
||||||
avatar: "",
|
avatar: "",
|
||||||
role: UserRole.User,
|
role: UserRole.User,
|
||||||
};
|
};
|
||||||
|
|
||||||
interface UserState {
|
|
||||||
assistantList: User[];
|
|
||||||
currentUser: User;
|
|
||||||
getAssistantById: (id: string) => User | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useUserStore = create<UserState>()(() => ({
|
|
||||||
assistantList: assistantList,
|
|
||||||
currentUser: localUser,
|
|
||||||
getAssistantById: (id: Id) => {
|
|
||||||
const user = assistantList.find((user) => user.id === id);
|
|
||||||
return user || undefined;
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
Reference in New Issue
Block a user