erp-platform/ui/src/store/base.model.ts

105 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-05-06 06:45:49 +00:00
import type { Action } from 'easy-peasy'
import { action } from 'easy-peasy'
type ChatType = 'chat' | 'query' | 'analyze'
interface BaseContent {
type: ChatType
question: string
sql: string | null
answer: string | any[]
chart?: string
error?: string
}
type MessageContent = string | BaseContent
export interface Message {
role: 'user' | 'assistant'
content: MessageContent
}
export interface StoreError {
id: string
title: string
message: string
cid: string
statusCode?: string
}
export interface BaseStoreModel {
common: {
currentRouteKey: string
tabHasFocus: boolean
}
messages: {
errors: StoreError[]
// success: string[]
warning: string[]
aiPosts: Message[]
}
}
export interface BaseStoreActions {
common: {
setCurrentRouteKey: Action<BaseStoreModel['common'], string>
setTabHasFocus: Action<BaseStoreModel['common'], boolean>
}
messages: {
addError: Action<BaseStoreModel['messages'], StoreError>
removeError: Action<BaseStoreModel['messages'], string>
// setSuccess: Action<BaseStoreModel, string>
setWarning: Action<BaseStoreModel['messages'], string>
addAiPost: Action<BaseStoreModel['messages'], Message>
setAiPosts: Action<BaseStoreModel['messages'], Message[]>
}
}
export type BaseModel = BaseStoreModel & BaseStoreActions
const initialState: BaseStoreModel = {
common: { currentRouteKey: '', tabHasFocus: false },
messages: {
errors: [],
// success: [],
warning: [],
aiPosts: [],
},
}
export const baseModel: BaseModel = {
common: {
...initialState.common,
setCurrentRouteKey: action((state, payload) => {
state.currentRouteKey = payload
}),
setTabHasFocus: action((state, payload) => {
state.tabHasFocus = payload
}),
},
messages: {
...initialState.messages,
addError: action((state, payload) => {
state.errors = [...state.errors, payload]
}),
removeError: action((state, payload) => {
state.errors = [...state.errors.filter((a) => a.id != payload)]
}),
setWarning: action((state, payload) => {
if (payload) {
state.warning = [payload]
} else {
state.warning = []
}
}),
addAiPost: action((state, payload) => {
state.aiPosts = [...state.aiPosts, payload]
2025-06-28 21:34:28 +00:00
localStorage.setItem('AiPosts', JSON.stringify(state.aiPosts))
2025-05-06 06:45:49 +00:00
}),
setAiPosts: action((state, payload) => {
state.aiPosts = payload
})
},
}