47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import { computed, ref } from 'vue'
|
|
import type { User } from '@/stores/users.ts'
|
|
import type { Message } from '@/stores/messages.ts'
|
|
import { useAuthStore } from '@/stores/auth.ts'
|
|
|
|
export interface Chat {
|
|
id: string
|
|
typeId: number
|
|
name: string
|
|
users: User[]
|
|
message?: Message
|
|
image?: string
|
|
}
|
|
|
|
export const useChatsStore = defineStore('chats', () => {
|
|
const authStore = useAuthStore()
|
|
const chats = ref<Chat[]>([])
|
|
const selected = ref<string[]>([])
|
|
|
|
const selectedChat = computed(() => {
|
|
if (!selected.value.length) return
|
|
|
|
return chats.value.find((chat: Chat) => chat.id === selected.value[0])
|
|
})
|
|
|
|
function getChatInfo(chat: Chat) {
|
|
switch (chat.typeId) {
|
|
case 1:
|
|
const user = chat.users.find((user) => {
|
|
return user.id !== authStore.me?.id
|
|
})
|
|
return user ?? chat
|
|
case 2:
|
|
return chat
|
|
default:
|
|
return chat
|
|
}
|
|
}
|
|
|
|
function getChatLastMessage(chat: Chat) {
|
|
return chats.value.find((el) => el.id === chat.id)
|
|
}
|
|
|
|
return { chats, selected, selectedChat, getChatInfo }
|
|
})
|