/** * The signed-in shopper's unread message count, shared between the shell badge * and the message center. Every read/mark-all/delete action refreshes the count * through `$api.getUnreadCount()` so the badge never drifts from the server. */ export function useUnreadMessages() { const { $api } = useNuxtApp(); const session = useSessionStore(); const unread = useState("unread-messages", () => 0); /** Anonymous shoppers have no message center, so the count stays at zero. */ async function refresh(): Promise { if (!session.isLoggedIn) { unread.value = 0; return; } try { unread.value = (await $api.getUnreadCount()).unread; } catch { unread.value = 0; } } /** Let a mutating page push the authoritative number without a second call. */ function setCount(count: number): void { unread.value = Math.max(0, count); } return { unread, refresh, setCount }; }