101 lines
3.1 KiB
TypeScript
101 lines
3.1 KiB
TypeScript
import { ref, computed } from 'vue'
|
||
import { defineStore } from 'pinia'
|
||
import type { AccountConfig } from '@shared/types/account'
|
||
import { useIpc } from '../composables/useIpc'
|
||
|
||
export const useAccountsStore = defineStore('accounts', () => {
|
||
const { invoke, on } = useIpc()
|
||
|
||
const accounts = ref<AccountConfig[]>([])
|
||
const activeAccountId = ref<string | null>(null)
|
||
const connectionStatus = ref<Record<string, { connected: boolean; error?: string }>>({})
|
||
const loading = ref(false)
|
||
|
||
const activeAccount = computed(() =>
|
||
accounts.value.find((a) => a.id === activeAccountId.value) ?? null
|
||
)
|
||
|
||
async function loadAccounts(): Promise<void> {
|
||
loading.value = true
|
||
try {
|
||
accounts.value = await invoke('accounts:list')
|
||
|
||
// Connect all accounts first, then set active
|
||
// (folders watcher fires on activeAccountId change — account must be connected by then)
|
||
for (const acc of accounts.value) {
|
||
const result = await invoke('accounts:connect', { accountId: acc.id })
|
||
connectionStatus.value[acc.id] = {
|
||
connected: result.success,
|
||
error: result.error
|
||
}
|
||
// Инициализируем курсоры новых UID сразу после подключения —
|
||
// иначе первый autoCheckTimer только заполняет курсоры, а второй уже шлёт уведомления.
|
||
if (result.success) {
|
||
invoke('messages:poll', { accountId: acc.id }).catch(() => {})
|
||
}
|
||
}
|
||
|
||
if (accounts.value.length > 0 && !activeAccountId.value) {
|
||
activeAccountId.value = accounts.value[0].id
|
||
}
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
async function addAccount(config: AccountConfig): Promise<{ success: boolean; error?: string }> {
|
||
const result = await invoke('accounts:add', config)
|
||
if (result.success) {
|
||
await loadAccounts()
|
||
activeAccountId.value = config.id
|
||
}
|
||
return result
|
||
}
|
||
|
||
async function removeAccount(accountId: string): Promise<void> {
|
||
await invoke('accounts:remove', { accountId })
|
||
accounts.value = accounts.value.filter((a) => a.id !== accountId)
|
||
delete connectionStatus.value[accountId]
|
||
if (activeAccountId.value === accountId) {
|
||
activeAccountId.value = accounts.value[0]?.id ?? null
|
||
}
|
||
}
|
||
|
||
async function testConnection(config: AccountConfig): Promise<{ success: boolean; error?: string }> {
|
||
return invoke('accounts:test', config)
|
||
}
|
||
|
||
async function updateAccount(config: AccountConfig): Promise<{ success: boolean; error?: string }> {
|
||
const result = await invoke('accounts:update', config)
|
||
if (result.success) {
|
||
await loadAccounts()
|
||
}
|
||
return result
|
||
}
|
||
|
||
function setActive(accountId: string): void {
|
||
activeAccountId.value = accountId
|
||
}
|
||
|
||
// Listen for status updates
|
||
on('account:status', (data) => {
|
||
connectionStatus.value[data.accountId] = {
|
||
connected: data.connected,
|
||
error: data.error
|
||
}
|
||
})
|
||
|
||
return {
|
||
accounts,
|
||
activeAccountId,
|
||
activeAccount,
|
||
connectionStatus,
|
||
loading,
|
||
loadAccounts,
|
||
addAccount,
|
||
removeAccount,
|
||
testConnection,
|
||
updateAccount,
|
||
setActive
|
||
}
|
||
})
|