mirror of
https://github.com/ArthurDanjou/artchat.git
synced 2026-01-20 10:22:15 +01:00
- Replaced Nuxt ESLint configuration with Antfu's ESLint config. - Removed 'nuxt-visitors' module from Nuxt configuration. - Added linting scripts to package.json for easier code quality checks. - Introduced a new API endpoint for fetching weather data from OpenWeather. - Enhanced chat types with new enums and properties for better state management. - Added OpenWeather response types for improved type safety. - Updated social links in types/index.ts to include an email contact.
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import type { ChatMessage, ChatType } from '~~/types'
|
|
import { ChatFetchState, ChatSender, ChatState } from '~~/types'
|
|
|
|
export const useChatStore = defineStore('chat', () => {
|
|
const messages = ref<ChatMessage[]>([])
|
|
let id = 0
|
|
|
|
const canSend = computed(() => {
|
|
return !messages.value.some(msg => msg.state === ChatState.LOADING)
|
|
})
|
|
|
|
function addMessage(type: ChatType, content: string, sender: ChatSender, fetchStates: ChatFetchState[]) {
|
|
if (sender === ChatSender.ARTHUR) {
|
|
const message: ChatMessage = {
|
|
id: id++,
|
|
type,
|
|
content: null,
|
|
sender,
|
|
state: ChatState.LOADING,
|
|
fetchStates: [...fetchStates, ChatFetchState.DONE],
|
|
}
|
|
|
|
messages.value.push(message)
|
|
}
|
|
else {
|
|
messages.value.push({
|
|
id: id++,
|
|
type,
|
|
content,
|
|
sender,
|
|
state: ChatState.SENT,
|
|
fetchStates: [ChatFetchState.DONE],
|
|
})
|
|
}
|
|
}
|
|
|
|
function clearMessages() {
|
|
messages.value.splice(0, messages.value.length)
|
|
id = 0
|
|
}
|
|
|
|
function setLoadingState(messageId: number, loadingState: ChatState) {
|
|
const message = messages.value.find(msg => msg.id === messageId)
|
|
if (message) {
|
|
message.state = loadingState
|
|
}
|
|
}
|
|
|
|
return { messages, addMessage, clearMessages, canSend, setLoadingState }
|
|
})
|