diff --git a/assets/js/app.js b/assets/js/app.js index 8b95bb9..f6a108b 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -3746,10 +3746,32 @@ function renderBannerItem() { if (entry.type === 'expired') { const item = entry.data; const qtyDisplay = formatQuantity(item.quantity, item.unit, item.default_quantity, item.package_unit); - const daysText = item.days_expired === 0 - ? t('expiry.expired_today_long') - : t('expiry.expired_ago_long').replace('{n}', item.days_expired); + const isOpenedExpiry = !!item.opened_at; const safety = getExpiredSafety(item, item.days_expired); + + let daysText, suffix; + if (isOpenedExpiry) { + const todayMs = new Date(); todayMs.setHours(0, 0, 0, 0); + const daysSinceOpened = Math.round((todayMs - new Date(item.opened_at)) / 86400000); + daysText = daysSinceOpened === 0 + ? t('expiry.opened_today_long') + : t('expiry.opened_ago_long').replace('{n}', daysSinceOpened); + suffix = safety.level === 'ok' + ? t('expiry.opened_suffix_ok') + : safety.level === 'warning' + ? t('expiry.opened_suffix_warning') + : t('expiry.opened_suffix'); + } else { + daysText = item.days_expired === 0 + ? t('expiry.expired_today_long') + : t('expiry.expired_ago_long').replace('{n}', item.days_expired); + suffix = safety.level === 'ok' + ? t('expiry.expired_suffix_ok') + : safety.level === 'warning' + ? t('expiry.expired_suffix_warning') + : t('expiry.expired_suffix'); + } + if (safety.level === 'danger') { banner.className = 'alert-banner banner-expired banner-expired-danger'; iconEl.textContent = '🚫'; @@ -3760,16 +3782,24 @@ function renderBannerItem() { banner.className = 'alert-banner banner-expired banner-expired-ok'; iconEl.textContent = '✅'; } - const expiredSuffix = safety.level === 'ok' - ? t('expiry.expired_suffix_ok') - : safety.level === 'warning' - ? t('expiry.expired_suffix_warning') - : t('expiry.expired_suffix'); - titleEl.textContent = `${item.name}${item.brand ? ' (' + item.brand + ')' : ''} ${expiredSuffix}`; - const baseDetail = t('dashboard.banner_expired_detail').replace('{when}', daysText).replace('{qty}', qtyDisplay); - const locationTag = item.location ? ` · ${escapeHtml(item.location)}` : ''; - const expiryTag = item.expiry_date ? ` · scade il ${escapeHtml(item.expiry_date)}` : ''; - detailEl.innerHTML = `${baseDetail}${locationTag}${expiryTag} `; + titleEl.textContent = `${item.name}${item.brand ? ' (' + item.brand + ')' : ''} ${suffix}`; + + let baseDetail; + if (isOpenedExpiry) { + const locLabel = (LOCATIONS[item.location] + ? LOCATIONS[item.location].icon + ' ' + LOCATIONS[item.location].label + : (item.location || '')); + baseDetail = t('dashboard.banner_opened_detail') + .replace('{when}', daysText) + .replace('{location}', escapeHtml(locLabel)) + .replace('{qty}', qtyDisplay); + } else { + baseDetail = t('dashboard.banner_expired_detail').replace('{when}', daysText).replace('{qty}', qtyDisplay); + const locationTag = item.location ? ` · ${escapeHtml(item.location)}` : ''; + const expiryTag = item.expiry_date ? ` · ${escapeHtml(item.expiry_date)}` : ''; + baseDetail += locationTag + expiryTag; + } + detailEl.innerHTML = `${baseDetail} `; let btns = ''; if (safety.level !== 'danger') { btns += ``; diff --git a/index.html b/index.html index 322a21e..379634c 100644 --- a/index.html +++ b/index.html @@ -11,7 +11,7 @@