// Cart count badge in the site header. Lived inside `initTabs()` until the // dashboard strips became described regions and `core/tabs.ts` went away; it // was never tab behaviour, it just ran on every page from the one place that // already did. /** Fill the header cart place with its item count, and reveal it when non-zero. * Inert on pages whose header offers no cart place. * * Found by `data-place`, which is `quasi_router::Place::key` as * `quasi-webview` emits it. The header is a described `Chrome::band` since * `c7b0d3c1`, so there is no hand-written `id="nav-cart-link"` to look for; * the key is the app's own identifier and is what a script should be keying * off anyway. The badge is appended here rather than described, because a * count nothing on the server knows at render time is not a fact a * description can carry. */ export function initCartBadge(): void { const cartLink = document.querySelector('[data-place="cart"]'); if (!cartLink) return; fetch('/api/cart/count', { credentials: 'same-origin' }) .then((r) => (r.ok ? r.json() : null)) .then((data: { count?: number } | null) => { if (data && typeof data.count === 'number' && data.count > 0) { // The place is hidden until this attribute is on it; `style.css` says // so, keyed off `data-place="cart"`. cartLink.dataset.count = String(data.count); const badge = document.createElement('span'); badge.className = 'cart-badge-count'; badge.textContent = ' (' + data.count + ')'; cartLink.appendChild(badge); } }) .catch(() => {}); }