Files
kycnotme/web/src/components/DynamicFavicon.astro

80 lines
2.0 KiB
Plaintext
Raw Normal View History

2025-06-09 10:00:55 +00:00
---
2025-06-14 18:56:58 +00:00
import { DEPLOYMENT_MODE } from '../lib/client/envVariables'
2025-06-09 10:00:55 +00:00
import { prisma } from '../lib/prisma'
const user = Astro.locals.user
const hasUnreadNotifications = await Astro.locals.banners.try(
'Error getting unread notification count',
async () =>
user
? !!(await prisma.notification.findFirst({
where: { userId: user.id, read: false },
select: { id: true },
}))
: false,
false
)
function addBadgeIfUnread(href: string) {
if (hasUnreadNotifications) return href.replace('.svg', '-badge.svg')
return href
}
---
{
DEPLOYMENT_MODE === 'production' && (
<>
2025-06-09 10:53:52 +00:00
<link rel="icon" type="image/svg+xml" sizes="any" href={addBadgeIfUnread('/favicon.svg')} />
2025-06-09 10:00:55 +00:00
<link
rel="icon"
type="image/svg+xml"
2025-06-09 10:53:52 +00:00
sizes="any"
2025-06-09 10:00:55 +00:00
href={addBadgeIfUnread('/favicon-lightmode.svg')}
media="(prefers-color-scheme: light)"
/>
</>
)
}
{
DEPLOYMENT_MODE === 'development' && (
2025-06-09 10:53:52 +00:00
<>
<link rel="icon" type="image/svg+xml" sizes="any" href={addBadgeIfUnread('/favicon-dev.svg')} />
<link
rel="icon"
type="image/svg+xml"
sizes="any"
href={addBadgeIfUnread('/favicon-dev-lightmode.svg')}
media="(prefers-color-scheme: light)"
/>
</>
2025-06-09 10:00:55 +00:00
)
}
{
DEPLOYMENT_MODE === 'staging' && (
2025-06-09 10:53:52 +00:00
<>
<link rel="icon" type="image/svg+xml" sizes="any" href={addBadgeIfUnread('/favicon-stage.svg')} />
<link
rel="icon"
type="image/svg+xml"
sizes="any"
href={addBadgeIfUnread('/favicon-stage-lightmode.svg')}
media="(prefers-color-scheme: light)"
/>
</>
2025-06-09 10:00:55 +00:00
)
}
<script>
2025-06-10 17:42:42 +00:00
document.addEventListener('sse:new-notification', () => {
2025-06-09 10:00:55 +00:00
const links = document.querySelectorAll('link[rel="icon"]')
links.forEach((link) => {
const href = link.getAttribute('href')
if (href && href.includes('favicon') && !href.endsWith('-badge.svg')) {
const newHref = href.replace('.svg', '-badge.svg')
link.setAttribute('href', newHref)
}
})
})
</script>