Переработать счётчик непрочитанных на плоский ref

Вместо мутации вложенных свойств дерева папок использовать
отдельный плоский ref<Record<string, number>> (ключ accountId:folderId).
Это гарантирует реактивность при любых изменениях счётчика.

- folders.store: unreadCounts, initUnreadCounts, getFolderUnread
- MessageList: activeUnread computed читает из getFolderUnread
- FolderTreeItem: unreadCount computed читает из getFolderUnread,
  не зависит от folder.unreadCount prop

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
vodyanoysh 2026-03-31 23:05:24 +07:00
parent df5e7f430b
commit c1a49cc627
3 changed files with 36 additions and 21 deletions

View file

@ -16,6 +16,10 @@ const selectMode = ref(false)
const folderName = computed(() => foldersStore.activeFolder?.name ?? '')
const hasSelected = computed(() => messagesStore.selectedIds.size > 0)
const activeUnread = computed(() => {
if (!accountsStore.activeAccountId || !foldersStore.activeFolderId) return 0
return foldersStore.getFolderUnread(accountsStore.activeAccountId, foldersStore.activeFolderId)
})
const allVisibleSelected = computed(() =>
messagesStore.filteredMessages.length > 0 &&
messagesStore.filteredMessages.every((m) => messagesStore.selectedIds.has(m.id))
@ -136,13 +140,13 @@ async function handleSwipeAction(action: SwipeAction, messageId: string): Promis
<div class="message-list-header">
<h2 class="folder-title truncate">{{ folderName }}</h2>
<button
v-if="(foldersStore.activeFolder?.unreadCount ?? 0) > 0"
v-if="activeUnread > 0"
class="message-count"
:class="{ 'unread-active': messagesStore.unreadOnly }"
title="Только непрочитанные"
@click="messagesStore.unreadOnly = !messagesStore.unreadOnly"
>
{{ foldersStore.activeFolder?.unreadCount }}
{{ activeUnread }}
</button>
<div class="header-actions">
<button

View file

@ -3,6 +3,7 @@ import { ref, computed } from 'vue'
import type { Folder } from '@shared/types/folder'
import { SpecialFolderRole } from '@shared/types/folder'
import { useFoldersStore } from '../../stores/folders.store'
import { useAccountsStore } from '../../stores/accounts.store'
import AppIcon from '../AppIcon.vue'
import type { IconName } from '../AppIcon.vue'
@ -12,10 +13,15 @@ const props = defineProps<{
}>()
const foldersStore = useFoldersStore()
const accountsStore = useAccountsStore()
const expanded = ref(true)
const isActive = computed(() => foldersStore.activeFolderId === props.folder.id)
const hasChildren = computed(() => props.folder.children.length > 0)
const unreadCount = computed(() => {
if (!accountsStore.activeAccountId) return 0
return foldersStore.getFolderUnread(accountsStore.activeAccountId, props.folder.id)
})
const folderIcon = computed((): IconName => {
switch (props.folder.role) {
@ -59,8 +65,8 @@ function toggleExpand(event: Event): void {
<AppIcon :name="folderIcon" :size="14" class="folder-icon" />
<span class="folder-name truncate">{{ folder.name }}</span>
<span v-if="folder.unreadCount > 0" class="unread-badge">
{{ folder.unreadCount }}
<span v-if="unreadCount > 0" class="unread-badge">
{{ unreadCount }}
</span>
</button>

View file

@ -13,6 +13,10 @@ export const useFoldersStore = defineStore('folders', () => {
const activeFolderId = ref<string | null>(null)
const loading = ref(false)
// Плоский счётчик непрочитанных: ключ `${accountId}:${folderId}`
// Хранится отдельно от дерева — гарантированно реактивен
const unreadCounts = ref<Record<string, number>>({})
const folders = computed(() => {
if (!accountsStore.activeAccountId) return []
return foldersByAccount.value[accountsStore.activeAccountId] ?? []
@ -27,6 +31,7 @@ export const useFoldersStore = defineStore('folders', () => {
try {
const result = await invoke('folders:list', { accountId })
foldersByAccount.value[accountId] = result.folders
initUnreadCounts(accountId, result.folders)
// Auto-select inbox
const inbox = findFolderByRole(result.folders, SpecialFolderRole.Inbox)
@ -44,10 +49,21 @@ export const useFoldersStore = defineStore('folders', () => {
activeFolderId.value = folderId
}
function initUnreadCounts(accountId: string, folders: Folder[]): void {
for (const f of folders) {
unreadCounts.value[`${accountId}:${f.id}`] = f.unreadCount
if (f.children.length > 0) initUnreadCounts(accountId, f.children)
}
}
function updateFolderUnread(accountId: string, folderId: string, delta: number): void {
const tree = foldersByAccount.value[accountId]
if (!tree) return
patchUnreadInTree(tree, folderId, delta)
const key = `${accountId}:${folderId}`
const current = unreadCounts.value[key] ?? 0
unreadCounts.value[key] = Math.max(0, current + delta)
}
function getFolderUnread(accountId: string, folderId: string): number {
return unreadCounts.value[`${accountId}:${folderId}`] ?? 0
}
// Auto-load folders when active account changes
@ -74,28 +90,17 @@ export const useFoldersStore = defineStore('folders', () => {
return {
foldersByAccount,
folders,
unreadCounts,
activeFolderId,
activeFolder,
loading,
loadFolders,
setActiveFolder,
updateFolderUnread
updateFolderUnread,
getFolderUnread
}
})
// Заменяет элемент массива целиком — гарантированно триггерит Vue-реактивность
function patchUnreadInTree(folders: Folder[], folderId: string, delta: number): boolean {
for (let i = 0; i < folders.length; i++) {
if (folders[i].id === folderId) {
folders[i] = { ...folders[i], unreadCount: Math.max(0, folders[i].unreadCount + delta) }
return true
}
if (folders[i].children.length > 0 && patchUnreadInTree(folders[i].children, folderId, delta)) {
return true
}
}
return false
}
function findFolderById(folders: Folder[], id: string | null): Folder | null {
if (!id) return null