Files
gerbeur/src/hooks/useDocumentTitle.ts
khannurien b313f70d31
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 46s
v3: chat mentions stop going missing, and live events get a light sound — the server skips a mention notification for anyone with the chatbox open, on the grounds that they're reading it, but "open" also meant a chatbox left open in a background tab, and the same modal cleared the unread badge on every arrival, so a mention there left no bell, no badge and no trace. Reading the chat now means open and on screen: the focus flag is re-announced on every visibilitychange, and the unread count only clears while the tab is visible. A reconnect with the chatbox open refetches the latest page, since whatever was said while the socket was down never reached the live buffer. The server's mention regex excluded a backslash and the letter w rather than word characters (\\w inside a class), so an email address mentioned its domain; it now matches the autocomplete's rule. On top of that, sounds synthesised with Web Audio — a soft blip for chat you aren't looking at, throttled, and a two-note chime for notifications and for a mention you're reading live, which gets no notification of its own — claimed through localStorage so several tabs chime once, with an on/off toggle under a new Notifications section in settings. The tab title carries the unread count, composed with the page title in useDocumentTitle so navigation doesn't drop it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-22 19:25:19 +00:00

64 lines
2.4 KiB
TypeScript

import { useEffect } from "react";
// The server injects the configured site name into both <title> and this meta
// tag (see api/lib/static.ts / api/middleware/og.ts). Read it once at startup.
// In the Vite dev server the placeholder isn't substituted, so fall back.
function readSiteName(): string {
const meta = document.querySelector('meta[name="site-name"]')
?.getAttribute("content")?.trim();
if (meta && meta !== "__SITE_NAME__") return meta;
return "gerbeur";
}
export const SITE_NAME = readSiteName();
// The server injects the configured emoji into a <meta name="site-emoji"> tag.
// In the Vite dev server it's substituted too; fall back to the default truck.
function readSiteEmoji(): string {
const meta = document.querySelector('meta[name="site-emoji"]')
?.getAttribute("content")?.trim();
if (meta && meta !== "__SITE_EMOJI__") return meta;
return "🚚";
}
export const SITE_EMOJI = readSiteEmoji();
// The page title and the unread badge are set from different places (each
// page, and the live-updates provider), so both are kept here and composed.
// `null` base means no page has set one yet: keep the server-rendered title.
let baseTitle: string | null = null;
let badge = 0;
function applyTitle() {
const base = baseTitle ?? document.title.replace(/^\(\d+\+?\) /, "");
document.title = badge > 0
? `(${badge > 99 ? "99+" : badge}) ${base}`
: base;
}
/**
* Keeps document.title in sync with the current page during client-side
* navigation. Without this the tab title only ever reflects the server-rendered
* <title> from the initial page load and goes stale on every SPA navigation.
*
* - Pass a non-empty string to show `<title> · <site name>`.
* - Pass `null` to show just the site name (home / generic pages).
* - Pass `undefined` while the page-specific title is still loading: the title
* is left untouched, so a server-rendered OG title survives until data
* arrives (avoids a flash to the bare site name on direct loads).
*/
export function useDocumentTitle(title: string | null | undefined): void {
useEffect(() => {
if (title === undefined) return;
baseTitle = title ? `${title} · ${SITE_NAME}` : SITE_NAME;
applyTitle();
}, [title]);
}
/** Prefixes the tab title with an unread count, e.g. "(3) Chat · gerbeur". */
export function setTitleBadge(count: number): void {
if (count === badge) return;
badge = count;
applyTitle();
}