mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-09-25 17:15:19 +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 { Chat, UNKNOWN_ID, User } from "@/types";
|
||||||
import { generateUUID } from "@/utils";
|
import { generateUUID } from "@/utils";
|
||||||
|
|
||||||
export const defaultChat: Chat = {
|
const getDefaultChat = (): Chat => {
|
||||||
|
return {
|
||||||
id: generateUUID(),
|
id: generateUUID(),
|
||||||
connectionId: UNKNOWN_ID,
|
connectionId: UNKNOWN_ID,
|
||||||
databaseName: "",
|
databaseName: "",
|
||||||
assistantId: "sql-assistant",
|
assistantId: "sql-assistant",
|
||||||
|
title: "default chat",
|
||||||
|
createdAt: Date.now(),
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
interface ChatState {
|
interface ChatState {
|
||||||
@ -20,13 +24,11 @@ interface ChatState {
|
|||||||
export const useChatStore = create<ChatState>()(
|
export const useChatStore = create<ChatState>()(
|
||||||
persist(
|
persist(
|
||||||
(set) => ({
|
(set) => ({
|
||||||
chatList: [defaultChat],
|
chatList: [getDefaultChat()],
|
||||||
currentChat: defaultChat,
|
currentChat: getDefaultChat(),
|
||||||
createChat: (assistant: User) => {
|
createChat: (assistant: User) => {
|
||||||
const chat: Chat = {
|
const chat: Chat = {
|
||||||
id: generateUUID(),
|
...getDefaultChat(),
|
||||||
connectionId: UNKNOWN_ID,
|
|
||||||
databaseName: "",
|
|
||||||
assistantId: assistant.id,
|
assistantId: assistant.id,
|
||||||
};
|
};
|
||||||
set((state) => ({
|
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 {
|
export interface Chat {
|
||||||
id: string;
|
id: string;
|
||||||
connectionId: Id;
|
connectionId: Id;
|
||||||
databaseName: string;
|
databaseName: string;
|
||||||
assistantId: Id;
|
assistantId: Id;
|
||||||
|
title: string;
|
||||||
|
createdAt: Timestamp;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user