mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-08-02 22:58:43 +08:00
chore: update connection store
This commit is contained in:
@ -3,11 +3,15 @@ import { persist } from "zustand/middleware";
|
||||
import { Chat, UNKNOWN_ID, User } from "@/types";
|
||||
import { generateUUID } from "@/utils";
|
||||
|
||||
export const defaultChat: Chat = {
|
||||
id: generateUUID(),
|
||||
connectionId: UNKNOWN_ID,
|
||||
databaseName: "",
|
||||
assistantId: "sql-assistant",
|
||||
const getDefaultChat = (): Chat => {
|
||||
return {
|
||||
id: generateUUID(),
|
||||
connectionId: UNKNOWN_ID,
|
||||
databaseName: "",
|
||||
assistantId: "sql-assistant",
|
||||
title: "default chat",
|
||||
createdAt: Date.now(),
|
||||
};
|
||||
};
|
||||
|
||||
interface ChatState {
|
||||
@ -20,13 +24,11 @@ interface ChatState {
|
||||
export const useChatStore = create<ChatState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
chatList: [defaultChat],
|
||||
currentChat: defaultChat,
|
||||
chatList: [getDefaultChat()],
|
||||
currentChat: getDefaultChat(),
|
||||
createChat: (assistant: User) => {
|
||||
const chat: Chat = {
|
||||
id: generateUUID(),
|
||||
connectionId: UNKNOWN_ID,
|
||||
databaseName: "",
|
||||
...getDefaultChat(),
|
||||
assistantId: assistant.id,
|
||||
};
|
||||
set((state) => ({
|
||||
|
37
store/connection.ts
Normal file
37
store/connection.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
import { Connection, Database } from "@/types";
|
||||
|
||||
interface ConnectionContext {
|
||||
connection: Connection;
|
||||
database: Database;
|
||||
}
|
||||
|
||||
interface ConnectionState {
|
||||
connectionList: Connection[];
|
||||
databaseList: Database[];
|
||||
currentConnectionCtx?: ConnectionContext;
|
||||
createConnection: (connection: Connection) => void;
|
||||
setCurrentConnectionCtx: (connectionCtx: ConnectionContext) => void;
|
||||
}
|
||||
|
||||
export const useConnectionStore = create<ConnectionState>()(
|
||||
persist(
|
||||
(set) => ({
|
||||
connectionList: [],
|
||||
databaseList: [],
|
||||
createConnection: (connection: Connection) => {
|
||||
set((state) => ({
|
||||
connectionList: [...state.connectionList, connection],
|
||||
}));
|
||||
},
|
||||
setCurrentConnectionCtx: (connectionCtx: ConnectionContext) =>
|
||||
set(() => ({
|
||||
currentConnectionCtx: connectionCtx,
|
||||
})),
|
||||
}),
|
||||
{
|
||||
name: "connection-storage",
|
||||
}
|
||||
)
|
||||
);
|
@ -1,8 +1,10 @@
|
||||
import { Id } from "./";
|
||||
import { Id, Timestamp } from "./";
|
||||
|
||||
export interface Chat {
|
||||
id: string;
|
||||
connectionId: Id;
|
||||
databaseName: string;
|
||||
assistantId: Id;
|
||||
title: string;
|
||||
createdAt: Timestamp;
|
||||
}
|
||||
|
Reference in New Issue
Block a user