import { useSyncExternalStore } from "react" import { WallpaperImage } from "@/components/wallpaper" import type { ClockFont, ClockStyle } from "@/lib/clock" import { cn } from "@/vendor/collabry/lib/cn" const subscribe = (tick: () => void) => { const timer = setInterval(tick, 1000) return () => clearInterval(timer) } // changes once a minute, so the clock re-renders once a minute const currentMinute = () => Math.floor(Date.now() / 60_000) const FONTS: Record = { serif: "font-serif tracking-wide", sans: "font-sans font-bold tracking-tight", light: "font-sans font-extralight tracking-tight", rounded: "font-rounded font-extrabold", slab: "font-slab font-semibold", } // What the wallpaper looks like through the digits, and how much white is in the glass. const GLASS: Record, [string, string]> = { glass: ["blur-xl saturate-150 brightness-150", "bg-white/45"], tinted: ["blur-3xl saturate-150 brightness-150", "bg-white/20"], } export function Clock({ font, look, wallpaper, }: { font: ClockFont look: ClockStyle wallpaper: string | null }) { // null on the server: its clock and locale are not the visitor's const minute = useSyncExternalStore( subscribe, currentMinute, () => null ) const now = minute === null ? null : new Date(minute * 60_000) const parts = now ? new Intl.DateTimeFormat(undefined, { hour: "numeric", minute: "2-digit", }).formatToParts(now) : [] const time = parts .filter((p) => p.type !== "dayPeriod") .map((p) => p.value) .join("") .trim() const period = parts.find((p) => p.type === "dayPeriod")?.value // Each piece fades in by itself. Opacity on a common parent would cut the // glass off from the wallpaper it blends with. const fade = cn("transition-opacity duration-500", !now && "opacity-0") const digits = ( <> {time || "0:00"} {period && ( {period} )} ) return (

{now ? now.toLocaleDateString(undefined, { weekday: "long", month: "long", day: "numeric", }) : "\u00a0"}

) }